Skip to content

Commit c4dcfea

Browse files
authored
[Fusion] Refactored rule "ExternalArgumentDefaultMismatchRule" (#8196)
1 parent 363907a commit c4dcfea

File tree

5 files changed

+64
-29
lines changed

5 files changed

+64
-29
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Logging/LogEntryHelper.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,29 @@ public static LogEntry EnumValuesMismatch(
203203
}
204204

205205
public static LogEntry ExternalArgumentDefaultMismatch(
206-
string argumentName,
206+
IValueNode? externalDefaultValue,
207+
MutableInputFieldDefinition externalArgument,
207208
string fieldName,
208-
string typeName)
209+
string typeName,
210+
MutableSchemaDefinition externalSchema,
211+
IValueNode? defaultValue,
212+
string schemaName)
209213
{
210-
var coordinate = new SchemaCoordinate(typeName, fieldName, argumentName);
214+
var coordinate = new SchemaCoordinate(typeName, fieldName, externalArgument.Name);
211215

212216
return new LogEntry(
213-
string.Format(LogEntryHelper_ExternalArgumentDefaultMismatch, coordinate),
217+
string.Format(
218+
LogEntryHelper_ExternalArgumentDefaultMismatch,
219+
externalDefaultValue is null ? "(null)" : externalDefaultValue.ToString(),
220+
coordinate,
221+
externalSchema.Name,
222+
defaultValue is null ? "(null)" : defaultValue.ToString(),
223+
schemaName),
214224
LogEntryCodes.ExternalArgumentDefaultMismatch,
215225
LogSeverity.Error,
216-
coordinate);
226+
coordinate,
227+
externalArgument,
228+
externalSchema);
217229
}
218230

