-
-
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 "RootQueryUsedRule" (#7866)
- Loading branch information
Showing
8 changed files
with
154 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
31 changes: 31 additions & 0 deletions
31
...ocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/RootQueryUsedRule.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 the root query type in any source schema must be named <c>Query</c>. | ||
/// Defining a root query type with a name other than <c>Query</c> or using a differently named type | ||
/// alongside a type explicitly named <c>Query</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-Query-Used"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class RootQueryUsedRule : IEventHandler<SchemaEvent> | ||
{ | ||
public void Handle(SchemaEvent @event, CompositionContext context) | ||
{ | ||
var schema = @event.Schema; | ||
var rootQuery = schema.QueryType; | ||
|
||
if (rootQuery is not null && rootQuery.Name != WellKnownTypeNames.Query) | ||
{ | ||
context.Log.Write(RootQueryUsed(schema)); | ||
} | ||
|
||
// An object type named 'Query' will be set as the root query type if it has not yet been | ||
// defined, so it's not necessary to check for this type in the absence of a root query | ||
// 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
...on-vnext/test/Fusion.Composition.Tests/PreMergeValidation/Rules/RootQueryUsedRuleTests.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 RootQueryUsedRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new RootQueryUsedRule()]); | ||
|
||
[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_QUERY_USED")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// Valid example. | ||
{ | ||
[ | ||
""" | ||
schema { | ||
query: Query | ||
} | ||
type Query { | ||
product(id: ID!): 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 `RootQuery` is used as the root query | ||
// type, but a type named `Query` is also defined. | ||
{ | ||
[ | ||
""" | ||
schema { | ||
query: RootQuery | ||
} | ||
type RootQuery { | ||
product(id: ID!): Product | ||
} | ||
type Query { | ||
deprecatedField: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The root query type in schema 'A' must be named 'Query'." | ||
] | ||
} | ||
}; | ||
} | ||
} |