-
-
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 "KeyDirectiveInFieldsArgumen…
…tRule" (#7872)
- Loading branch information
Showing
9 changed files
with
218 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
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
30 changes: 30 additions & 0 deletions
30
...vnext/src/Fusion.Composition/PreMergeValidation/Rules/KeyDirectiveInFieldsArgumentRule.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,30 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// The <c>@key</c> directive specifies the set of fields used to uniquely identify an entity. The | ||
/// <c>fields</c> argument must consist of a valid GraphQL selection set that does not include any | ||
/// directive applications. Directives in the <c>fields</c> argument are not supported. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Key-Directive-in-Fields-Argument"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class KeyDirectiveInFieldsArgumentRule : IEventHandler<KeyFieldNodeEvent> | ||
{ | ||
public void Handle(KeyFieldNodeEvent @event, CompositionContext context) | ||
{ | ||
var (entityType, keyDirective, fieldNode, fieldNamePath, schema) = @event; | ||
|
||
if (fieldNode.Directives.Count != 0) | ||
{ | ||
context.Log.Write( | ||
KeyDirectiveInFieldsArgument( | ||
entityType.Name, | ||
keyDirective, | ||
fieldNamePath, | ||
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
130 changes: 130 additions & 0 deletions
130
...usion.Composition.Tests/PreMergeValidation/Rules/KeyDirectiveInFieldsArgumentRuleTests.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,130 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class KeyDirectiveInFieldsArgumentRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new KeyDirectiveInFieldsArgumentRule()]); | ||
|
||
[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_DIRECTIVE_IN_FIELDS_ARG")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, the `fields` argument of the `@key` directive does not include any | ||
// directive applications, satisfying the rule. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id name") { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In this example, the `fields` argument of the `@key` directive includes a directive | ||
// application `@lowercase`, which is not allowed. | ||
{ | ||
[ | ||
""" | ||
directive @lowercase on FIELD_DEFINITION | ||
type User @key(fields: "id name @lowercase") { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'User' in schema 'A' references field 'name', " + | ||
"which must not include directive applications." | ||
] | ||
}, | ||
// In this example, the `fields` argument includes a directive application `@lowercase` | ||
// nested inside the selection set, which is also invalid. | ||
{ | ||
[ | ||
""" | ||
directive @lowercase on FIELD_DEFINITION | ||
type User @key(fields: "id name { firstName @lowercase }") { | ||
id: ID! | ||
name: FullName | ||
} | ||
type FullName { | ||
firstName: String | ||
lastName: String | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'User' in schema 'A' references field " + | ||
"'name.firstName', which must not include directive applications." | ||
] | ||
}, | ||
// Multiple keys. | ||
{ | ||
[ | ||
""" | ||
directive @example on FIELD_DEFINITION | ||
type User @key(fields: "id @example") @key(fields: "name @example") { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'User' in schema 'A' references field 'id', " + | ||
"which must not include directive applications.", | ||
|
||
"An @key directive on type 'User' in schema 'A' references field 'name', " + | ||
"which must not include directive applications." | ||
] | ||
} | ||
}; | ||
} | ||
} |