-
-
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 "KeyInvalidSyntaxRule"
- Loading branch information
Showing
9 changed files
with
155 additions
and
1 deletion.
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
24 changes: 24 additions & 0 deletions
24
...late/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/KeyInvalidSyntaxRule.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,24 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// Each <c>@key</c> directive must specify the fields that uniquely identify an entity using a | ||
/// valid GraphQL selection set in its <c>fields</c> argument. If the <c>fields</c> argument string | ||
/// is syntactically incorrect—missing closing braces, containing invalid tokens, or otherwise | ||
/// malformed—it cannot be composed into a valid schema and triggers the <c>KEY_INVALID_SYNTAX</c> | ||
/// error. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Key-Invalid-Syntax"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class KeyInvalidSyntaxRule : IEventHandler<KeyFieldsInvalidSyntaxEvent> | ||
{ | ||
public void Handle(KeyFieldsInvalidSyntaxEvent @event, CompositionContext context) | ||
{ | ||
var (entityType, keyDirective, schema) = @event; | ||
|
||
context.Log.Write(KeyInvalidSyntax(entityType.Name, keyDirective, 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
92 changes: 92 additions & 0 deletions
92
...vnext/test/Fusion.Composition.Tests/PreMergeValidation/Rules/KeyInvalidSyntaxRuleTests.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,92 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class KeyInvalidSyntaxRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new KeyInvalidSyntaxRule()]); | ||
|
||
[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_INVALID_SYNTAX")); | ||
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 is a correctly formed selection set: | ||
// "sku featuredItem { id }" is properly balanced and contains no syntax errors. | ||
{ | ||
[ | ||
""" | ||
type Product @key(fields: "sku featuredItem { id }") { | ||
sku: String! | ||
featuredItem: Node! | ||
} | ||
interface Node { | ||
id: ID! | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// Here, the selection set "featuredItem { id" is missing the closing brace "}". It is | ||
// thus invalid syntax, causing a "KEY_INVALID_SYNTAX" error. | ||
{ | ||
[ | ||
""" | ||
type Product @key(fields: "featuredItem { id") { | ||
featuredItem: Node! | ||
sku: String! | ||
} | ||
interface Node { | ||
id: ID! | ||
} | ||
""" | ||
], | ||
[ | ||
"A @key directive on type 'Product' in schema 'A' contains invalid syntax in " + | ||
"the 'fields' argument." | ||
] | ||
} | ||
}; | ||
} | ||
} |