-
-
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 "KeyInvalidFieldsRule" (#7874)
- Loading branch information
Showing
9 changed files
with
257 additions
and
14 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
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
...late/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/KeyInvalidFieldsRule.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 static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// Even if the selection set for <c>@key(fields: "…")</c> is syntactically valid, field references | ||
/// within that selection set must also refer to <b>actual</b> fields on the annotated type. This | ||
/// includes nested selections, which must appear on the corresponding return type. If any | ||
/// referenced field is missing or incorrectly named, composition fails with a | ||
/// <c>KEY_INVALID_FIELDS</c> error because the entity key cannot be resolved correctly. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Key-Invalid-Fields"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class KeyInvalidFieldsRule : IEventHandler<KeyFieldsInvalidReferenceEvent> | ||
{ | ||
public void Handle(KeyFieldsInvalidReferenceEvent @event, CompositionContext context) | ||
{ | ||
var (entityType, keyDirective, fieldNode, type, schema) = @event; | ||
|
||
context.Log.Write( | ||
KeyInvalidFields( | ||
entityType.Name, | ||
keyDirective, | ||
fieldNode.Name.Value, | ||
type.Name, | ||
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
154 changes: 154 additions & 0 deletions
154
...vnext/test/Fusion.Composition.Tests/PreMergeValidation/Rules/KeyInvalidFieldsRuleTests.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,154 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class KeyInvalidFieldsRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new KeyInvalidFieldsRule()]); | ||
|
||
[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 == "KEY_INVALID_FIELDS")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, the `fields` argument of the `@key` directive is properly defined | ||
// with valid syntax and references existing fields. | ||
{ | ||
[ | ||
""" | ||
type Product @key(fields: "sku featuredItem { id }") { | ||
sku: String! | ||
featuredItem: Node! | ||
} | ||
interface Node { | ||
id: ID! | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// In this example, the `fields` argument of the `@key` directive references a field | ||
// `id`, which does not exist on the `Product` type. | ||
{ | ||
[ | ||
""" | ||
type Product @key(fields: "id") { | ||
sku: String! | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'Product' in schema 'A' references field " + | ||
"'Product.id', which does not exist." | ||
] | ||
}, | ||
// Nested field. | ||
{ | ||
[ | ||
""" | ||
type Product @key(fields: "info { category { id } }") { | ||
info: ProductInfo! | ||
} | ||
type ProductInfo { | ||
subcategory: Category | ||
} | ||
type Category { | ||
name: String! | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'Product' in schema 'A' references field " + | ||
"'ProductInfo.category', which does not exist." | ||
] | ||
}, | ||
// Multiple nested fields. | ||
{ | ||
[ | ||
""" | ||
type Product @key(fields: "category { id name } info { id }") { | ||
category: ProductCategory! | ||
} | ||
type ProductCategory { | ||
description: String | ||
} | ||
type ProductInfo { | ||
updatedAt: DateTime! | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'Product' in schema 'A' references field " + | ||
"'ProductCategory.id', which does not exist.", | ||
|
||
"An @key directive on type 'Product' in schema 'A' references field " + | ||
"'ProductCategory.name', which does not exist.", | ||
|
||
"An @key directive on type 'Product' in schema 'A' references field " + | ||
"'Product.info', which does not exist." | ||
] | ||
}, | ||
// Multiple keys. | ||
{ | ||
[ | ||
""" | ||
type Product @key(fields: "id") @key(fields: "name") { | ||
sku: String! | ||
} | ||
""" | ||
], | ||
[ | ||
"An @key directive on type 'Product' in schema 'A' references field " + | ||
"'Product.id', which does not exist.", | ||
|
||
"An @key directive on type 'Product' in schema 'A' references field " + | ||
"'Product.name', which does not exist." | ||
] | ||
} | ||
}; | ||
} | ||
} |