-
-
Notifications
You must be signed in to change notification settings - Fork 754
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 "ProvidesInvalidFieldsTypeRu…
…le" (#7914)
- Loading branch information
Showing
9 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
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
25 changes: 25 additions & 0 deletions
25
...on-vnext/src/Fusion.Composition/PreMergeValidation/Rules/ProvidesInvalidFieldsTypeRule.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,25 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// The <c>@provides</c> directive indicates that a field is <b>providing</b> one or more additional | ||
/// fields on the returned (child) type. The <c>fields</c> argument accepts a <b>string</b> | ||
/// representing a GraphQL selection set (for example, <c>"title author"</c>). If the <c>fields</c> | ||
/// argument is given as a non-string type (e.g., <c>Boolean</c>, <c>Int</c>, <c>Array</c>), the | ||
/// schema fails to compose because it cannot interpret a valid selection set. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Provides-Invalid-Fields-Type"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class ProvidesInvalidFieldsTypeRule : IEventHandler<ProvidesFieldsInvalidTypeEvent> | ||
{ | ||
public void Handle(ProvidesFieldsInvalidTypeEvent @event, CompositionContext context) | ||
{ | ||
var (providesDirective, field, type, schema) = @event; | ||
|
||
context.Log.Write( | ||
ProvidesInvalidFieldsType(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
101 changes: 101 additions & 0 deletions
101
...t/Fusion.Composition.Tests/PreMergeValidation/Rules/ProvidesInvalidFieldsTypeRuleTests.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,101 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class ProvidesInvalidFieldsTypeRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new ProvidesInvalidFieldsTypeRule()]); | ||
|
||
[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_INVALID_FIELDS_TYPE")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, the @provides directive on "details" uses the string | ||
// "features specifications" to specify that both fields are provided in the child type | ||
// "ProductDetails". | ||
{ | ||
[ | ||
""" | ||
type Product { | ||
id: ID! | ||
details: ProductDetails @provides(fields: "features specifications") | ||
} | ||
type ProductDetails { | ||
features: [String] | ||
specifications: String | ||
} | ||
type Query { | ||
products: [Product] | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// Here, the @provides directive includes a numeric value (123) instead of a string in | ||
// its "fields" argument. This invalid usage raises a PROVIDES_INVALID_FIELDS_TYPE | ||
// error. | ||
{ | ||
[ | ||
""" | ||
type Product { | ||
id: ID! | ||
details: ProductDetails @provides(fields: 123) | ||
} | ||
type ProductDetails { | ||
features: [String] | ||
specifications: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The @provides directive on field 'Product.details' in schema 'A' must " + | ||
"specify a string value for the 'fields' argument." | ||
] | ||
} | ||
}; | ||
} | ||
} |