Skip to content

Commit 7e117b9

Browse files
authored
[Fusion] Ignore descriptions when comparing with canonical directives (#8883)
1 parent 0c13b41 commit 7e117b9

File tree

6 files changed

+71
-37
lines changed

6 files changed

+71
-37
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/SourceSchemaMerger.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,13 @@ private void MergeDirectiveDefinitions(MutableSchemaDefinition mergedSchema)
103103
}
104104

105105
var canonicalDirectiveDefinition = directiveMerger.GetCanonicalDirectiveDefinition(mergedSchema);
106-
107-
// Ensure that all directive definitions match the canonical definition.
108106
var canonicalDirectiveNode = canonicalDirectiveDefinition.ToSyntaxNode();
109107

110-
// We want to ignore the descriptions when comparing directives.
111-
// TODO: Long-term we should do this differently
112-
var canonicalDirectiveNodeWithoutDescription = canonicalDirectiveNode.WithDescription(null);
113-
108+
// Ensure that all directive definitions match the canonical definition.
114109
if (!grouping.All(
115110
d => d.DirectiveDefinition
116111
.ToSyntaxNode()
117-
.WithDescription(null)
118-
.Equals(canonicalDirectiveNodeWithoutDescription, SyntaxComparison.Syntax)))
112+
.Equals(canonicalDirectiveNode, SyntaxComparison.SyntaxIgnoreDescriptions)))
119113
{
120114
// Skip merging if there is a mismatch.
121115
continue;

src/HotChocolate/Fusion-vnext/test/Fusion.Composition.Tests/SourceSchemaMerger.TagDirective.Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ input FooInput @tag(name: "a") {
6767
field: Int @tag(name: "a")
6868
}
6969
70-
directive @tag(name: String!) repeatable on {{s_tagLocations}}
70+
directive @tag("Some description" name: String!) repeatable on {{s_tagLocations}}
7171
""",
7272
$$"""
7373
# Schema B
@@ -99,7 +99,7 @@ input FooInput @tag(name: "b") {
9999
field: Int @tag(name: "b")
100100
}
101101
102-
directive @tag(name: String!) repeatable on {{s_tagLocations}}
102+
directive @tag("Some description" name: String!) repeatable on {{s_tagLocations}}
103103
"""
104104
],
105105
$$"""

src/HotChocolate/Language/src/Language.SyntaxTree/Extensions/SyntaxComparison.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ namespace HotChocolate.Language;
33
public enum SyntaxComparison
44
{
55
Reference = 0,
6-
Syntax = 1
6+
Syntax = 1,
7+
SyntaxIgnoreDescriptions = 2
78
}

src/HotChocolate/Language/src/Language.SyntaxTree/Extensions/SyntaxNodeExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ public static bool Equals(
107107
this ISyntaxNode node,
108108
ISyntaxNode? other,
109109
SyntaxComparison comparison)
110-
=> comparison is SyntaxComparison.Syntax
111-
? SyntaxComparer.BySyntax.Equals(node, other)
112-
: SyntaxComparer.ByReference.Equals(node, other);
110+
=> comparison switch
111+
{
112+
SyntaxComparison.Reference => SyntaxComparer.ByReference.Equals(node, other),
113+
SyntaxComparison.Syntax => SyntaxComparer.BySyntax.Equals(node, other),
114+
SyntaxComparison.SyntaxIgnoreDescriptions => SyntaxComparer.BySyntaxIgnoreDescriptions.Equals(node, other),
115+
_ => throw new ArgumentOutOfRangeException(nameof(comparison), comparison, null)
116+
};
113117

114118
public static string ToString(this ISyntaxNode node, SyntaxSerializerOptions options)
115119
{

src/HotChocolate/Language/src/Language.SyntaxTree/SyntaxComparer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ public static class SyntaxComparer
55
public static IEqualityComparer<ISyntaxNode> BySyntax { get; }
66
= new SyntaxEqualityComparer();
77

8+
public static IEqualityComparer<ISyntaxNode> BySyntaxIgnoreDescriptions { get; }
9+
= new SyntaxEqualityComparer(ignoreDescriptions: true);
10+
811
public static IEqualityComparer<ISyntaxNode> ByReference { get; }
912
= new DefaultSyntaxEqualityComparer();
1013

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

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace HotChocolate.Language;
22

3-
internal sealed class SyntaxEqualityComparer : IEqualityComparer<ISyntaxNode>
3+
internal sealed class SyntaxEqualityComparer(bool ignoreDescriptions = false) : IEqualityComparer<ISyntaxNode>
44
{
55
public bool Equals(ISyntaxNode? x, ISyntaxNode? y)
66
{
@@ -171,7 +171,7 @@ private bool Equals(BooleanValueNode x, BooleanValueNode y)
171171

172172
private bool Equals(DirectiveDefinitionNode x, DirectiveDefinitionNode y)
173173
=> Equals(x.Name, y.Name)
174-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
174+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
175175
&& x.IsRepeatable.Equals(y.IsRepeatable)
176176
&& Equals(x.Arguments, y.Arguments)
177177
&& Equals(x.Locations, y.Locations);
@@ -184,7 +184,7 @@ private bool Equals(DocumentNode x, DocumentNode y)
184184

185185
private bool Equals(EnumTypeDefinitionNode x, EnumTypeDefinitionNode y)
186186
=> Equals(x.Name, y.Name)
187-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
187+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
188188
&& Equals(x.Directives, y.Directives)
189189
&& Equals(x.Values, y.Values);
190190

@@ -195,14 +195,14 @@ private bool Equals(EnumTypeExtensionNode x, EnumTypeExtensionNode y)
195195

196196
private bool Equals(EnumValueDefinitionNode x, EnumValueDefinitionNode y)
197197
=> Equals(x.Name, y.Name)
198-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description);
198+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description));
199199

200200
private bool Equals(EnumValueNode x, EnumValueNode y)
201201
=> x.AsSpan().SequenceEqual(y.AsSpan());
202202

203203
private bool Equals(FieldDefinitionNode x, FieldDefinitionNode y)
204204
=> Equals(x.Name, y.Name)
205-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
205+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
206206
&& Equals(x.Directives, y.Directives)
207207
&& Equals(x.Arguments, y.Arguments)
208208
&& Equals(x.Type, y.Type);
@@ -235,7 +235,7 @@ private bool Equals(InlineFragmentNode x, InlineFragmentNode y)
235235

236236
private bool Equals(InputObjectTypeDefinitionNode x, InputObjectTypeDefinitionNode y)
237237
=> Equals(x.Name, y.Name)
238-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
238+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
239239
&& Equals(x.Directives, y.Directives)
240240
&& Equals(x.Fields, y.Fields);
241241

@@ -246,14 +246,14 @@ private bool Equals(InputObjectTypeExtensionNode x, InputObjectTypeExtensionNode
246246

247247
private bool Equals(InputValueDefinitionNode x, InputValueDefinitionNode y)
248248
=> Equals(x.Name, y.Name)
249-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
249+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
250250
&& Equals(x.Type, y.Type)
251251
&& Equals(x.DefaultValue, y.DefaultValue)
252252
&& Equals(x.Directives, y.Directives);
253253

254254
private bool Equals(InterfaceTypeDefinitionNode x, InterfaceTypeDefinitionNode y)
255255
=> Equals(x.Name, y.Name)
256-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
256+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
257257
&& Equals(x.Directives, y.Directives)
258258
&& Equals(x.Interfaces, y.Interfaces)
259259
&& Equals(x.Fields, y.Fields);
@@ -308,7 +308,7 @@ private bool Equals(ObjectFieldNode x, ObjectFieldNode y)
308308

309309
private bool Equals(ObjectTypeDefinitionNode x, ObjectTypeDefinitionNode y)
310310
=> Equals(x.Name, y.Name)
311-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
311+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
312312
&& Equals(x.Directives, y.Directives)
313313
&& Equals(x.Interfaces, y.Interfaces)
314314
&& Equals(x.Fields, y.Fields);
@@ -335,7 +335,7 @@ private bool Equals(OperationTypeDefinitionNode x, OperationTypeDefinitionNode y
335335

336336
private bool Equals(ScalarTypeDefinitionNode x, ScalarTypeDefinitionNode y)
337337
=> Equals(x.Name, y.Name)
338-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
338+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
339339
&& Equals(x.Directives, y.Directives);
340340

341341
private bool Equals(ScalarTypeExtensionNode x, ScalarTypeExtensionNode y)
@@ -349,7 +349,7 @@ private bool Equals(SchemaCoordinateNode x, SchemaCoordinateNode y)
349349
&& SyntaxComparer.BySyntax.Equals(x.ArgumentName, y.ArgumentName);
350350

351351
private bool Equals(SchemaDefinitionNode x, SchemaDefinitionNode y)
352-
=> SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
352+
=> (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
353353
&& Equals(x.Directives, y.Directives)
354354
&& Equals(x.OperationTypes, y.OperationTypes);
355355

@@ -365,7 +365,7 @@ private bool Equals(StringValueNode x, StringValueNode y)
365365

366366
private bool Equals(UnionTypeDefinitionNode x, UnionTypeDefinitionNode y)
367367
=> Equals(x.Name, y.Name)
368-
&& SyntaxComparer.BySyntax.Equals(x.Description, y.Description)
368+
&& (ignoreDescriptions || SyntaxComparer.BySyntax.Equals(x.Description, y.Description))
369369
&& Equals(x.Directives, y.Directives)
370370
&& Equals(x.Types, y.Types);
371371

@@ -545,7 +545,10 @@ private int GetHashCode(DirectiveDefinitionNode node)
545545
var hashCode = new HashCode();
546546
hashCode.Add(node.Kind);
547547
hashCode.Add(GetHashCode(node.Name));
548-
hashCode.Add(GetHashCode(node.Description));
548+
if (!ignoreDescriptions)
549+
{
550+
hashCode.Add(GetHashCode(node.Description));
551+
}
549552
hashCode.Add(node.IsRepeatable);
550553

551554
for (var i = 0; i < node.Arguments.Count; i++)
@@ -597,7 +600,10 @@ private int GetHashCode(EnumTypeDefinitionNode node)
597600
var hashCode = new HashCode();
598601
hashCode.Add(node.Kind);
599602
hashCode.Add(GetHashCode(node.Name));
600-
hashCode.Add(GetHashCode(node.Description));
603+
if (!ignoreDescriptions)
604+
{
605+
hashCode.Add(GetHashCode(node.Description));
606+
}
601607

602608
for (var i = 0; i < node.Directives.Count; i++)
603609
{
@@ -636,7 +642,9 @@ private int GetHashCode(EnumTypeExtensionNode node)
636642
}
637643

638644
private int GetHashCode(EnumValueDefinitionNode node)
639-
=> HashCode.Combine(node.Kind, GetHashCode(node.Name), GetHashCode(node.Description));
645+
=> ignoreDescriptions
646+
? HashCode.Combine(node.Kind, GetHashCode(node.Name))
647+
: HashCode.Combine(node.Kind, GetHashCode(node.Name), GetHashCode(node.Description));
640648

641649
private int GetHashCode(EnumValueNode node)
642650
=> HashCode.Combine(node.Kind, node.Value);
@@ -646,7 +654,10 @@ private int GetHashCode(FieldDefinitionNode node)
646654
var hashCode = new HashCode();
647655
hashCode.Add(node.Kind);
648656
hashCode.Add(GetHashCode(node.Name));
649-
hashCode.Add(GetHashCode(node.Description));
657+
if (!ignoreDescriptions)
658+
{
659+
hashCode.Add(GetHashCode(node.Description));
660+
}
650661

651662
for (var i = 0; i < node.Directives.Count; i++)
652663
{
@@ -765,7 +776,10 @@ private int GetHashCode(InputObjectTypeDefinitionNode node)
765776
var hashCode = new HashCode();
766777
hashCode.Add(node.Kind);
767778
hashCode.Add(GetHashCode(node.Name));
768-
hashCode.Add(GetHashCode(node.Description));
779+
if (!ignoreDescriptions)
780+
{
781+
hashCode.Add(GetHashCode(node.Description));
782+
}
769783

770784
for (var i = 0; i < node.Directives.Count; i++)
771785
{
@@ -808,7 +822,10 @@ private int GetHashCode(InputValueDefinitionNode node)
808822
var hashCode = new HashCode();
809823
hashCode.Add(node.Kind);
810824
hashCode.Add(GetHashCode(node.Name));
811-
hashCode.Add(GetHashCode(node.Description));
825+
if (!ignoreDescriptions)
826+
{
827+
hashCode.Add(GetHashCode(node.Description));
828+
}
812829
hashCode.Add(GetHashCode(node.Type));
813830
hashCode.Add(GetHashCode(node.DefaultValue));
814831

@@ -826,7 +843,10 @@ private int GetHashCode(InterfaceTypeDefinitionNode node)
826843
var hashCode = new HashCode();
827844
hashCode.Add(node.Kind);
828845
hashCode.Add(GetHashCode(node.Name));
829-
hashCode.Add(GetHashCode(node.Description));
846+
if (!ignoreDescriptions)
847+
{
848+
hashCode.Add(GetHashCode(node.Description));
849+
}
830850

831851
for (var i = 0; i < node.Directives.Count; i++)
832852
{
@@ -947,7 +967,10 @@ private int GetHashCode(ObjectTypeDefinitionNode node)
947967
var hashCode = new HashCode();
948968
hashCode.Add(node.Kind);
949969
hashCode.Add(GetHashCode(node.Name));
950-
hashCode.Add(GetHashCode(node.Description));
970+
if (!ignoreDescriptions)
971+
{
972+
hashCode.Add(GetHashCode(node.Description));
973+
}
951974

952975
for (var i = 0; i < node.Directives.Count; i++)
953976
{
@@ -1043,7 +1066,10 @@ private int GetHashCode(ScalarTypeDefinitionNode node)
10431066
var hashCode = new HashCode();
10441067
hashCode.Add(node.Kind);
10451068
hashCode.Add(GetHashCode(node.Name));
1046-
hashCode.Add(GetHashCode(node.Description));
1069+
if (!ignoreDescriptions)
1070+
{
1071+
hashCode.Add(GetHashCode(node.Description));
1072+
}
10471073

10481074
for (var i = 0; i < node.Directives.Count; i++)
10491075
{
@@ -1081,7 +1107,10 @@ private int GetHashCode(SchemaDefinitionNode node)
10811107
{
10821108
var hashCode = new HashCode();
10831109
hashCode.Add(node.Kind);
1084-
hashCode.Add(GetHashCode(node.Description));
1110+
if (!ignoreDescriptions)
1111+
{
1112+
hashCode.Add(GetHashCode(node.Description));
1113+
}
10851114

10861115
for (var i = 0; i < node.Directives.Count; i++)
10871116
{
@@ -1158,7 +1187,10 @@ private int GetHashCode(UnionTypeDefinitionNode node)
11581187
var hashCode = new HashCode();
11591188
hashCode.Add(node.Kind);
11601189
hashCode.Add(GetHashCode(node.Name));
1161-
hashCode.Add(GetHashCode(node.Description));
1190+
if (!ignoreDescriptions)
1191+
{
1192+
hashCode.Add(GetHashCode(node.Description));
1193+
}
11621194

11631195
for (var i = 0; i < node.Directives.Count; i++)
11641196
{

0 commit comments

Comments
 (0)