-
-
Notifications
You must be signed in to change notification settings - Fork 748
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 "LookupMustNotReturnListRule" (
- Loading branch information
1 parent
0f469a5
commit 7df44ba
Showing
7 changed files
with
169 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
27 changes: 27 additions & 0 deletions
27
...sion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/LookupMustNotReturnListRule.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,27 @@ | ||
using HotChocolate.Fusion.Events; | ||
using HotChocolate.Skimmed; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// Fields annotated with the <c>@lookup</c> directive are intended to retrieve a single entity | ||
/// based on provided arguments. To avoid ambiguity in entity resolution, such fields must return a | ||
/// single object and not a list. This validation rule enforces that any field annotated with | ||
/// <c>@lookup</c> must have a return type that is <b>NOT</b> a list. | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec--lookup-must-not-return-a-list"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class LookupMustNotReturnListRule : IEventHandler<OutputFieldEvent> | ||
{ | ||
public void Handle(OutputFieldEvent @event, CompositionContext context) | ||
{ | ||
var (field, type, schema) = @event; | ||
|
||
if (ValidationHelper.IsLookup(field) && field.Type.NullableType() is ListTypeDefinition) | ||
{ | ||
context.Log.Write(LookupMustNotReturnList(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
109 changes: 109 additions & 0 deletions
109
...est/Fusion.Composition.Tests/PreMergeValidation/Rules/LookupMustNotReturnListRuleTests.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,109 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class LookupMustNotReturnListRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = | ||
new([new LookupMustNotReturnListRule()]); | ||
|
||
[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 == "LOOKUP_MUST_NOT_RETURN_LIST")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, "userById" returns a "User" object, satisfying the requirement. | ||
{ | ||
[ | ||
""" | ||
type Query { | ||
userById(id: ID!): User @lookup | ||
} | ||
type User { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// Here, "usersByIds" returns a list of "User" objects, which violates the requirement | ||
// that a @lookup field must return a single object. | ||
{ | ||
[ | ||
""" | ||
type Query { | ||
usersByIds(ids: [ID!]!): [User!] @lookup | ||
} | ||
type User { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The lookup field 'Query.usersByIds' in schema 'A' must not return a list." | ||
] | ||
}, | ||
// Non-null list. | ||
{ | ||
[ | ||
""" | ||
type Query { | ||
usersByIds(ids: [ID!]!): [User!]! @lookup | ||
} | ||
type User { | ||
id: ID! | ||
name: String | ||
} | ||
""" | ||
], | ||
[ | ||
"The lookup field 'Query.usersByIds' in schema 'A' must not return a list." | ||
] | ||
} | ||
}; | ||
} | ||
} |