-
-
Notifications
You must be signed in to change notification settings - Fork 749
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 "RootSubscriptionUsedRule"
- Loading branch information
Showing
8 changed files
with
156 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
33 changes: 33 additions & 0 deletions
33
.../Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/RootSubscriptionUsedRule.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,33 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// This rule enforces that, for any source schema, if a root subscription type is defined, it must | ||
/// be named <c>Subscription</c>. Defining a root subscription type with a name other than | ||
/// <c>Subscription</c> or using a differently named type alongside a type explicitly named | ||
/// <c>Subscription</c> creates inconsistencies in schema design and violates the composite schema | ||
/// specification. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Root-Subscription-Used"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class RootSubscriptionUsedRule : IEventHandler<SchemaEvent> | ||
{ | ||
public void Handle(SchemaEvent @event, CompositionContext context) | ||
{ | ||
var schema = @event.Schema; | ||
var rootSubscription = schema.SubscriptionType; | ||
|
||
if (rootSubscription is not null | ||
&& rootSubscription.Name != WellKnownTypeNames.Subscription) | ||
{ | ||
context.Log.Write(RootSubscriptionUsed(schema)); | ||
} | ||
|
||
// An object type named 'Subscription' will be set as the root subscription type if it has | ||
// not yet been defined, so it's not necessary to check for this type in the absence of a | ||
// root subscription type. | ||
} | ||
} |
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
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
97 changes: 97 additions & 0 deletions
97
...t/test/Fusion.Composition.Tests/PreMergeValidation/Rules/RootSubscriptionUsedRuleTests.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,97 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class RootSubscriptionUsedRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new RootSubscriptionUsedRule()]); | ||
|
||
[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 == "ROOT_SUBSCRIPTION_USED")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// Valid example. | ||
{ | ||
[ | ||
""" | ||
schema { | ||
subscription: Subscription | ||
} | ||
type Subscription { | ||
productCreated: Product | ||
} | ||
type Product { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// The following example violates the rule because `RootSubscription` is used as the | ||
// root subscription type, but a type named `Subscription` is also defined. | ||
{ | ||
[ | ||
""" | ||
schema { | ||
subscription: RootSubscription | ||
} | ||
type RootSubscription { | ||
productCreated: Product | ||
} | ||
type Subscription { | ||
deprecatedField: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The root subscription type in schema 'A' must be named 'Subscription'." | ||
] | ||
} | ||
}; | ||
} | ||
} |