Skip to content

Commit

Permalink
[Fusion] Added pre-merge validation rule "RootSubscriptionUsedRule"
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 committed Dec 24, 2024
1 parent 5e3b61e commit 9cf4a08
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public static class LogEntryCodes
public const string OutputFieldTypesNotMergeable = "OUTPUT_FIELD_TYPES_NOT_MERGEABLE";
public const string RootMutationUsed = "ROOT_MUTATION_USED";
public const string RootQueryUsed = "ROOT_QUERY_USED";
public const string RootSubscriptionUsed = "ROOT_SUBSCRIPTION_USED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,14 @@ public static LogEntry RootQueryUsed(SchemaDefinition schema)
member: schema,
schema: schema);
}

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

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

0 comments on commit 9cf4a08

Please sign in to comment.