-
-
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.
Use node field for to fetch patches. (#6019)
- Loading branch information
1 parent
13349fa
commit 05469b0
Showing
59 changed files
with
923 additions
and
319 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
1 change: 1 addition & 0 deletions
1
src/HotChocolate/Core/src/Types/Types/Extensions/TypeExtensions.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
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
Binary file not shown.
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
164 changes: 164 additions & 0 deletions
164
src/HotChocolate/Fusion/src/Composition/Pipeline/Enrichers/NodeEntityEnricher.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,164 @@ | ||
using HotChocolate.Language; | ||
using HotChocolate.Skimmed; | ||
|
||
namespace HotChocolate.Fusion.Composition.Pipeline; | ||
|
||
internal class NodeEntityEnricher : IEntityEnricher | ||
{ | ||
private static readonly FieldNode _idField = | ||
new FieldNode( | ||
null, | ||
new NameNode("id"), | ||
null, | ||
null, | ||
Array.Empty<DirectiveNode>(), | ||
Array.Empty<ArgumentNode>(), | ||
null); | ||
|
||
private static readonly VariableDefinitionNode _idVariable = | ||
new VariableDefinitionNode( | ||
null, | ||
new VariableNode("var"), | ||
new NonNullTypeNode(new NamedTypeNode("ID")), | ||
null, | ||
Array.Empty<DirectiveNode>()); | ||
|
||
private static readonly VariableDefinitionNode _idsVariable = | ||
new VariableDefinitionNode( | ||
null, | ||
new VariableNode("var"), | ||
new NonNullTypeNode(new ListTypeNode(new NonNullTypeNode(new NamedTypeNode("ID")))), | ||
null, | ||
Array.Empty<DirectiveNode>()); | ||
|
||
public ValueTask EnrichAsync( | ||
CompositionContext context, | ||
EntityGroup entity, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
foreach (var (type, schema) in entity.Parts) | ||
{ | ||
if (schema.QueryType is not null && | ||
schema.QueryType.Fields.ContainsName("node") && | ||
schema.Types.TryGetType<InterfaceType>("Node", out var nodeType) && | ||
type.Implements.Contains(nodeType)) | ||
{ | ||
ResolveWithNode(entity, schema, type, entity.Name); | ||
|
||
if (schema.QueryType.Fields.ContainsName("nodes")) | ||
{ | ||
ResolveWithNodes(entity, schema, type, entity.Name); | ||
} | ||
} | ||
} | ||
return default; | ||
} | ||
|
||
private static void ResolveWithNode( | ||
EntityGroup entity, | ||
Schema sourceSchema, | ||
ObjectType sourceType, | ||
string targetName) | ||
{ | ||
var arguments = new List<ArgumentNode>(); | ||
|
||
var spread = new FragmentSpreadNode( | ||
null, | ||
new NameNode(targetName), | ||
Array.Empty<DirectiveNode>()); | ||
|
||
var inlineFragment = new InlineFragmentNode( | ||
null, | ||
new NamedTypeNode(sourceType.Name), | ||
Array.Empty<DirectiveNode>(), | ||
new SelectionSetNode(new[] { spread })); | ||
|
||
// Create a new FieldNode for the entity resolver | ||
var selection = new FieldNode( | ||
null, | ||
new NameNode("node"), | ||
null, | ||
null, | ||
Array.Empty<DirectiveNode>(), | ||
arguments, | ||
new SelectionSetNode(new[] { inlineFragment })); | ||
|
||
// Create a new SelectionSetNode for the entity resolver | ||
var selectionSet = new SelectionSetNode(new[] { selection }); | ||
|
||
// Create a new EntityResolver for the entity | ||
var resolver = new EntityResolver( | ||
EntityResolverKind.Single, | ||
selectionSet, | ||
sourceType.Name, | ||
sourceSchema.Name); | ||
|
||
var var = sourceType.CreateVariableName(new SchemaCoordinate(targetName, "id")); | ||
var varNode = new VariableNode(var); | ||
arguments.Add(new ArgumentNode("id", varNode)); | ||
|
||
resolver.Variables.Add( | ||
var, | ||
new VariableDefinition( | ||
var, | ||
_idField, | ||
_idVariable.WithVariable(varNode))); | ||
|
||
// Add the new EntityResolver to the entity metadata | ||
entity.Metadata.EntityResolvers.Add(resolver); | ||
} | ||
|
||
private static void ResolveWithNodes( | ||
EntityGroup entity, | ||
Schema sourceSchema, | ||
ObjectType sourceType, | ||
string targetName) | ||
{ | ||
var arguments = new List<ArgumentNode>(); | ||
|
||
var spread = new FragmentSpreadNode( | ||
null, | ||
new NameNode(targetName), | ||
Array.Empty<DirectiveNode>()); | ||
|
||
var inlineFragment = new InlineFragmentNode( | ||
null, | ||
new NamedTypeNode(sourceType.Name), | ||
Array.Empty<DirectiveNode>(), | ||
new SelectionSetNode(new[] { spread })); | ||
|
||
// Create a new FieldNode for the entity resolver | ||
var selection = new FieldNode( | ||
null, | ||
new NameNode("nodes"), | ||
null, | ||
null, | ||
Array.Empty<DirectiveNode>(), | ||
arguments, | ||
new SelectionSetNode(new[] { inlineFragment })); | ||
|
||
// Create a new SelectionSetNode for the entity resolver | ||
var selectionSet = new SelectionSetNode(new[] { selection }); | ||
|
||
// Create a new EntityResolver for the entity | ||
var resolver = new EntityResolver( | ||
EntityResolverKind.BatchWithKey, | ||
selectionSet, | ||
sourceType.Name, | ||
sourceSchema.Name); | ||
|
||
var var = sourceType.CreateVariableName(new SchemaCoordinate(targetName, "id")); | ||
var varNode = new VariableNode(var); | ||
arguments.Add(new ArgumentNode("ids", varNode)); | ||
|
||
resolver.Variables.Add( | ||
var, | ||
new VariableDefinition( | ||
var, | ||
_idField, | ||
_idsVariable.WithVariable(varNode))); | ||
|
||
// Add the new EntityResolver to the entity metadata | ||
entity.Metadata.EntityResolvers.Add(resolver); | ||
} | ||
} |
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
Oops, something went wrong.