-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
ResolverTaskFactory.cs
382 lines (346 loc) · 12.3 KB
/
ResolverTaskFactory.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Types;
namespace HotChocolate.Execution.Processing.Tasks;
internal static class ResolverTaskFactory
{
private static List<ResolverTask>? _pooled = new();
public static ObjectResult EnqueueResolverTasks(
OperationContext operationContext,
ISelectionSet selectionSet,
object? parent,
Path path,
IImmutableDictionary<string, object?> scopedContext)
{
var selectionsCount = selectionSet.Selections.Count;
var responseIndex = selectionsCount;
var parentResult = operationContext.Result.RentObject(selectionsCount);
var scheduler = operationContext.Scheduler;
var pathFactory = operationContext.PathFactory;
var includeFlags = operationContext.IncludeFlags;
var final = !selectionSet.IsConditional;
var bufferedTasks = Interlocked.Exchange(ref _pooled, null) ?? new();
Debug.Assert(bufferedTasks.Count == 0, "The buffer must be clean.");
try
{
ref var selectionSpace = ref ((SelectionSet)selectionSet).GetSelectionsReference();
// we are iterating reverse so that in the case of a mutation the first
// synchronous root selection is executed first, since the work scheduler
// is using two stacks one for parallel work an one for synchronous work.
// the scheduler this tries to schedule new work first.
// coincidentally we can use that to schedule a mutation so that we honor the spec
// guarantees while executing efficient.
for (var i = selectionsCount - 1; i >= 0; i--)
{
ref var selection = ref Unsafe.Add(ref selectionSpace, i);
if (final || selection.IsIncluded(includeFlags))
{
bufferedTasks.Add(
operationContext.CreateResolverTask(
selection,
parent,
parentResult,
--responseIndex,
pathFactory.Append(path, selection.ResponseName),
scopedContext));
}
}
if (bufferedTasks.Count == 0)
{
// in the case all root fields are skipped we execute a dummy task in order
// to not have to many extra API for this special case.
scheduler.Register(new NoOpExecutionTask(operationContext));
}
else
{
scheduler.Register(bufferedTasks);
}
if (selectionSet.Fragments.Count > 0)
{
TryHandleDeferredFragments(
operationContext,
selectionSet,
scopedContext,
path,
parent);
}
return parentResult;
}
finally
{
bufferedTasks.Clear();
Interlocked.Exchange(ref _pooled!, bufferedTasks);
}
}
public static ResolverTask EnqueueElementTasks(
OperationContext operationContext,
ISelection selection,
object? parent,
Path path,
int index,
IAsyncEnumerator<object?> value,
IImmutableDictionary<string, object?> scopedContext)
{
var parentResult = operationContext.Result.RentObject(1);
var bufferedTasks = Interlocked.Exchange(ref _pooled, null) ?? new();
Debug.Assert(bufferedTasks.Count == 0, "The buffer must be clean.");
var resolverTask =
operationContext.CreateResolverTask(
selection,
parent,
parentResult,
0,
path,
scopedContext);
try
{
CompleteInline(
operationContext,
resolverTask.ResolverContext,
selection,
selection.Type.ElementType(),
operationContext.PathFactory.Append(path, index),
0,
parentResult,
value.Current,
bufferedTasks);
}
finally
{
bufferedTasks.Clear();
Interlocked.Exchange(ref _pooled, bufferedTasks);
}
return resolverTask;
}
public static ObjectResult EnqueueOrInlineResolverTasks(
OperationContext operationContext,
MiddlewareContext resolverContext,
Path path,
ObjectType parentType,
object parent,
ISelectionSet selectionSet,
List<ResolverTask> bufferedTasks)
{
var responseIndex = 0;
var selectionsCount = selectionSet.Selections.Count;
var parentResult = operationContext.Result.RentObject(selectionsCount);
var pathFactory = operationContext.PathFactory;
var includeFlags = operationContext.IncludeFlags;
var final = !selectionSet.IsConditional;
ref var selectionSpace = ref ((SelectionSet)selectionSet).GetSelectionsReference();
for (var i = 0; i < selectionsCount; i++)
{
ref var selection = ref Unsafe.Add(ref selectionSpace, i);
if (final || selection.IsIncluded(includeFlags))
{
var selectionPath = pathFactory.Append(path, selection.ResponseName);
if (selection.Strategy is SelectionExecutionStrategy.Pure)
{
ResolveAndCompleteInline(
operationContext,
resolverContext,
selection,
selectionPath,
responseIndex++,
parentType,
parent,
parentResult,
bufferedTasks);
}
else
{
bufferedTasks.Add(
operationContext.CreateResolverTask(
selection,
parent,
parentResult,
responseIndex++,
selectionPath,
resolverContext.ScopedContextData));
}
}
}
if (selectionSet.Fragments.Count > 0)
{
TryHandleDeferredFragments(
operationContext,
selectionSet,
resolverContext.ScopedContextData,
path,
parent);
}
return parentResult;
}
private static void ResolveAndCompleteInline(
OperationContext operationContext,
MiddlewareContext resolverContext,
ISelection selection,
Path path,
int responseIndex,
ObjectType parentType,
object parent,
ObjectResult parentResult,
List<ResolverTask> bufferedTasks)
{
var executedSuccessfully = false;
object? resolverResult = null;
try
{
// we first try to create a context for our pure resolver.
// this should actually only fail if we are unable to coerce
// the field arguments.
if (resolverContext.TryCreatePureContext(
selection, path, parentType, parent,
out var childContext))
{
// if we have a pure context we can execute out pure resolver.
resolverResult = selection.PureResolver!(childContext);
executedSuccessfully = true;
}
}
catch (OperationCanceledException)
{
// If we run into this exception the request was aborted.
// In this case we do nothing and just return.
return;
}
catch (Exception ex)
{
operationContext.ReportError(ex, resolverContext, selection, path);
}
if (executedSuccessfully)
{
// if we were able to execute the resolver we will try to complete the
// resolver result inline and commit the value to the result..
CompleteInline(
operationContext,
resolverContext,
selection,
selection.Type,
path,
responseIndex,
parentResult,
resolverResult,
bufferedTasks);
}
else
{
// if we were not able to execute the resolver we will commit the null value
// of the resolver to the object result which could trigger a non-null propagation.
CommitValue(
operationContext,
selection,
path,
responseIndex,
parentResult,
resolverResult);
}
}
private static void CompleteInline(
OperationContext operationContext,
MiddlewareContext resolverContext,
ISelection selection,
IType selectionType,
Path path,
int responseIndex,
ObjectResult resultMap,
object? value,
List<ResolverTask> bufferedTasks)
{
object? completedValue = null;
try
{
completedValue = ValueCompletion.Complete(
operationContext,
resolverContext,
bufferedTasks,
selection,
path,
selectionType,
selection.ResponseName,
responseIndex,
value);
if (completedValue is ResultData result)
{
result.Parent = resultMap;
}
}
catch (OperationCanceledException)
{
// If we run into this exception the request was aborted.
// In this case we do nothing and just return.
return;
}
catch (Exception ex)
{
operationContext.ReportError(ex, resolverContext, selection, path);
}
CommitValue(
operationContext,
selection,
path,
responseIndex,
resultMap,
completedValue);
}
private static void CommitValue(
OperationContext operationContext,
ISelection selection,
Path path,
int responseIndex,
ObjectResult parentResult,
object? completedValue)
{
var isNonNullType = selection.Type.Kind is TypeKind.NonNull;
parentResult.SetValueUnsafe(
responseIndex,
selection.ResponseName,
completedValue,
!isNonNullType);
if (completedValue is null && isNonNullType)
{
// if we detect a non-null violation we will stash it for later.
// the non-null propagation is delayed so that we can parallelize better.
operationContext.Result.AddNonNullViolation(selection, path, parentResult);
}
}
private static void TryHandleDeferredFragments(
OperationContext operationContext,
ISelectionSet selectionSet,
IImmutableDictionary<string, object?> scopedContext,
Path path,
object? parent)
{
var fragments = selectionSet.Fragments;
var includeFlags = operationContext.IncludeFlags;
for (var i = 0; i < fragments.Count; i++)
{
var fragment = fragments[i];
if (!fragment.IsConditional || fragment.IsIncluded(includeFlags))
{
operationContext.DeferredScheduler.Register(
new DeferredFragment(
fragment,
fragment.GetLabel(operationContext.Variables),
path.Clone(),
parent,
scopedContext));
}
}
}
private sealed class NoOpExecutionTask : ExecutionTask
{
public NoOpExecutionTask(OperationContext context)
{
Context = context;
}
protected override IExecutionTaskContext Context { get; }
protected override ValueTask ExecuteAsync(CancellationToken cancellationToken)
=> default;
}
}