-
-
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 "QueryRootTypeInaccessibleRu…
…le" (#7886)
- Loading branch information
Showing
7 changed files
with
155 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
27 changes: 27 additions & 0 deletions
27
...on-vnext/src/Fusion.Composition/PreMergeValidation/Rules/QueryRootTypeInaccessibleRule.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,27 @@ | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// Every source schema that contributes to the final composite schema must expose a public | ||
/// (accessible) root query type. Marking the root query type as <c>@inaccessible</c> makes it | ||
/// invisible to the gateway, defeating its purpose as the primary entry point for queries and | ||
/// lookups. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Query-Root-Type-Inaccessible"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class QueryRootTypeInaccessibleRule : IEventHandler<SchemaEvent> | ||
{ | ||
public void Handle(SchemaEvent @event, CompositionContext context) | ||
{ | ||
var schema = @event.Schema; | ||
var rootQuery = schema.QueryType; | ||
|
||
if (rootQuery is not null && !ValidationHelper.IsAccessible(rootQuery)) | ||
{ | ||
context.Log.Write(QueryRootTypeInaccessible(rootQuery, 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/QueryRootTypeInaccessibleRuleTests.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 QueryRootTypeInaccessibleRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new QueryRootTypeInaccessibleRule()]); | ||
|
||
[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 == "QUERY_ROOT_TYPE_INACCESSIBLE")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, no @inaccessible annotation is applied to the query root, so the | ||
// rule is satisfied. | ||
{ | ||
[ | ||
""" | ||
extend schema { | ||
query: Query | ||
} | ||
type Query { | ||
allBooks: [Book] | ||
} | ||
type Book { | ||
id: ID! | ||
title: String | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// Since the schema marks the query root type as @inaccessible, the rule is violated. | ||
// QUERY_ROOT_TYPE_INACCESSIBLE is raised because a schema’s root query type cannot be | ||
// hidden from consumers. | ||
{ | ||
[ | ||
""" | ||
extend schema { | ||
query: Query | ||
} | ||
type Query @inaccessible { | ||
allBooks: [Book] | ||
} | ||
type Book { | ||
id: ID! | ||
title: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The root query type in schema 'A' must be accessible." | ||
] | ||
} | ||
}; | ||
} | ||
} |