-
-
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
ExternalMissingOnBaseRule
(#…
- Loading branch information
Showing
8 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Extensions/ImmutableArrayExtensions.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,21 @@ | ||
using System.Collections.Immutable; | ||
|
||
namespace HotChocolate.Fusion.Extensions; | ||
|
||
internal static class ImmutableArrayExtensions | ||
{ | ||
public static int Count<T>(this ImmutableArray<T> array, Predicate<T> predicate) | ||
{ | ||
var count = 0; | ||
|
||
foreach (var item in array) | ||
{ | ||
if (predicate(item)) | ||
{ | ||
count++; | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
...Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/ExternalMissingOnBaseRule.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,30 @@ | ||
using HotChocolate.Fusion.Events; | ||
using HotChocolate.Fusion.Extensions; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// This rule ensures that any field marked as <c>@external</c> in a source schema is actually | ||
/// defined (non-<c>@external</c>) in at least one other source schema. The <c>@external</c> | ||
/// directive is used to indicate that the field is not usually resolved by the source schema it is | ||
/// declared in, implying it should be resolvable by at least one other source schema. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-External-Missing-on-Base"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class ExternalMissingOnBaseRule : IEventHandler<OutputFieldGroupEvent> | ||
{ | ||
public void Handle(OutputFieldGroupEvent @event, CompositionContext context) | ||
{ | ||
var (fieldName, fieldGroup, typeName) = @event; | ||
|
||
var externalFieldCount = fieldGroup.Count(i => ValidationHelper.IsExternal(i.Field)); | ||
var nonExternalFieldCount = fieldGroup.Length - externalFieldCount; | ||
|
||
if (externalFieldCount != 0 && nonExternalFieldCount == 0) | ||
{ | ||
context.Log.Write(ExternalMissingOnBase(fieldName, typeName)); | ||
} | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
.../test/Fusion.Composition.Tests/PreMergeValidation/Rules/ExternalMissingOnBaseRuleTests.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,117 @@ | ||
using HotChocolate.Fusion; | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
using HotChocolate.Skimmed.Serialization; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class ExternalMissingOnBaseRuleTests | ||
{ | ||
[Test] | ||
[MethodDataSource(nameof(ValidExamplesData))] | ||
public async Task Examples_Valid(string[] sdl) | ||
{ | ||
// arrange | ||
var log = new CompositionLog(); | ||
var context = new CompositionContext([.. sdl.Select(SchemaParser.Parse)], log); | ||
var preMergeValidator = new PreMergeValidator([new ExternalMissingOnBaseRule()]); | ||
|
||
// act | ||
var result = preMergeValidator.Validate(context); | ||
|
||
// assert | ||
await Assert.That(result.IsSuccess).IsTrue(); | ||
await Assert.That(log.IsEmpty).IsTrue(); | ||
} | ||
|
||
[Test] | ||
[MethodDataSource(nameof(InvalidExamplesData))] | ||
public async Task Examples_Invalid(string[] sdl) | ||
{ | ||
// arrange | ||
var log = new CompositionLog(); | ||
var context = new CompositionContext([.. sdl.Select(SchemaParser.Parse)], log); | ||
var preMergeValidator = new PreMergeValidator([new ExternalMissingOnBaseRule()]); | ||
|
||
// act | ||
var result = preMergeValidator.Validate(context); | ||
|
||
// assert | ||
await Assert.That(result.IsFailure).IsTrue(); | ||
await Assert.That(log.Count()).IsEqualTo(1); | ||
await Assert.That(log.First().Code).IsEqualTo("EXTERNAL_MISSING_ON_BASE"); | ||
await Assert.That(log.First().Severity).IsEqualTo(LogSeverity.Error); | ||
} | ||
|
||
public static IEnumerable<Func<string[]>> ValidExamplesData() | ||
{ | ||
return | ||
[ | ||
// Here, the `name` field on Product is defined in source schema A and marked as | ||
// @external in source schema B, which is valid because there is a base definition in | ||
// source schema A. | ||
() => | ||
[ | ||
""" | ||
# Source schema A | ||
type Product { | ||
id: ID | ||
name: String | ||
} | ||
""", | ||
""" | ||
# Source schema B | ||
type Product { | ||
id: ID | ||
name: String @external | ||
} | ||
""" | ||
] | ||
]; | ||
} | ||
|
||
public static IEnumerable<Func<string[]>> InvalidExamplesData() | ||
{ | ||
return | ||
[ | ||
// In this example, the `name` field on Product is marked as @external in source schema | ||
// B but has no non-@external declaration in any other source schema, violating the | ||
// rule. | ||
() => | ||
[ | ||
""" | ||
# Source schema A | ||
type Product { | ||
id: ID | ||
} | ||
""", | ||
""" | ||
# Source schema B | ||
type Product { | ||
id: ID | ||
name: String @external | ||
} | ||
""" | ||
], | ||
// The `name` field is external in both source schemas. | ||
() => | ||
[ | ||
""" | ||
# Source schema A | ||
type Product { | ||
id: ID | ||
name: String @external | ||
} | ||
""", | ||
""" | ||
# Source schema B | ||
type Product { | ||
id: ID | ||
name: String @external | ||
} | ||
""" | ||
] | ||
]; | ||
} | ||
} |