-
-
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 "ProvidesFieldsMissingExtern…
…alRule" (#7885)
- Loading branch information
Showing
7 changed files
with
202 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
...next/src/Fusion.Composition/PreMergeValidation/Rules/ProvidesFieldsMissingExternalRule.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 static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// The <c>@provides</c> directive indicates that an object type field will supply additional fields | ||
/// belonging to the return type in this execution-specific path. Any field listed in the | ||
/// <c>@provides(fields: ...)</c> argument must therefore be <i>external</i> in the local schema, | ||
/// meaning that the local schema itself does <b>not</b> provide it. | ||
/// </para> | ||
/// <para> | ||
/// This rule disallows selecting non-external fields in a <c>@provides</c> selection set. If a | ||
/// field is already provided by the same schema in all execution paths, there is no need to | ||
/// <c>@provide</c>. | ||
/// </para> | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Provides-Fields-Missing-External"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class ProvidesFieldsMissingExternalRule : IEventHandler<ProvidesFieldEvent> | ||
{ | ||
public void Handle(ProvidesFieldEvent @event, CompositionContext context) | ||
{ | ||
var (providedField, providedType, providesDirective, field, type, schema) = @event; | ||
|
||
if (!ValidationHelper.IsExternal(providedField)) | ||
{ | ||
context.Log.Write( | ||
ProvidesFieldsMissingExternal( | ||
providedField.Name, | ||
providedType.Name, | ||
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
125 changes: 125 additions & 0 deletions
125
...sion.Composition.Tests/PreMergeValidation/Rules/ProvidesFieldsMissingExternalRuleTests.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,125 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class ProvidesFieldsMissingExternalRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new ProvidesFieldsMissingExternalRule()]); | ||
|
||
[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_FIELDS_MISSING_EXTERNAL")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// Here, the "Order" type from this schema is providing fields on "User" through | ||
// @provides. The "name" field of "User" is not defined in this schema; it is declared | ||
// with @external indicating that the "name" field comes from elsewhere. Thus, | ||
// referencing "name" under @provides(fields: "name") is valid. | ||
{ | ||
[ | ||
""" | ||
type Order { | ||
id: ID! | ||
customer: User @provides(fields: "name") | ||
} | ||
type User @key(fields: "id") { | ||
id: ID! | ||
name: String @external | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In this example, "User.address" is not marked as @external in the same schema that | ||
// applies @provides. This means the schema already provides the "address" field in all | ||
// possible paths, so using @provides(fields: "address") is invalid. | ||
{ | ||
[ | ||
""" | ||
type User { | ||
id: ID! | ||
address: String | ||
} | ||
type Order { | ||
id: ID! | ||
buyer: User @provides(fields: "address") | ||
} | ||
""" | ||
], | ||
[ | ||
"The @provides directive on field 'Order.buyer' in schema 'A' references " + | ||
"field 'User.address', which must be marked as external." | ||
] | ||
}, | ||
// Nested field. | ||
{ | ||
[ | ||
""" | ||
type User { | ||
id: ID! | ||
info: UserInfo | ||
} | ||
type UserInfo { | ||
address: String | ||
} | ||
type Order { | ||
id: ID! | ||
buyer: User @provides(fields: "info { address }") | ||
} | ||
""" | ||
], | ||
[ | ||
"The @provides directive on field 'Order.buyer' in schema 'A' references " + | ||
"field 'User.info', which must be marked as external.", | ||
|
||
"The @provides directive on field 'Order.buyer' in schema 'A' references " + | ||
"field 'UserInfo.address', which must be marked as external." | ||
] | ||
} | ||
}; | ||
} | ||
} |