-
-
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.
Fixed infer arguments when using lookup. (#7552)
- Loading branch information
1 parent
f72f117
commit f2a93cb
Showing
6 changed files
with
206 additions
and
15 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
62 changes: 62 additions & 0 deletions
62
src/HotChocolate/Fusion/src/Composition/Pipeline/Enrichers/LookupEntityEnricher.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,62 @@ | ||
using System.Runtime.CompilerServices; | ||
using HotChocolate.Language; | ||
using HotChocolate.Skimmed; | ||
using HotChocolate.Types; | ||
|
||
namespace HotChocolate.Fusion.Composition.Pipeline; | ||
|
||
/// <summary> | ||
/// A pipeline enricher that processes entity groups and adds entity resolvers to | ||
/// metadata for all arguments that contain the @ref directive. | ||
/// </summary> | ||
internal sealed class LookupEntityEnricher : IEntityEnricher | ||
{ | ||
/// <inheritdoc /> | ||
public ValueTask EnrichAsync( | ||
CompositionContext context, | ||
EntityGroup entity, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
foreach (var (type, schema) in entity.Parts) | ||
{ | ||
// Check if the schema has a query type | ||
if (schema.QueryType is null) | ||
{ | ||
continue; | ||
} | ||
|
||
if (!schema.DirectiveDefinitions.TryGetDirective("is", out var isDirective)) | ||
{ | ||
if(!schema.Types.TryGetType(BuiltIns.String.Name, out var stringType)) | ||
{ | ||
stringType = BuiltIns.String.Create(); | ||
schema.Types.Add(stringType); | ||
} | ||
|
||
isDirective = new DirectiveDefinition("is"); | ||
isDirective.Arguments.Add(new InputFieldDefinition("field", stringType)); | ||
schema.DirectiveDefinitions.Add(isDirective); | ||
} | ||
|
||
// Loop through each query field | ||
foreach (var entityResolverField in schema.QueryType.Fields) | ||
{ | ||
if (entityResolverField.Directives.ContainsName("lookup")) | ||
{ | ||
foreach (var argument in entityResolverField.Arguments) | ||
{ | ||
if (!argument.ContainsIsDirective()) | ||
{ | ||
argument.Directives.Add( | ||
new Directive( | ||
isDirective, | ||
new ArgumentAssignment("field", argument.Name))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...ion.Tests/__snapshots__/DemoIntegrationTests.User_Field_Is_Fully_Specified_Lookup.graphql
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,24 @@ | ||
schema | ||
@fusion(version: 1) | ||
@transport(subgraph: "Schema1", group: "Schema1", location: "http:\/\/localhost:5000\/graphql", kind: "HTTP") { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
user(id: Int!): User | ||
@variable(subgraph: "Schema1", name: "id", argument: "id") | ||
@resolver(subgraph: "Schema1", select: "{ user(id: $id) }", arguments: [ { name: "id", type: "Int!" } ]) | ||
} | ||
|
||
type User | ||
@variable(subgraph: "Schema1", name: "User_id", select: "id") | ||
@resolver(subgraph: "Schema1", select: "{ user(id: $User_id) }", arguments: [ { name: "User_id", type: "Int!" } ]) { | ||
email: String! | ||
@source(subgraph: "Schema1") | ||
id: Int! | ||
@source(subgraph: "Schema1") | ||
name: String! | ||
@source(subgraph: "Schema1") | ||
password: String! | ||
@source(subgraph: "Schema1") | ||
} |
24 changes: 24 additions & 0 deletions
24
...n.Tests/__snapshots__/DemoIntegrationTests.User_Field__Lookup_Infers_Is_Directive.graphql
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,24 @@ | ||
schema | ||
@fusion(version: 1) | ||
@transport(subgraph: "Schema1", group: "Schema1", location: "http:\/\/localhost:5000\/graphql", kind: "HTTP") { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
user(id: Int!): User | ||
@variable(subgraph: "Schema1", name: "id", argument: "id") | ||
@resolver(subgraph: "Schema1", select: "{ user(id: $id) }", arguments: [ { name: "id", type: "Int!" } ]) | ||
} | ||
|
||
type User | ||
@variable(subgraph: "Schema1", name: "User_id", select: "id") | ||
@resolver(subgraph: "Schema1", select: "{ user(id: $User_id) }", arguments: [ { name: "User_id", type: "Int!" } ]) { | ||
email: String! | ||
@source(subgraph: "Schema1") | ||
id: Int! | ||
@source(subgraph: "Schema1") | ||
name: String! | ||
@source(subgraph: "Schema1") | ||
password: String! | ||
@source(subgraph: "Schema1") | ||
} |
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