Skip to content

Ignore ObjectFieldNode ordering when checking if selections can be merged #8432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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 @@ -99,6 +99,154 @@ fragment mergeIdenticalFieldsWithIdenticalValues on Dog {
""");
}

[Fact]
public void IdenticalInputFieldValuesButDifferentOrdering()
{
ExpectValid(
"""
{
findDog(complex: { name: "A", owner: "B", child: { owner: "D", name: "C" } }) {
name
}
... mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering
}

fragment mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering on Query {
findDog(complex: { owner: "B", name: "A", child: { name: "C", owner: "D" } }) {
barks
}
}
""");
}

[Fact]
public void ObjectValueWithNoFields()
{
ExpectValid(
"""
{
findDog(complex: { }) {
name
}
... mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering
}

fragment mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering on Query {
findDog(complex: { }) {
barks
}
}
""");
}

[Fact]
public void ConflictingInputFieldValues()
{
ExpectErrors(
"""
{
findDog(complex: { name: "A", owner: "B" }) {
name
}
... mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering
}

fragment mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering on Query {
findDog(complex: { owner: "OTHER", name: "A" }) {
barks
}
}
""",
t => Assert.Equal(
"Encountered fields for the same object that cannot be merged.",
t.Message));
}

[Fact]
public void ConflictingNestedInputFieldValues()
{
ExpectErrors(
"""
{
findDog(complex: { name: "A", owner: "B", child: { owner: "D", name: "C" } }) {
name
}
... mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering
}

