-
-
Notifications
You must be signed in to change notification settings - Fork 750
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 InputFieldsTypesMergeableRule
- Loading branch information
1 parent
7ecc977
commit 83ff50f
Showing
7 changed files
with
261 additions
and
124 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
46 changes: 46 additions & 0 deletions
46
...ion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/InputFieldTypesMergeableRule.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,46 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// The input fields of input objects with the same name must be mergeable. This rule ensures that | ||
/// input objects with the same name in different source schemas have fields that can be merged | ||
/// consistently without conflicts. | ||
/// </para> | ||
/// <para> | ||
/// Input fields are considered mergeable when they share the same name and have compatible types. | ||
/// The compatibility of types is determined by their structure (e.g., lists), excluding | ||
/// nullability. Mergeable input fields with different nullability are considered mergeable, | ||
/// and the resulting merged field will be the most permissive of the two. | ||
/// </para> | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Input-Field-Types-mergeable"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class InputFieldTypesMergeableRule : IEventHandler<InputFieldGroupEvent> | ||
{ | ||
public void Handle(InputFieldGroupEvent @event, CompositionContext context) | ||
{ | ||
var (_, fieldGroup, typeName) = @event; | ||
|
||
for (var i = 0; i < fieldGroup.Length - 1; i++) | ||
{ | ||
var fieldInfoA = fieldGroup[i]; | ||
var fieldInfoB = fieldGroup[i + 1]; | ||
var typeA = fieldInfoA.Field.Type; | ||
var typeB = fieldInfoB.Field.Type; | ||
|
||
if (!ValidationHelper.SameTypeShape(typeA, typeB)) | ||
{ | ||
context.Log.Write( | ||
InputFieldTypesNotMergeable( | ||
fieldInfoA.Field, | ||
typeName, | ||
fieldInfoA.Schema, | ||
fieldInfoB.Schema)); | ||
} | ||
} | ||
} | ||
} |
150 changes: 26 additions & 124 deletions
150
...Chocolate/Fusion-vnext/src/Fusion.Composition/Properties/CompositionResources.Designer.cs
Large diffs are not rendered by default.
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
163 changes: 163 additions & 0 deletions
163
...st/Fusion.Composition.Tests/PreMergeValidation/Rules/InputFieldTypesMergeableRuleTests.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,163 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class InputFieldTypesMergeableRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new InputFieldTypesMergeableRule()]); | ||
|
||
[Theory] | ||
[MemberData(nameof(ValidExamplesData))] | ||
public void Examples_Valid(string[] sdl) | ||
{ | ||
// arrange | ||
var context = CreateCompositionContext(sdl); | ||
|
||
// act | ||
var result = _preMergeValidator.Validate(context); | ||
|
||
// assert | ||
Assert.True(result.IsSuccess); | ||
Assert.True(context.Log.IsEmpty); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(InvalidExamplesData))] | ||
public void Examples_Invalid(string[] sdl, string[] errorMessages) | ||
{ | ||
// arrange | ||
var context = CreateCompositionContext(sdl); | ||
|
||
// act | ||
var result = _preMergeValidator.Validate(context); | ||
|
||
// assert | ||
Assert.True(result.IsFailure); | ||
Assert.Equal(errorMessages, context.Log.Select(e => e.Message).ToArray()); | ||
Assert.True(context.Log.All(e => e.Code == "INPUT_FIELD_TYPES_NOT_MERGEABLE")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In the following example, the field "name" in "AuthorInput" has compatible types | ||
// across source schemas, making them mergeable. | ||
{ | ||
[ | ||
""" | ||
# Schema A | ||
input AuthorInput { | ||
name: String! | ||
} | ||
""", | ||
""" | ||
# Schema B | ||
input AuthorInput { | ||
name: String! | ||
} | ||
""" | ||
] | ||
}, | ||
// The following example shows that fields are mergeable if they have different | ||
// nullability but the named type is the same and the list structure is the same. | ||
{ | ||
[ | ||
""" | ||
# Schema A | ||
input AuthorInput { | ||
tags: [String!] | ||
} | ||
""", | ||
""" | ||
# Schema B | ||
input AuthorInput { | ||
tags: [String]! | ||
} | ||
""", | ||
""" | ||
# Schema C | ||
input AuthorInput { | ||
tags: [String] | ||
} | ||
""" | ||
] | ||
}, | ||
// Multiple input fields. | ||
{ | ||
[ | ||
""" | ||
# Schema A | ||
input AuthorInput { | ||
name: String! | ||
tags: [String!] | ||
birthdate: DateTime | ||
} | ||
""", | ||
""" | ||
# Schema B | ||
input AuthorInput { | ||
name: String! | ||
tags: [String]! | ||
birthdate: DateTime | ||
} | ||
""" | ||
] | ||
}, | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In this example, the field "birthdate" on "AuthorInput" is not mergeable as the | ||
// field has different named types ("String" and "DateTime") across source schemas. | ||
{ | ||
[ | ||
""" | ||
# Schema A | ||
input AuthorInput { | ||
birthdate: String! | ||
} | ||
""", | ||
""" | ||
# Schema B | ||
input AuthorInput { | ||
birthdate: DateTime! | ||
} | ||
""" | ||
], | ||
[ | ||
"Input field 'AuthorInput.birthdate' has a different type shape in schema " + | ||
"'A' than it does in schema 'B'." | ||
] | ||
}, | ||
// List versus non-list | ||
{ | ||
[ | ||
""" | ||
# Schema A | ||
input AuthorInput { | ||
birthdate: String! | ||
} | ||
""", | ||
""" | ||
# Schema B | ||
input AuthorInput { | ||
birthdate: [String!] | ||
} | ||
""" | ||
], | ||
[ | ||
"Input field 'AuthorInput.birthdate' has a different type shape in schema " + | ||
"'A' than it does in schema 'B'." | ||
] | ||
} | ||
}; | ||
} | ||
} |