Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fusion] Added pre-merge validation rule "RootQueryUsedRule" #7866

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public static class LogEntryCodes
public const string ExternalUnused = "EXTERNAL_UNUSED";
public const string OutputFieldTypesNotMergeable = "OUTPUT_FIELD_TYPES_NOT_MERGEABLE";
public const string RootMutationUsed = "ROOT_MUTATION_USED";
public const string RootQueryUsed = "ROOT_QUERY_USED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,14 @@ public static LogEntry RootMutationUsed(SchemaDefinition schema)
member: schema,
schema: schema);
}

public static LogEntry RootQueryUsed(SchemaDefinition schema)
{
return new LogEntry(
string.Format(LogEntryHelper_RootQueryUsed, schema.Name),
LogEntryCodes.RootQueryUsed,
severity: LogSeverity.Error,
member: schema,
schema: schema);
}
}
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.
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@
<data name="LogEntryHelper_RootMutationUsed" xml:space="preserve">
<value>The root mutation type in schema '{0}' must be named 'Mutation'.</value>
</data>
<data name="LogEntryHelper_RootQueryUsed" xml:space="preserve">
<value>The root query type in schema '{0}' must be named 'Query'.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private CompositionResult<SchemaDefinition> MergeSchemaDefinitions(CompositionCo
new ExternalMissingOnBaseRule(),
new ExternalUnusedRule(),
new OutputFieldTypesMergeableRule(),
new RootMutationUsedRule()
new RootMutationUsedRule(),
new RootQueryUsedRule()
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ namespace HotChocolate.Fusion;
internal static class WellKnownTypeNames
{
public const string Mutation = "Mutation";
public const string Query = "Query";
}
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'."
]
}
};
}
}
Loading