219231
public static LogEntry ExternalMissingOnBase(

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidationRules/ExternalArgumentDefaultMismatchRule.cs

+31-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
using HotChocolate.Fusion.Events;
33
using HotChocolate.Fusion.Events.Contracts;
44
using HotChocolate.Fusion.Extensions;
5-
using HotChocolate.Language;
5+
using HotChocolate.Fusion.Info;
66
using static HotChocolate.Fusion.Logging.LogEntryHelper;
7+
using static HotChocolate.Language.SyntaxComparer;
78

89
namespace HotChocolate.Fusion.PreMergeValidationRules;
910

@@ -21,7 +22,10 @@ public void Handle(OutputFieldGroupEvent @event, CompositionContext context)
2122
{
2223
var (fieldName, fieldGroup, typeName) = @event;
2324

24-
if (!fieldGroup.Any(i => i.Field.HasExternalDirective()))
25+
var externalFieldGroup =
26+
fieldGroup.Where(i => i.Field.HasExternalDirective()).ToImmutableHashSet();
27+
28+
if (externalFieldGroup.IsEmpty)
2529
{
2630
return;
2731
}
@@ -32,18 +36,35 @@ public void Handle(OutputFieldGroupEvent @event, CompositionContext context)
3236

3337
foreach (var argumentName in argumentNames)
3438
{
35-
var arguments = fieldGroup
36-
.SelectMany(i => i.Field.Arguments.AsEnumerable().Where(a => a.Name == argumentName))
37-
.ToImmutableArray();
39+
var argumentGroup =
40+
fieldGroup
41+
.SelectMany(
42+
i => i.Field.Arguments.AsEnumerable().Where(a => a.Name == argumentName),
43+
(i, a) => new FieldArgumentInfo(a, i.Field, i.Type, i.Schema))
44+
.ToImmutableHashSet();
3845

39-
var defaultValue = arguments[0].DefaultValue;
46+
var externalArgumentGroup =
47+
externalFieldGroup
48+
.SelectMany(
49+
i => i.Field.Arguments.AsEnumerable().Where(a => a.Name == argumentName),
50+
(i, a) => new FieldArgumentInfo(a, i.Field, i.Type, i.Schema));
4051

41-
foreach (var argument in arguments)
52+
foreach (var (externalArgument, _, _, externalSchema) in externalArgumentGroup)
4253
{
43-
if (!SyntaxComparer.BySyntax.Equals(argument.DefaultValue, defaultValue))
54+
foreach (var (argument, _, _, schema) in argumentGroup)
4455
{
45-
context.Log.Write(
46-
ExternalArgumentDefaultMismatch(argumentName, fieldName, typeName));
56+
if (!BySyntax.Equals(argument.DefaultValue, externalArgument.DefaultValue))
57+
{
58+
context.Log.Write(
59+
ExternalArgumentDefaultMismatch(
60+
externalArgument.DefaultValue,
61+
externalArgument,
62+
fieldName,
63+
typeName,
64+
externalSchema,
65+
argument.DefaultValue,
66+
schema.Name));
67+
}
4768
}
4869
}
4970
}

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Properties/CompositionResources.Designer.cs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Properties/CompositionResources.resx

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
<value>The enum type '{0}' in schema '{1}' must define the value '{2}'.</value>
173173
</data>
174174
<data name="LogEntryHelper_ExternalArgumentDefaultMismatch" xml:space="preserve">
175-
<value>The argument with schema coordinate '{0}' has inconsistent default values.</value>
175+
<value>The default value '{0}' of external argument '{1}' in schema '{2}' differs from the default value of '{3}' in schema '{4}'.</value>
176176
</data>
177177
<data name="LogEntryHelper_ExternalMissingOnBase" xml:space="preserve">
178178
<value>The external field '{0}' in schema '{1}' is not defined (non-external) in any other schema.</value>

src/HotChocolate/Fusion-vnext/test/Fusion.Composition.Tests/PreMergeValidationRules/ExternalArgumentDefaultMismatchRuleTests.cs

+14-12
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ type Product {
109109
"""
110110
],
111111
[
112-
"The argument with schema coordinate 'Product.name(language:)' has " +
113-
"inconsistent default values."
112+
"The default value '\"de\"' of external argument 'Product.name(language:)' " +
113+
"in schema 'B' differs from the default value of '\"en\"' in schema 'A'."
114114
]
115115
},
116116
// In the following example, the "name" field on "Product" is defined in one source
@@ -132,12 +132,12 @@ type Product {
132132
"""
133133
],
134134
[
135-
"The argument with schema coordinate 'Product.name(language:)' has " +
136-
"inconsistent default values."
135+
"The default value '(null)' of external argument 'Product.name(language:)' " +
136+
"in schema 'B' differs from the default value of '\"en\"' in schema 'A'."
137137
]
138138
},
139-
// Here, the "name" field on "Product" is defined without a default value in the
140-
// non-external source schema, violating the rule.
139+
// Here, the "name" field on "Product" is defined without a default value for the
140+
// "language" argument in the non-external source schema, violating the rule.
141141
{
142142
[
143143
"""
@@ -152,8 +152,8 @@ type Product {
152152
"""
153153
],
154154
[
155-
"The argument with schema coordinate 'Product.name(language:)' has " +
156-
"inconsistent default values."
155+
"The default value '\"en\"' of external argument 'Product.name(language:)' " +
156+
"in schema 'B' differs from the default value of '(null)' in schema 'A'."
157157
]
158158
},
159159
// Here, the "name" field on "Product" is defined with multiple arguments. One argument
@@ -172,8 +172,9 @@ type Product {
172172
"""
173173
],
174174
[
175-
"The argument with schema coordinate 'Product.name(localization:)' has " +
176-
"inconsistent default values."
175+
"The default value '\"sa\"' of external argument " +
176+
"'Product.name(localization:)' in schema 'B' differs from the default value " +
177+
"of '\"sr\"' in schema 'A'."
177178
]
178179
},
179180
// Here, the "name" field on "Product" is defined with multiple arguments. One argument
@@ -193,8 +194,9 @@ type Product {
193194
"""
194195
],
195196
[
196-
"The argument with schema coordinate 'Product.name(localization:)' has " +
197-
"inconsistent default values."
197+
"The default value '(null)' of external argument " +
198+
"'Product.name(localization:)' in schema 'B' differs from the default value " +
199+
"of '\"sr\"' in schema 'A'."
198200
]
199201
}
200202
};

0 commit comments

Comments
 (0)