-
-
Notifications
You must be signed in to change notification settings - Fork 748
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 "KeyFieldsHasArgumentsRule" (#…
- Loading branch information
Showing
7 changed files
with
189 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
...Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/KeyFieldsHasArgumentsRule.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,33 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// The <c>@key</c> directive is used to define the set of fields that uniquely identify an entity. | ||
/// These fields must not include any field that is defined with arguments, as arguments introduce | ||
/// variability that prevents consistent and valid entity resolution across subgraphs. Fields | ||
/// included in the <c>fields</c> argument of the <c>@key</c> directive must be static and | ||
/// consistently resolvable. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Key-Fields-Has-Arguments"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class KeyFieldsHasArgumentsRule : IEventHandler<KeyFieldEvent> | ||
{ | ||
public void Handle(KeyFieldEvent @event, CompositionContext context) | ||
{ | ||
var (entityType, keyDirective, field, type, schema) = @event; | ||
|
||
if (field.Arguments.Count != 0) | ||
{ | ||
context.Log.Write( | ||
KeyFieldsHasArguments( | ||
entityType.Name, | ||
keyDirective, | ||
field.Name, | ||
type.Name, | ||
schema)); | ||
} | ||
} | ||
} |
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
122 changes: 122 additions & 0 deletions
122
.../test/Fusion.Composition.Tests/PreMergeValidation/Rules/KeyFieldsHasArgumentsRuleTests.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,122 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class KeyFieldsHasArgumentsRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new KeyFieldsHasArgumentsRule()]); | ||
|
||
[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 == "KEY_FIELDS_HAS_ARGS")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, the `User` type has a valid `@key` directive that references the | ||
// argument-free fields `id` and `name`. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id name") { | ||
id: ID! | ||
name: String | ||
tags: [String] | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In this example, the `@key` directive references a field (`tags`) that is defined | ||
// with arguments (`limit`), which is not allowed. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id tags") { | ||
id: ID! | ||
tags(limit: Int = 10): [String] | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'User' in schema 'A' references field " + | ||
"'User.tags', which must not have arguments." | ||
] | ||
}, | ||
// Nested field. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id info { tags }") { | ||
id: ID! | ||
info: UserInfo | ||
} | ||
type UserInfo { | ||
tags(limit: Int = 10): [String] | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'User' in schema 'A' references field " + | ||
"'UserInfo.tags', which must not have arguments." | ||
] | ||
}, | ||
// Multiple keys. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id") @key(fields: "tags") { | ||
id(global: Boolean = true): ID! | ||
tags(limit: Int = 10): [String] | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'User' in schema 'A' references field " + | ||
"'User.id', which must not have arguments.", | ||
|
||
"An @key directive on type 'User' in schema 'A' references field " + | ||
"'User.tags', which must not have arguments." | ||
] | ||
} | ||
}; | ||
} | ||
} |