Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public FileReferenceNode(FileReference value)

string IValueNode<string>.Value => Value.FileName;

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location) => this;

/// <summary>
/// Gets the children of this node.
/// </summary>
Expand Down
33 changes: 12 additions & 21 deletions src/HotChocolate/Core/src/Execution/Processing/VariableRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,51 +223,42 @@ private static bool TryRewriteValue(
{
case SyntaxKind.Variable:
rewritten = Rewrite((VariableNode)original, defaultValue, variableValues);
return true;
break;

case SyntaxKind.ObjectValue when type.Kind == TypeKind.InputObject:
rewritten = Rewrite(
(ObjectValueNode)original,
(InputObjectType)type,
variableValues);

if (ReferenceEquals(rewritten, original))
{
rewritten = null;
return false;
}
return true;
break;

case SyntaxKind.ObjectValue when type.Kind == TypeKind.List:
rewritten = Rewrite(
(ObjectValueNode)original,
(ListType)type,
variableValues);

if (ReferenceEquals(rewritten, original))
{
rewritten = null;
return false;
}
return true;

case SyntaxKind.ListValue when type.Kind == TypeKind.List:
rewritten = Rewrite(
(ListValueNode)original,
(ListType)type,
variableValues);

if (ReferenceEquals(rewritten, original))
{
rewritten = null;
return false;
}
return true;
break;

default:
rewritten = null;
return false;
}

if (ReferenceEquals(rewritten, original))
{
rewritten = null;
return false;
}

rewritten = rewritten.WithLocation(original.Location);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the original location inserted again. There are multiple issues with this.

  1. The location is not really correct after a rewrite. Think about multiple rewrites happening that shorten and lengthening strings. The location after a rewrite is inherently incorrect.

  2. Wither now does an additional copy which means that we create an additional object for this mutation, potentially in the hot path on large syntax trees that have maybe hundreds of these mutations.

  3. Location on a production server should be simply shut off to save perf.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to read into the issue a bit more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding 1: Shouldn’t the location always reflect the position of the associated (input) syntax element within the original document? For example, given

query($v: String!) { foo(arg: { bar: { id: $v } }) }

with

{ "v": "MyLongString" }

supplied as an argument, I would expect that in the rewritten query

query($v: String!) { foo(arg: { bar: { id: "MyLongString" } }) }

the location of "MyLongString" would still point back to $v (at least as a dev).
If this assumption is incorrect, the entire PR can be closed.
I dont see how multiple rewrites should do harm here, as we always stay with the original location - can you give a sample? My assumption here is that for simple variable-to-value rewrites (any only for such!, probably I should just adjust the first case of the switch), retaining the original variable location is the most useful behavior.

Regarding 2:
I see the concern. A possible workaround would be to pass the original location directly when creating the rewritten value node. This would avoid introducing any additional overhead and doesnt look too complicated

Regarding 3:
Is this possible today? At least for a public API I think this would be problematic, as error messages require a loaction (well OK, its not strictly required, ist only "should" be present according to the spec)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding 3, yes, this is a well used practice that is also used by the GraphQL reference implementation.

https://github.com/graphql/graphql-js/blob/60ae6c48b9c78332bf3d6036e7d931a3617d0674/src/language/parser.ts#L79

return true;
}

private static IValueNode Rewrite(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"locations": [
{
"line": 1,
"column": 57
"column": 72
}
],
"path": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"locations": [
{
"line": 1,
"column": 59
"column": 81
}
],
"path": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"locations": [
{
"line": 1,
"column": 53
"column": 60
}
],
"path": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"locations": [
{
"line": 1,
"column": 54
"column": 69
}
],
"path": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
{
"data": {
"foo": {
"kind": "ObjectValue",
"location": null,
"location": {
"start": 31,
"end": 36,
"line": 1,
"column": 32
},
"fields": [
{
"kind": "ObjectField",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
{
"data": {
"foo": {
"kind": "ObjectValue",
"location": null,
"location": {
"start": 31,
"end": 36,
"line": 1,
"column": 32
},
"fields": [
{
"kind": "ObjectField",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public BooleanValueNode(
public BooleanValueNode WithLocation(Location? location)
=> new(location, Value);

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="Value" /> with <paramref name="value" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ public interface IValueNode : ISyntaxNode
/// Gets the value.
/// </summary>
object? Value { get; }

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="Location" /> with <paramref name="location" />.
/// </summary>
/// <param name="location">
/// The location that shall be used to replace the current location.
/// </param>
/// <returns>
/// Returns the new node with the new <paramref name="location" />.
/// </returns>
IValueNode WithLocation(Location? location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ public ReadOnlyMemorySegment AsMemorySegment()
public EnumValueNode WithLocation(Location? location)
=> new(location, Value);

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="Value" /> with <paramref name="value" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ public ReadOnlyMemorySegment AsMemorySegment()
public FloatValueNode WithLocation(Location? location)
=> new(location, Format) { _memorySegment = _memorySegment, _value = _value };

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="Value" /> with <paramref name="value" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,10 @@ public ReadOnlyMemorySegment AsMemorySegment()
public IntValueNode WithLocation(Location? location)
=> new(location) { _memorySegment = _memorySegment, _value = _value };

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="Value" /> with <paramref name="value" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public ListValueNode(
/// </returns>
public ListValueNode WithLocation(Location? location) => new(location, Items);

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="Items" /> with <paramref name="items" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public override bool Equals(object? obj)
/// </returns>
public NullValueNode WithLocation(Location? location) => new(location);

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Gets the default null value instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public ObjectValueNode(Location? location, IReadOnlyList<ObjectFieldNode> fields
public ObjectValueNode WithLocation(Location? location)
=> new(location, Fields);

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="Fields" /> with <paramref name="fields" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public ReadOnlyMemorySegment AsMemorySegment()
public StringValueNode WithLocation(Location? location)
=> new(location, Value, Block);

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

public StringValueNode WithValue(string value)
=> new(Location, value, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public IEnumerable<ISyntaxNode> GetNodes()
/// </returns>
public VariableNode WithLocation(Location? location) => new(location, Name);

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location)
=> WithLocation(location);

/// <summary>
/// Creates a new node from the current instance and replaces the
/// <see cref="NamedSyntaxNode.Name" /> with <paramref name="name" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public FileValueNode(IFile file)

string IValueNode<string>.Value => Value.Name;

/// <inheritdoc />
IValueNode IValueNode.WithLocation(Location? location) => this;

/// <summary>
/// Determines whether the specified <see cref="FileValueNode"/>
/// is equal to the current <see cref="FileValueNode"/>.
Expand Down