Skip to content

Commit 029d029

Browse files
Improve performance and handle empty object case
1 parent 00440d2 commit 029d029

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/HotChocolate/Core/src/Validation/Rules/FieldVisitor.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ private static bool AreArgumentsIdentical(FieldNode fieldA, FieldNode fieldB)
370370

371371
if (BySyntax.Equals(argumentA.Name, argumentB.Name))
372372
{
373-
if (IsValueIdentical(argumentA.Value, argumentB.Value))
373+
if (AreValuesIdentical(argumentA.Value, argumentB.Value))
374374
{
375375
validPairs++;
376376
}
@@ -383,9 +383,7 @@ private static bool AreArgumentsIdentical(FieldNode fieldA, FieldNode fieldB)
383383
return fieldA.Arguments.Count == validPairs;
384384
}
385385

386-
private static bool IsValueIdentical(
387-
IValueNode valueA,
388-
IValueNode valueB)
386+
private static bool AreValuesIdentical(IValueNode valueA, IValueNode valueB)
389387
{
390388
var stack = new Stack<(IValueNode ValueA, IValueNode ValueB)>();
391389
stack.Push((valueA, valueB));
@@ -413,27 +411,22 @@ private static bool IsValueIdentical(
413411
return false;
414412
}
415413

416-
for (var i = 0; i < objectA.Fields.Count; i++)
414+
if (objectA.Fields.Count == 0)
417415
{
418-
var fieldA = objectA.Fields[i];
419-
var matchFound = false;
416+
continue;
417+
}
420418

421-
for (var j = 0; j < objectB.Fields.Count; j++)
422-
{
423-
var fieldB = objectB.Fields[j];
419+
var fieldDictB = objectB.Fields
420+
.ToDictionary(f => f.Name, f => f.Value, BySyntax);
424421

425-
if (BySyntax.Equals(fieldA.Name, fieldB.Name))
426-
{
427-
stack.Push((fieldA.Value, fieldB.Value));
428-
matchFound = true;
429-
break;
430-
}
431-
}
432-
433-
if (!matchFound)
422+
foreach (var fieldA in objectA.Fields)
423+
{
424+
if (!fieldDictB.TryGetValue(fieldA.Name, out var fieldBValue))
434425
{
435426
return false;
436427
}
428+
429+
stack.Push((fieldA.Value, fieldBValue));
437430
}
438431
}
439432
else if (!BySyntax.Equals(currentA, currentB))

src/HotChocolate/Core/test/Validation.Tests/FieldSelectionMergingRuleTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ fragment mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering o
119119
""");
120120
}
121121

122+
[Fact]
123+
public void ObjectValueWithNoFields()
124+
{
125+
ExpectValid(
126+
"""
127+
{
128+
findDog(complex: { }) {
129+
name
130+
}
131+
... mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering
132+
}
133+
134+
fragment mergeIdenticalFieldsWithIdenticalInputFieldValuesButDifferentOrdering on Query {
135+
findDog(complex: { }) {
136+
barks
137+
}
138+
}
139+
""");
140+
}
141+
122142
[Fact]
123143
public void ConflictingInputFieldValues()
124144
{

0 commit comments

Comments
 (0)