-
-
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 "ExternalUnusedRule" (#7864)
- Loading branch information
Showing
10 changed files
with
225 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
39 changes: 39 additions & 0 deletions
39
...colate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/ExternalUnusedRule.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,39 @@ | ||
using System.Collections.Immutable; | ||
using HotChocolate.Fusion.Events; | ||
using HotChocolate.Skimmed; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// This rule ensures that every field marked as <c>@external</c> in a source schema is actually | ||
/// used by that source schema in a <c>@provides</c> directive. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-External-Unused"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class ExternalUnusedRule : IEventHandler<OutputFieldEvent> | ||
{ | ||
public void Handle(OutputFieldEvent @event, CompositionContext context) | ||
{ | ||
var (field, type, schema) = @event; | ||
|
||
if (ValidationHelper.IsExternal(field)) | ||
{ | ||
var referencingFields = | ||
schema.Types | ||
.OfType<ComplexTypeDefinition>() | ||
.SelectMany(t => t.Fields) | ||
.Where(f => f.Type == type) | ||
.ToImmutableArray(); | ||
|
||
var isReferenced = | ||
referencingFields.Any(f => ValidationHelper.ProvidesFieldName(f, field.Name)); | ||
|
||
if (!isReferenced) | ||
{ | ||
context.Log.Write(ExternalUnused(field, type, 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
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/WellKnownArgumentNames.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 WellKnownArgumentNames | ||
{ | ||
public const string Fields = "fields"; | ||
} |
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
126 changes: 126 additions & 0 deletions
126
...n-vnext/test/Fusion.Composition.Tests/PreMergeValidation/Rules/ExternalUnusedRuleTests.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,126 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class ExternalUnusedRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new ExternalUnusedRule()]); | ||
|
||
[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 == "EXTERNAL_UNUSED")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, the `name` field is marked with @external and is used by the | ||
// @provides directive, satisfying the rule. | ||
{ | ||
[ | ||
""" | ||
# Source schema A | ||
type Product { | ||
id: ID | ||
name: String @external | ||
} | ||
type Query { | ||
productByName(name: String): Product @provides(fields: "name") | ||
} | ||
""" | ||
] | ||
}, | ||
// Provides two fields. | ||
{ | ||
[ | ||
""" | ||
# Source schema A | ||
type Product { | ||
id: ID | ||
name: String @external | ||
} | ||
type Query { | ||
productByName(name: String): Product @provides(fields: "id name") | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In this example, the `name` field is marked with @external but is not used by the | ||
// @provides directive, violating the rule. | ||
{ | ||
[ | ||
""" | ||
# Source schema A | ||
type Product { | ||
title: String @external | ||
author: Author | ||
} | ||
""" | ||
], | ||
[ | ||
"External field 'Product.title' in schema 'A' is not referenced by an " + | ||
"@provides directive in the schema." | ||
] | ||
}, | ||
// Provides different field. | ||
{ | ||
[ | ||
""" | ||
# Source schema A | ||
type Product { | ||
title: String @external | ||
author: Author | ||
} | ||
type Query { | ||
productByName(name: String): Product @provides(fields: "author") | ||
} | ||
""" | ||
], | ||
[ | ||
"External field 'Product.title' in schema 'A' is not referenced by an " + | ||
"@provides directive in the schema." | ||
] | ||
} | ||
}; | ||
} | ||
} |