-
-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fusion] Added pre-merge validation rule "InputFieldDefaultMismatchRu…
…le" (#7891) Co-authored-by: Glen <glen.84@gmail.com>
- Loading branch information
1 parent
6ec7760
commit a8c9ac6
Showing
13 changed files
with
388 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...hocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Info/FieldArgumentInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using HotChocolate.Skimmed; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Info; | ||
|
||
internal record FieldArgumentInfo( | ||
InputFieldDefinition Argument, | ||
OutputFieldDefinition Field, | ||
INamedTypeDefinition Type, | ||
SchemaDefinition Schema); |
8 changes: 8 additions & 0 deletions
8
...otChocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Info/InputFieldInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using HotChocolate.Skimmed; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Info; | ||
|
||
internal record InputFieldInfo( | ||
InputFieldDefinition Field, | ||
INamedTypeDefinition Type, | ||
SchemaDefinition Schema); |
8 changes: 8 additions & 0 deletions
8
...tChocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Info/OutputFieldInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using HotChocolate.Skimmed; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Info; | ||
|
||
internal record OutputFieldInfo( | ||
OutputFieldDefinition Field, | ||
INamedTypeDefinition Type, | ||
SchemaDefinition Schema); |
5 changes: 5 additions & 0 deletions
5
src/HotChocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Info/TypeInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using HotChocolate.Skimmed; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Info; | ||
|
||
internal record TypeInfo(INamedTypeDefinition Type, SchemaDefinition Schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...on-vnext/src/Fusion.Composition/PreMergeValidation/Rules/InputFieldDefaultMismatchRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Collections.Immutable; | ||
using HotChocolate.Fusion.Events; | ||
using HotChocolate.Language; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Input fields in different source schemas that have the same name are required to have consistent | ||
/// default values. This ensures that there is no ambiguity or inconsistency when merging input | ||
/// fields from different source schemas. | ||
/// </para> | ||
/// <para> | ||
/// A mismatch in default values for input fields with the same name across different source schemas | ||
/// will result in a schema composition error. | ||
/// </para> | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Input-Field-Default-Mismatch"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class InputFieldDefaultMismatchRule : IEventHandler<InputFieldGroupEvent> | ||
{ | ||
public void Handle(InputFieldGroupEvent @event, CompositionContext context) | ||
{ | ||
var (_, fieldGroup, typeName) = @event; | ||
|
||
var fieldGroupWithDefaultValues = fieldGroup | ||
.Where(i => i.Field.DefaultValue is not null) | ||
.ToImmutableArray(); | ||
|
||
var defaultValues = fieldGroupWithDefaultValues | ||
.Select(i => i.Field.DefaultValue!) | ||
.ToImmutableHashSet(SyntaxComparer.BySyntax); | ||
|
||
if (defaultValues.Count <= 1) | ||
{ | ||
return; | ||
} | ||
|
||
for (var i = 0; i < fieldGroupWithDefaultValues.Length - 1; i++) | ||
{ | ||
var (fieldA, _, schemaA) = fieldGroupWithDefaultValues[i]; | ||
var (fieldB, _, schemaB) = fieldGroupWithDefaultValues[i + 1]; | ||
var defaultValueA = fieldA.DefaultValue!; | ||
var defaultValueB = fieldB.DefaultValue!; | ||
|
||
if (!SyntaxComparer.BySyntax.Equals(defaultValueA, defaultValueB)) | ||
{ | ||
context.Log.Write( | ||
InputFieldDefaultMismatch( | ||
defaultValueA, | ||
defaultValueB, | ||
fieldA, | ||
typeName, | ||
schemaA, | ||
schemaB)); | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...Chocolate/Fusion-vnext/src/Fusion.Composition/Properties/CompositionResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.