Skip to content

Commit 0c78645

Browse files
Move implementation to SyntaxEqualityComparer
1 parent 029d029 commit 0c78645

File tree

2 files changed

+36
-57
lines changed

2 files changed

+36
-57
lines changed

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

Lines changed: 1 addition & 56 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 (AreValuesIdentical(argumentA.Value, argumentB.Value))
373+
if (BySyntax.Equals(argumentA.Value, argumentB.Value))
374374
{
375375
validPairs++;
376376
}
@@ -383,61 +383,6 @@ private static bool AreArgumentsIdentical(FieldNode fieldA, FieldNode fieldB)
383383
return fieldA.Arguments.Count == validPairs;
384384
}
385385

386-
private static bool AreValuesIdentical(IValueNode valueA, IValueNode valueB)
387-
{
388-
var stack = new Stack<(IValueNode ValueA, IValueNode ValueB)>();
389-
stack.Push((valueA, valueB));
390-
391-
while (stack.Count > 0)
392-
{
393-
var (currentA, currentB) = stack.Pop();
394-
395-
if (currentA is ListValueNode listA && currentB is ListValueNode listB)
396-
{
397-
if (listA.Items.Count != listB.Items.Count)
398-
{
399-
return false;
400-
}
401-
402-
for (var i = 0; i < listA.Items.Count; i++)
403-
{
404-
stack.Push((listA.Items[i], listB.Items[i]));
405-
}
406-
}
407-
else if (currentA is ObjectValueNode objectA && currentB is ObjectValueNode objectB)
408-
{
409-
if (objectA.Fields.Count != objectB.Fields.Count)
410-
{
411-
return false;
412-
}
413-
414-
if (objectA.Fields.Count == 0)
415-
{
416-
continue;
417-
}
418-
419-
var fieldDictB = objectB.Fields
420-
.ToDictionary(f => f.Name, f => f.Value, BySyntax);
421-
422-
foreach (var fieldA in objectA.Fields)
423-
{
424-
if (!fieldDictB.TryGetValue(fieldA.Name, out var fieldBValue))
425-
{
426-
return false;
427-
}
428-
429-
stack.Push((fieldA.Value, fieldBValue));
430-
}
431-
}
432-
else if (!BySyntax.Equals(currentA, currentB))
433-
{
434-
return false;
435-
}
436-
}
437-
438-
return true;
439-
}
440-
441386
private static bool SameResponseShape(IType typeA, IType typeB)
442387
{
443388
while (!typeA.IsNamedType() && !typeB.IsNamedType())

src/HotChocolate/Language/src/Language.SyntaxTree/SyntaxEqualityComparer.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,41 @@ private bool Equals(ObjectTypeExtensionNode x, ObjectTypeExtensionNode y)
357357
Equals(x.Fields, y.Fields);
358358

359359
private bool Equals(ObjectValueNode x, ObjectValueNode y)
360-
=> Equals(x.Fields, y.Fields);
360+
{
361+
if (x.Fields.Count != y.Fields.Count)
362+
{
363+
return false;
364+
}
365+
366+
if (x.Fields.Count == 0)
367+
{
368+
return true;
369+
}
370+
371+
for (var i = 0; i < x.Fields.Count; i++)
372+
{
373+
var xField = x.Fields[i];
374+
ObjectFieldNode? matchingField = null;
375+
376+
for (var j = 0; j < y.Fields.Count; j++)
377+
{
378+
var yField = y.Fields[j];
379+
380+
if (Equals(xField.Name, yField.Name))
381+
{
382+
matchingField = yField;
383+
break;
384+
}
385+
}
386+
387+
if (matchingField is null || !Equals(xField.Value, matchingField.Value))
388+
{
389+
return false;
390+
}
391+
}
392+
393+
return true;
394+
}
361395

362396
private bool Equals(OperationDefinitionNode x, OperationDefinitionNode y)
363397
=> SyntaxComparer.BySyntax.Equals(x.Name, y.Name) &&

0 commit comments

Comments
 (0)