-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
NodeFieldTypeInterceptor.cs
112 lines (100 loc) · 3.86 KB
/
NodeFieldTypeInterceptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#nullable enable
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Types.Introspection;
using HotChocolate.Utilities;
using Microsoft.Extensions.DependencyInjection;
using static HotChocolate.Properties.TypeResources;
using static HotChocolate.Types.Relay.NodeConstants;
using static HotChocolate.Types.Relay.NodeFieldResolvers;
namespace HotChocolate.Types.Relay;
/// <summary>
/// This type interceptor adds the fields `node` and the `nodes` to the query type.
/// </summary>
internal sealed class NodeFieldTypeInterceptor : TypeInterceptor
{
public override void OnBeforeCompleteType(
ITypeCompletionContext completionContext,
DefinitionBase? definition,
IDictionary<string, object?> contextData)
{
if ((completionContext.IsQueryType ?? false) &&
definition is ObjectTypeDefinition objectTypeDefinition)
{
var typeInspector = completionContext.TypeInspector;
var serializer =
completionContext.Services.GetService<IIdSerializer>() ??
new IdSerializer();
// the nodes fields shall be chained in after the introspection fields,
// so we first get the index of the last introspection field,
// which is __typename
var typeNameField = objectTypeDefinition.Fields.First(
t => t.Name.EqualsOrdinal(IntrospectionFields.TypeName) &&
t.IsIntrospectionField);
var index = objectTypeDefinition.Fields.IndexOf(typeNameField);
CreateNodeField(typeInspector, serializer, objectTypeDefinition.Fields, index + 1);
CreateNodesField(typeInspector, serializer, objectTypeDefinition.Fields, index + 2);
}
}
private static void CreateNodeField(
ITypeInspector typeInspector,
IIdSerializer serializer,
IList<ObjectFieldDefinition> fields,
int index)
{
var node = typeInspector.GetTypeRef(typeof(NodeType));
var id = typeInspector.GetTypeRef(typeof(NonNullType<IdType>));
var field = new ObjectFieldDefinition(
Node,
Relay_NodeField_Description,
node)
{
Arguments =
{
new ArgumentDefinition(Id, Relay_NodeField_Id_Description, id)
},
MiddlewareDefinitions =
{
new FieldMiddlewareDefinition(
next => async context =>
{
await ResolveSingleNodeAsync(context, serializer).ConfigureAwait(false);
await next(context).ConfigureAwait(false);
})
}
};
fields.Insert(index, field);
}
private static void CreateNodesField(
ITypeInspector typeInspector,
IIdSerializer serializer,
IList<ObjectFieldDefinition> fields,
int index)
{
var nodes = typeInspector.GetTypeRef(typeof(NonNullType<ListType<NodeType>>));
var ids = typeInspector.GetTypeRef(typeof(NonNullType<ListType<NonNullType<IdType>>>));
var field = new ObjectFieldDefinition(
Nodes,
Relay_NodesField_Description,
nodes)
{
Arguments =
{
new ArgumentDefinition(Ids, Relay_NodesField_Ids_Description, ids)
},
MiddlewareDefinitions =
{
new FieldMiddlewareDefinition(
next => async context =>
{
await ResolveManyNodeAsync(context, serializer).ConfigureAwait(false);
await next(context).ConfigureAwait(false);
})
}
};
fields.Insert(index, field);
}
}