-
-
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 "ProvidesDirectiveInFieldsAr…
…gumentRule"
- Loading branch information
Showing
9 changed files
with
305 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
32 changes: 32 additions & 0 deletions
32
.../src/Fusion.Composition/PreMergeValidation/Rules/ProvidesDirectiveInFieldsArgumentRule.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,32 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// The <c>@provides</c> directive is used to specify the set of fields on an object type that a | ||
/// resolver provides for the parent type. The <c>fields</c> argument must consist of a valid | ||
/// GraphQL selection set without any directive applications, as directives within the <c>fields</c> | ||
/// argument are not supported. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Provides-Directive-in-Fields-Argument"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class ProvidesDirectiveInFieldsArgumentRule : IEventHandler<ProvidesFieldNodeEvent> | ||
{ | ||
public void Handle(ProvidesFieldNodeEvent @event, CompositionContext context) | ||
{ | ||
var (fieldNode, fieldNamePath, providesDirective, field, type, schema) = @event; | ||
|
||
if (fieldNode.Directives.Count != 0) | ||
{ | ||
context.Log.Write( | ||
ProvidesDirectiveInFieldsArgument( | ||
fieldNamePath, | ||
providesDirective, | ||
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
147 changes: 147 additions & 0 deletions
147
....Composition.Tests/PreMergeValidation/Rules/ProvidesDirectiveInFieldsArgumentRuleTests.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,147 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class ProvidesDirectiveInFieldsArgumentRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new ProvidesDirectiveInFieldsArgumentRule()]); | ||
|
||
[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 == "PROVIDES_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 @provides directive does not have any | ||
// directive applications, satisfying the rule. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id name") { | ||
id: ID! | ||
name: String | ||
profile: Profile @provides(fields: "name") | ||
} | ||
type Profile { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In this example, the "fields" argument of the @provides directive has a directive | ||
// application @lowercase, which is not allowed. | ||
{ | ||
[ | ||
""" | ||
directive @lowercase on FIELD_DEFINITION | ||
type User @key(fields: "id name") { | ||
id: ID! | ||
name: String | ||
profile: Profile @provides(fields: "name @lowercase") | ||
} | ||
type Profile { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The @provides directive on field 'User.profile' in schema 'A' references " + | ||
"field 'name', which must not include directive applications." | ||
] | ||
}, | ||
// Nested field. | ||
{ | ||
[ | ||
""" | ||
directive @lowercase on FIELD_DEFINITION | ||
type User @key(fields: "id name") { | ||
id: ID! | ||
name: String | ||
profile: Profile @provides(fields: "info { name @lowercase }") | ||
} | ||
type Profile { | ||
id: ID! | ||
info: ProfileInfo! | ||
} | ||
type ProfileInfo { | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The @provides directive on field 'User.profile' in schema 'A' references " + | ||
"field 'info.name', which must not include directive applications." | ||
] | ||
}, | ||
// Multiple fields. | ||
{ | ||
[ | ||
""" | ||
directive @example on FIELD_DEFINITION | ||
type User @key(fields: "id name") { | ||
id: ID! | ||
name: String | ||
profile: Profile @provides(fields: "id @example name @example") | ||
} | ||
""" | ||
], | ||
[ | ||
"The @provides directive on field 'User.profile' in schema 'A' references " + | ||
"field 'id', which must not include directive applications.", | ||
|
||
"The @provides directive on field 'User.profile' in schema 'A' references " + | ||
"field 'name', which must not include directive applications." | ||
] | ||
} | ||
}; | ||
} | ||
} |