-
-
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 "RootMutationUsedRule" (#7865)
- Loading branch information
Showing
10 changed files
with
163 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
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
31 changes: 31 additions & 0 deletions
31
...late/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/RootMutationUsedRule.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,31 @@ | ||
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 mutation type is defined, it must be | ||
/// named <c>Mutation</c>. Defining a root mutation type with a name other than <c>Mutation</c> or | ||
/// using a differently named type alongside a type explicitly named <c>Mutation</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-Mutation-Used"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class RootMutationUsedRule : IEventHandler<SchemaEvent> | ||
{ | ||
public void Handle(SchemaEvent @event, CompositionContext context) | ||
{ | ||
var schema = @event.Schema; | ||
var rootMutation = schema.MutationType; | ||
|
||
if (rootMutation is not null && rootMutation.Name != WellKnownTypeNames.Mutation) | ||
{ | ||
context.Log.Write(RootMutationUsed(schema)); | ||
} | ||
|
||
// An object type named 'Mutation' will be set as the root mutation type if it has not yet | ||
// been defined, so it's not necessary to check for this type in the absence of a root | ||
// mutation 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
6 changes: 6 additions & 0 deletions
6
src/HotChocolate/Fusion-vnext/src/Fusion.Composition/WellKnownTypeNames.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,6 @@ | ||
namespace HotChocolate.Fusion; | ||
|
||
internal static class WellKnownTypeNames | ||
{ | ||
public const string Mutation = "Mutation"; | ||
} |
97 changes: 97 additions & 0 deletions
97
...vnext/test/Fusion.Composition.Tests/PreMergeValidation/Rules/RootMutationUsedRuleTests.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 RootMutationUsedRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new RootMutationUsedRule()]); | ||
|
||
[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_MUTATION_USED")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// Valid example. | ||
{ | ||
[ | ||
""" | ||
schema { | ||
mutation: Mutation | ||
} | ||
type Mutation { | ||
createProduct(name: String): 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 `RootMutation` is used as the root | ||
// mutation type, but a type named `Mutation` is also defined. | ||
{ | ||
[ | ||
""" | ||
schema { | ||
mutation: RootMutation | ||
} | ||
type RootMutation { | ||
createProduct(name: String): Product | ||
} | ||
type Mutation { | ||
deprecatedField: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The root mutation type in schema 'A' must be named 'Mutation'." | ||
] | ||
} | ||
}; | ||
} | ||
} |