-
-
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 "OverrideFromSelfRule" (#7909)
- Loading branch information
Showing
9 changed files
with
160 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
33 changes: 33 additions & 0 deletions
33
...late/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/OverrideFromSelfRule.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,33 @@ | ||
using HotChocolate.Fusion.Events; | ||
using HotChocolate.Language; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
using static HotChocolate.Fusion.WellKnownArgumentNames; | ||
using static HotChocolate.Fusion.WellKnownDirectiveNames; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// When using <c>@override</c>, the <c>from</c> argument indicates the name of the source schema | ||
/// that originally owns the field. Overriding from the <b>same</b> schema creates a contradiction, | ||
/// as it implies both local and transferred ownership of the field within one schema. If the | ||
/// <c>from</c> value matches the local schema name, it triggers an <c>OVERRIDE_FROM_SELF</c> error. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Override-from-Self"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class OverrideFromSelfRule : IEventHandler<OutputFieldEvent> | ||
{ | ||
public void Handle(OutputFieldEvent @event, CompositionContext context) | ||
{ | ||
var (field, type, schema) = @event; | ||
|
||
var overrideDirective = field.Directives[Override].FirstOrDefault(); | ||
|
||
if ( | ||
overrideDirective?.Arguments[From] is StringValueNode from | ||
&& from.Value == schema.Name) | ||
{ | ||
context.Log.Write(OverrideFromSelf(overrideDirective, 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
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
94 changes: 94 additions & 0 deletions
94
...vnext/test/Fusion.Composition.Tests/PreMergeValidation/Rules/OverrideFromSelfRuleTests.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,94 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class OverrideFromSelfRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new OverrideFromSelfRule()]); | ||
|
||
[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 == "OVERRIDE_FROM_SELF")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In the following example, Schema B overrides the field "amount" from Schema A. The | ||
// two schema names are different, so no error is raised. | ||
{ | ||
[ | ||
""" | ||
# Source Schema A | ||
type Bill { | ||
id: ID! | ||
amount: Int | ||
} | ||
""", | ||
""" | ||
# Source Schema B | ||
type Bill { | ||
id: ID! | ||
amount: Int @override(from: "A") | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In the following example, the local schema is also "A", and the "from" argument is | ||
// "A". Overriding a field from the same schema is not allowed, causing an | ||
// OVERRIDE_FROM_SELF error. | ||
{ | ||
[ | ||
""" | ||
# Source Schema A (named "A") | ||
type Bill { | ||
id: ID! | ||
amount: Int @override(from: "A") | ||
} | ||
""" | ||
], | ||
[ | ||
"The @override directive on field 'Bill.amount' in schema 'A' must not " + | ||
"reference the same schema." | ||
] | ||
} | ||
}; | ||
} | ||
} |