-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
Selection.cs
357 lines (292 loc) · 10.2 KB
/
Selection.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using HotChocolate.Execution.Properties;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using Microsoft.Extensions.ObjectPool;
namespace HotChocolate.Execution.Processing;
/// <summary>
/// Represents a field selection during execution.
/// </summary>
public class Selection : ISelection
{
private static readonly ArgumentMap _emptyArguments = ArgumentMap.Empty;
private long[] _includeConditions;
private long _streamIfCondition;
private Flags _flags;
public Selection(
int id,
IObjectType declaringType,
IObjectField field,
IType type,
FieldNode syntaxNode,
string responseName,
ArgumentMap? arguments = null,
long[]? includeConditions = null,
bool isInternal = false,
bool isParallelExecutable = true,
FieldDelegate? resolverPipeline = null,
PureFieldDelegate? pureResolver = null)
{
Id = id;
DeclaringType = declaringType;
Field = field;
Type = type;
SyntaxNode = syntaxNode;
ResponseName = responseName;
Arguments = arguments ?? _emptyArguments;
ResolverPipeline = resolverPipeline;
PureResolver = pureResolver;
Strategy = InferStrategy(!isParallelExecutable, pureResolver is not null);
_includeConditions = includeConditions ?? Array.Empty<long>();
_flags = isInternal
? Flags.Internal
: Flags.None;
if (Type.IsListType())
{
_flags |= Flags.List;
}
if (Field.HasStreamResult)
{
_flags |= Flags.StreamResult;
}
}
protected Selection(Selection selection)
{
if (selection is null)
{
throw new ArgumentNullException(nameof(selection));
}
Id = selection.Id;
Strategy = selection.Strategy;
DeclaringType = selection.DeclaringType;
Field = selection.Field;
Type = selection.Type;
SyntaxNode = selection.SyntaxNode;
ResponseName = selection.ResponseName;
ResolverPipeline = selection.ResolverPipeline;
PureResolver = selection.PureResolver;
Arguments = selection.Arguments;
_flags = selection._flags;
_includeConditions =
selection._includeConditions.Length == 0
? Array.Empty<long>()
: selection._includeConditions.ToArray();
}
/// <inheritdoc />
public int Id { get; }
/// <inheritdoc />
public SelectionExecutionStrategy Strategy { get; private set; }
/// <inheritdoc />
public IObjectType DeclaringType { get; }
/// <inheritdoc />
public ISelectionSet DeclaringSelectionSet { get; private set; } = default!;
/// <inheritdoc />
public IObjectField Field { get; }
/// <inheritdoc />
public IType Type { get; }
/// <inheritdoc />
public TypeKind TypeKind => Type.Kind;
/// <inheritdoc />
public bool IsList => (_flags & Flags.List) == Flags.List;
/// <inheritdoc />
public FieldNode SyntaxNode { get; private set; }
public int SelectionSetId { get; private set; }
/// <inheritdoc />
public SelectionSetNode? SelectionSet => SyntaxNode.SelectionSet;
/// <inheritdoc />
public string ResponseName { get; }
/// <inheritdoc />
public FieldDelegate? ResolverPipeline { get; private set; }
/// <inheritdoc />
public PureFieldDelegate? PureResolver { get; private set; }
/// <inheritdoc />
public ArgumentMap Arguments { get; }
/// <inheritdoc />
public bool HasStreamResult => (_flags & Flags.StreamResult) == Flags.StreamResult;
/// <inheritdoc />
public bool HasStreamDirective(long includeFlags)
=> (_flags & Flags.Stream) == Flags.Stream &&
(_streamIfCondition is 0 || (includeFlags & _streamIfCondition) != _streamIfCondition);
/// <summary>
/// Specifies if the current selection is immutable.
/// </summary>
public bool IsReadOnly => (_flags & Flags.Sealed) == Flags.Sealed;
/// <inheritdoc />
public bool IsInternal => (_flags & Flags.Internal) == Flags.Internal;
/// <inheritdoc />
public bool IsConditional
=> _includeConditions.Length > 0 || (_flags & Flags.Internal) == Flags.Internal;
internal ReadOnlySpan<long> IncludeConditions => _includeConditions;
public bool IsIncluded(long includeFlags, bool allowInternals = false)
{
// in most case we do not have any include condition,
// so we can take the easy way out here if we do not have any flags.
if (_includeConditions.Length is 0)
{
return !IsInternal || allowInternals;
}
// if there are flags in most cases we just have one so we can
// check the first and optimize for this.
var includeCondition = _includeConditions[0];
if ((includeFlags & includeCondition) == includeCondition)
{
return !IsInternal || allowInternals;
}
// if we just have one flag and the flags are not fulfilled we can just exit.
if (_includeConditions.Length is 1)
{
return false;
}
// else, we will iterate over the rest of the conditions and validate them one by one.
for (var i = 1; i < _includeConditions.Length; i++)
{
includeCondition = _includeConditions[i];
if ((includeFlags & includeCondition) == includeCondition)
{
return !IsInternal || allowInternals;
}
}
return false;
}
public override string ToString()
=> SyntaxNode.ToString();
internal void AddSelection(FieldNode selectionSyntax, long includeCondition = 0)
{
if ((_flags & Flags.Sealed) == Flags.Sealed)
{
throw new NotSupportedException(Resources.PreparedSelection_ReadOnly);
}
if (includeCondition is not 0 &&
Array.IndexOf(_includeConditions, includeCondition) == -1)
{
var next = _includeConditions.Length;
Array.Resize(ref _includeConditions, next + 1);
_includeConditions[next] = includeCondition;
}
if (!SyntaxNode.Equals(selectionSyntax, SyntaxComparison.Syntax))
{
SyntaxNode = MergeField(SyntaxNode, selectionSyntax);
}
}
private static FieldNode MergeField(
FieldNode first,
FieldNode other)
{
var directives = first.Directives;
if (other.Directives.Count > 0)
{
if (directives.Count == 0)
{
directives = other.Directives;
}
else
{
var temp = new DirectiveNode[directives.Count + other.Directives.Count];
var next = 0;
for (var i = 0; i < directives.Count; i++)
{
temp[next++] = directives[i];
}
for (var i = 0; i < first.Directives.Count; i++)
{
temp[next++] = first.Directives[i];
}
directives = temp;
}
}
var selectionSet = first.SelectionSet;
if (selectionSet is not null && other.SelectionSet is not null)
{
var selections = new ISelectionNode[
selectionSet.Selections.Count +
other.SelectionSet.Selections.Count];
var next = 0;
for (var i = 0; i < selectionSet.Selections.Count; i++)
{
selections[next++] = selectionSet.Selections[i];
}
for (var i = 0; i < other.SelectionSet.Selections.Count; i++)
{
selections[next++] = other.SelectionSet.Selections[i];
}
selectionSet = selectionSet.WithSelections(selections);
}
return new FieldNode(
first.Location,
first.Name,
first.Alias,
first.Required,
directives,
first.Arguments,
selectionSet);
}
internal void SetResolvers(
FieldDelegate? resolverPipeline = null,
PureFieldDelegate? pureResolver = null)
{
if ((_flags & Flags.Sealed) == Flags.Sealed)
{
throw new NotSupportedException(Resources.PreparedSelection_ReadOnly);
}
ResolverPipeline = resolverPipeline;
PureResolver = pureResolver;
Strategy = InferStrategy(hasPureResolver: pureResolver is not null);
}
internal void SetSelectionSetId(int selectionSetId)
{
if ((_flags & Flags.Sealed) == Flags.Sealed)
{
throw new NotSupportedException(Resources.PreparedSelection_ReadOnly);
}
SelectionSetId = selectionSetId;
}
internal void MarkAsStream(long ifCondition)
{
if ((_flags & Flags.Sealed) == Flags.Sealed)
{
throw new NotSupportedException(Resources.PreparedSelection_ReadOnly);
}
_streamIfCondition = ifCondition;
_flags |= Flags.Stream;
}
internal void Seal(ISelectionSet declaringSelectionSet)
{
if ((_flags & Flags.Sealed) != Flags.Sealed)
{
DeclaringSelectionSet = declaringSelectionSet;
_flags |= Flags.Sealed;
}
Debug.Assert(
ReferenceEquals(declaringSelectionSet, DeclaringSelectionSet),
"Selections can only belong to a single selectionSet.");
}
private SelectionExecutionStrategy InferStrategy(
bool isSerial = false,
bool hasPureResolver = false)
{
// once a field is marked serial it even with a pure resolver cannot become pure.
if (Strategy is SelectionExecutionStrategy.Serial || isSerial)
{
return SelectionExecutionStrategy.Serial;
}
if (hasPureResolver)
{
return SelectionExecutionStrategy.Pure;
}
return SelectionExecutionStrategy.Default;
}
[Flags]
private enum Flags
{
None = 0,
Internal = 1,
Sealed = 2,
List = 4,
Stream = 8,
StreamResult = 16
}
}