fragment mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering on Query {
findDog(complex: { owner: "B", name: "A", child: { name: "C", owner: "OTHER" } }) {
barks
}
}
""",
t => Assert.Equal(
"Encountered fields for the same object that cannot be merged.",
t.Message));
}

[Fact]
public void IdenticalInputFieldListValuesButDifferentOrdering()
{
ExpectValid(
"""
{
findDog3(complexList: [
{ name: "A", owner: "B", childList: [{ name: "A1", owner: "B1" }, { owner: "C1", name: "D1" }] },
{ owner: "C", name: "D", childList: [{ name: "A1", owner: "B1" }, { owner: "C1", name: "D1" }] }]) {
name
}
findDog3(complexList: [
{ owner: "B", name: "A", childList: [{ owner: "B1", name: "A1" }, { name: "D1", owner: "C1" }] },
{ name: "D", owner: "C", childList: [{ owner: "B1", name: "A1" }, { name: "D1", owner: "C1" }] }]) {
barks
}
}
""");
}

[Fact]
public void ConflictingInputFieldListValues()
{
ExpectErrors(
"""
{
findDog3(complexList: [{ name: "A", owner: "B" }, { owner: "C", name: "D" }]) {
name
}
findDog3(complexList: [{ owner: "B", name: "A" }, { name: "OTHER", owner: "C" }]) {
barks
}
}
""",
t => Assert.Equal(
"Encountered fields for the same object that cannot be merged.",
t.Message));
}

[Fact]
public void ConflictingNestedInputFieldListValues()
{
ExpectErrors(
"""
{
findDog3(complexList: [
{ name: "A", owner: "B", childList: [{ name: "A1", owner: "B1" }, { owner: "C1", name: "D1" }] },
{ owner: "C", name: "D", childList: [{ name: "A1", owner: "B1" }, { owner: "C1", name: "D1" }] }]) {
name
}
findDog3(complexList: [
{ owner: "B", name: "A", childList: [{ owner: "B1", name: "A1" }, { name: "D1", owner: "C1" }] },
{ name: "D", owner: "C", childList: [{ owner: "B1", name: "A1" }, { name: "D1", owner: "OTHER" }] }]) {
barks
}
}
""",
t => Assert.Equal(
"Encountered fields for the same object that cannot be merged.",
t.Message));
}

[Fact]
public void ConflictingArgsOnValues()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace HotChocolate.Validation;

public record ComplexInput3(string Name, string Owner, List<ComplexInput3>? ChildList = null);
5 changes: 5 additions & 0 deletions src/HotChocolate/Core/test/Validation.Tests/Models/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class Query
return null;
}

public Dog? FindDog3(List<ComplexInput3> complexList)
{
return null;
}

public bool BooleanList(bool[]? booleanListArg)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"Message": "Encountered fields for the same object that cannot be merged.",
"Code": null,
"Path": null,
"Locations": [
{
"Line": 2,
"Column": 5
},
{
"Line": 5,
"Column": 5
}
],
"Extensions": {
"declaringTypeA": "Query",
"declaringTypeB": "Query",
"fieldA": "findDog3",
"fieldB": "findDog3",
"typeA": "Dog",
"typeB": "Dog",
"responseNameA": "findDog3",
"responseNameB": "findDog3",
"specifiedBy": "https://spec.graphql.org/October2021/#sec-Field-Selection-Merging"
},
"Exception": null
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"Message": "Encountered fields for the same object that cannot be merged.",
"Code": null,
"Path": null,
"Locations": [
{
"Line": 2,
"Column": 5
},
{
"Line": 9,
"Column": 5
}
],
"Extensions": {
"declaringTypeA": "Query",
"declaringTypeB": "Query",
"fieldA": "findDog",
"fieldB": "findDog",
"typeA": "Dog",
"typeB": "Dog",
"responseNameA": "findDog",
"responseNameB": "findDog",
"specifiedBy": "https://spec.graphql.org/October2021/#sec-Field-Selection-Merging"
},
"Exception": null
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"Message": "Encountered fields for the same object that cannot be merged.",
"Code": null,
"Path": null,
"Locations": [
{
"Line": 2,
"Column": 5
},
{
"Line": 7,
"Column": 5
}
],
"Extensions": {
"declaringTypeA": "Query",
"declaringTypeB": "Query",
"fieldA": "findDog3",
"fieldB": "findDog3",
"typeA": "Dog",
"typeB": "Dog",
"responseNameA": "findDog3",
"responseNameB": "findDog3",
"specifiedBy": "https://spec.graphql.org/October2021/#sec-Field-Selection-Merging"
},
"Exception": null
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"Message": "Encountered fields for the same object that cannot be merged.",
"Code": null,
"Path": null,
"Locations": [
{
"Line": 2,
"Column": 5
},
{
"Line": 9,
"Column": 5
}
],
"Extensions": {
"declaringTypeA": "Query",
"declaringTypeB": "Query",
"fieldA": "findDog",
"fieldB": "findDog",
"typeA": "Dog",
"typeB": "Dog",
"responseNameA": "findDog",
"responseNameB": "findDog",
"specifiedBy": "https://spec.graphql.org/October2021/#sec-Field-Selection-Merging"
},
"Exception": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,36 @@ private bool Equals(ObjectTypeExtensionNode x, ObjectTypeExtensionNode y)
Equals(x.Fields, y.Fields);

private bool Equals(ObjectValueNode x, ObjectValueNode y)
=> Equals(x.Fields, y.Fields);
{
if (x.Fields.Count != y.Fields.Count)
{
return false;
}

for (var i = 0; i < x.Fields.Count; i++)
{
var xField = x.Fields[i];
ObjectFieldNode? matchingField = null;

for (var j = 0; j < y.Fields.Count; j++)
{
var yField = y.Fields[j];

if (Equals(xField.Name, yField.Name))
{
matchingField = yField;
break;
}
}

if (matchingField is null || !Equals(xField.Value, matchingField.Value))
{
Comment on lines +366 to +383
Copy link
Preview

Copilot AI Jul 9, 2025

Choose a reason for hiding this comment

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

This nested loop results in O(n²) comparisons for each ObjectValueNode. Consider building a lookup (e.g., a dictionary keyed by field name) for y.Fields to reduce the complexity to O(n).

Suggested change
for (var i = 0; i < x.Fields.Count; i++)
{
var xField = x.Fields[i];
ObjectFieldNode? matchingField = null;
for (var j = 0; j < y.Fields.Count; j++)
{
var yField = y.Fields[j];
if (Equals(xField.Name, yField.Name))
{
matchingField = yField;
break;
}
}
if (matchingField is null || !Equals(xField.Value, matchingField.Value))
{
var yFieldsByName = y.Fields.ToDictionary(field => field.Name, field => field);
for (var i = 0; i < x.Fields.Count; i++)
{
var xField = x.Fields[i];
if (!yFieldsByName.TryGetValue(xField.Name, out var matchingField) || !Equals(xField.Value, matchingField.Value))
{

Copilot uses AI. Check for mistakes.

return false;
}
}

return true;
}

private bool Equals(OperationDefinitionNode x, OperationDefinitionNode y)
=> SyntaxComparer.BySyntax.Equals(x.Name, y.Name) &&
Expand Down Expand Up @@ -1060,9 +1089,8 @@ private int GetHashCode(ObjectValueNode node)
var hashCode = new HashCode();
hashCode.Add(node.Kind);

for (var i = 0; i < node.Fields.Count; i++)
foreach (var field in node.Fields.OrderBy(field => field.Name.Value, StringComparer.Ordinal))
{
var field = node.Fields[i];
hashCode.Add(GetHashCode(field));
}

Expand Down
Loading