-
-
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 "RequireInvalidSyntaxRule" (#…
- Loading branch information
Showing
9 changed files
with
169 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
29 changes: 29 additions & 0 deletions
29
.../Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/RequireInvalidSyntaxRule.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,29 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// The <c>@require</c> directive’s <c>fields</c> argument must be syntactically valid GraphQL. If | ||
/// the selection map string is malformed (e.g., missing closing braces, unbalanced quotes, invalid | ||
/// tokens), then the schema cannot be composed correctly. In such cases, the error | ||
/// <c>REQUIRE_INVALID_SYNTAX</c> is raised. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Require-Invalid-Syntax"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class RequireInvalidSyntaxRule : IEventHandler<RequireFieldsInvalidSyntaxEvent> | ||
{ | ||
public void Handle(RequireFieldsInvalidSyntaxEvent @event, CompositionContext context) | ||
{ | ||
var (requireDirective, argument, field, type, schema) = @event; | ||
|
||
context.Log.Write( | ||
RequireInvalidSyntax( | ||
requireDirective, | ||
argument.Name, | ||
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
93 changes: 93 additions & 0 deletions
93
...t/test/Fusion.Composition.Tests/PreMergeValidation/Rules/RequireInvalidSyntaxRuleTests.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,93 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class RequireInvalidSyntaxRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new RequireInvalidSyntaxRule()]); | ||
|
||
[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 == "REQUIRE_INVALID_SYNTAX")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In the following example, the @require directive’s "fields" argument is a valid | ||
// selection map and satisfies the rule. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id") { | ||
id: ID! | ||
profile(name: String! @require(fields: "name")): Profile | ||
} | ||
type Profile { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In the following example, the @require directive’s "fields" argument has invalid | ||
// syntax because it is missing a closing brace. | ||
{ | ||
[ | ||
""" | ||
type Book { | ||
id: ID! | ||
title(lang: String! @require(fields: "author { name ")): String | ||
} | ||
type Author { | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The @require directive on argument 'Book.title(lang:)' in schema 'A' " + | ||
"contains invalid syntax in the 'fields' argument." | ||
] | ||
} | ||
}; | ||
} | ||
} |