-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
ApplyComplexTypeExtension.cs
198 lines (171 loc) · 6.58 KB
/
ApplyComplexTypeExtension.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Execution;
using HotChocolate.Language;
namespace HotChocolate.Stitching.Types.Pipeline.ApplyExtensions;
internal abstract class ApplyComplexTypeExtension<TDef, TExt>
: ApplyExtension<TDef, TExt>
where TDef : ComplexTypeDefinitionNodeBase, ITypeDefinitionNode
where TExt : ComplexTypeDefinitionNodeBase, ITypeExtensionNode
{
protected override TDef Apply(TDef definition, TExt extension)
{
IReadOnlyList<DirectiveNode> directives = definition.Directives;
IReadOnlyList<NamedTypeNode> interfaces = definition.Interfaces;
IReadOnlyList<FieldDefinitionNode> fields = definition.Fields;
if (extension.Directives.Count > 0)
{
var temp = definition.Directives.ToList();
temp.AddRange(extension.Directives);
directives = temp;
}
if (extension.Interfaces.Count > 0)
{
var names = definition.Interfaces.Select(t => t.Name.Value).ToHashSet();
List<NamedTypeNode>? temp = null;
foreach (NamedTypeNode type in extension.Interfaces)
{
if (names.Add(type.Name.Value))
{
(temp ?? definition.Interfaces.ToList()).Add(type);
}
}
if (temp is not null)
{
interfaces = temp;
}
}
if (extension.Fields.Count > 0)
{
var map = new OrderedDictionary<string, FieldDefinitionNode>(
definition.Fields.Select(t => new KeyValuePair<string, FieldDefinitionNode>(
t.Name.Value,
t)));
var touched = false;
foreach (FieldDefinitionNode extensionField in extension.Fields)
{
// By default extensions would only allow to insert new fields.
// We also use extensions as a vehicle to annotate fields.
// So if we see that there is already a field we will try to merge it.
if (map.TryGetValue(extensionField.Name.Value, out FieldDefinitionNode? field))
{
FieldDefinitionNode temp =
ApplyExtensions(definition.Name.Value, field, extensionField);
if (!touched)
{
touched = !ReferenceEquals(temp, field);
}
map[extensionField.Name.Value] = temp;
}
else
{
map.Add(extensionField.Name.Value, extensionField);
touched = true;
}
}
fields = map.Values.ToList();
}
if (!ReferenceEquals(definition.Directives, directives) ||
!ReferenceEquals(definition.Interfaces, interfaces) ||
!ReferenceEquals(definition.Fields, fields))
{
return CreateDefinition(
definition.Name,
definition.Description,
directives,
interfaces,
fields);
}
return definition;
}
protected abstract TDef CreateDefinition(
NameNode name,
StringValueNode? description,
IReadOnlyList<DirectiveNode> directives,
IReadOnlyList<NamedTypeNode> interfaces,
IReadOnlyList<FieldDefinitionNode> fields);
private static FieldDefinitionNode ApplyExtensions(
string typeName,
FieldDefinitionNode definition,
FieldDefinitionNode extension)
{
// we first need to validate that the field structure can be merged.
if (definition.Arguments.Count != extension.Arguments.Count)
{
throw ExceptionHelper.ApplyExtensionsMiddleware_ArgumentCountMismatch(typeName, definition, extension);
}
IReadOnlyList<InputValueDefinitionNode> arguments = definition.Arguments;
IReadOnlyList<DirectiveNode> directives = definition.Directives;
if (definition.Arguments.Count > 0)
{
List<InputValueDefinitionNode>? temp = null;
for (var i = 0; i < definition.Arguments.Count; i++)
{
InputValueDefinitionNode arg = definition.Arguments[i];
InputValueDefinitionNode argExt = definition.Arguments[i];
if (!arg.Name.Equals(argExt.Name, SyntaxComparison.Syntax))
{
throw ExceptionHelper.ApplyExtensionsMiddleware_UnexpectedArgumentName(
arg.Name.Value,
argExt.Name.Value,
i,
typeName,
definition.Name.Value);
}
if (!arg.Type.Equals(argExt.Type, SyntaxComparison.Syntax))
{
throw ExceptionHelper.ApplyExtensionsMiddleware_ArgumentTypeMismatch(
arg,
argExt,
i,
typeName,
definition.Name.Value);
}
if (argExt.Directives.Count > 0)
{
if (temp is null)
{
temp = new();
if (i > 0)
{
for (var j = 0; j < i; j++)
{
temp.Add(arguments[j]);
}
}
}
var argDirectives = arg.Directives.ToList();
argDirectives.AddRange(argExt.Directives);
arg = arg.WithDirectives(argDirectives);
temp.Add(arg);
}
else if(temp is not null)
{
temp.Add(arg);
}
}
if (temp is not null)
{
arguments = temp;
}
}
if (extension.Directives.Count > 0)
{
var temp = definition.Directives.ToList();
temp.AddRange(extension.Directives);
directives = temp;
}
if (!ReferenceEquals(arguments, definition.Arguments) ||
!ReferenceEquals(directives, definition.Directives))
{
return new FieldDefinitionNode(
null,
definition.Name,
definition.Description,
arguments,
definition.Type,
directives);
}
return definition;
}
}