-
-
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 "RequireInvalidFieldsTypeRul…
…e" (#7888)
- Loading branch information
Showing
7 changed files
with
167 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
40 changes: 40 additions & 0 deletions
40
...ion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/RequireInvalidFieldsTypeRule.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,40 @@ | ||
using HotChocolate.Fusion.Events; | ||
using HotChocolate.Language; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
using static HotChocolate.Fusion.WellKnownArgumentNames; | ||
using static HotChocolate.Fusion.WellKnownDirectiveNames; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// When using the <c>@require</c> directive, the <c>fields</c> argument must always be a string | ||
/// that defines a (potentially nested) selection set of fields from the same type. If the | ||
/// <c>fields</c> argument is provided as a type other than a string (such as an integer, boolean, | ||
/// or enum), the directive usage is invalid and will cause schema composition to fail. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Require-Invalid-Fields-Type"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class RequireInvalidFieldsTypeRule : IEventHandler<FieldArgumentEvent> | ||
{ | ||
public void Handle(FieldArgumentEvent @event, CompositionContext context) | ||
{ | ||
var (argument, field, type, schema) = @event; | ||
|
||
var requireDirective = argument.Directives.FirstOrDefault(Require); | ||
|
||
if ( | ||
requireDirective is not null | ||
&& requireDirective.Arguments.TryGetValue(Fields, out var fields) | ||
&& fields is not StringValueNode) | ||
{ | ||
context.Log.Write( | ||
RequireInvalidFieldsType( | ||
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
95 changes: 95 additions & 0 deletions
95
...st/Fusion.Composition.Tests/PreMergeValidation/Rules/RequireInvalidFieldsTypeRuleTests.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,95 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class RequireInvalidFieldsTypeRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new RequireInvalidFieldsTypeRule()]); | ||
|
||
[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_FIELDS_TYPE")); | ||
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 | ||
// string 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[]> | ||
{ | ||
// Since "fields" is set to 123 (an integer) instead of a string, this violates the rule | ||
// and triggers a REQUIRE_INVALID_FIELDS_TYPE error. | ||
{ | ||
[ | ||
""" | ||
type User @key(fields: "id") { | ||
id: ID! | ||
profile(name: String! @require(fields: 123)): Profile | ||
} | ||
type Profile { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The @require directive on argument 'User.profile(name:)' in schema 'A' must " + | ||
"specify a string value for the 'fields' argument." | ||
] | ||
} | ||
}; | ||
} | ||
} |