-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
ResolverTask.Execute.cs
423 lines (357 loc) · 14 KB
/
ResolverTask.Execute.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution.Internal;
namespace HotChocolate.Execution.Processing.Tasks;
internal sealed partial class ResolverTask
{
private async Task ExecuteAsync(CancellationToken cancellationToken)
{
try
{
using (DiagnosticEvents.ResolveFieldValue(_resolverContext))
{
var success = await TryExecuteAsync(cancellationToken).ConfigureAwait(false);
CompleteValue(success, cancellationToken);
switch (_taskBuffer.Count)
{
case 0:
break;
case 1:
_operationContext.Scheduler.Register(_taskBuffer[0]);
break;
default:
_operationContext.Scheduler.Register(_taskBuffer);
break;
}
}
Status = _completionStatus;
}
catch
{
// If an exception occurs on this level it means that something was wrong with the
// operation context.
// In this case we will mark the task as faulted and set the result to null.
// However, we will not report or rethrow the exception since the context was already
// destroyed and we would cause further exceptions.
// The exception on this level is most likely caused by a cancellation of the request.
Status = ExecutionTaskStatus.Faulted;
_resolverContext.Result = null;
}
finally
{
_operationContext.Scheduler.Complete(this);
_objectPool.Return(this);
}
}
private async ValueTask<bool> TryExecuteAsync(CancellationToken cancellationToken)
{
try
{
// We will pre-check if the request was already canceled and mark the task as faulted if
// this is the case. This essentially gives us a cheap and easy way out without any
// exceptions.
if (cancellationToken.IsCancellationRequested)
{
_completionStatus = ExecutionTaskStatus.Faulted;
return false;
}
// If the arguments are already parsed and processed we can just process.
// Arguments need no pre-processing if they have no variables.
if (Selection.Arguments.IsFinalNoErrors)
{
_resolverContext.Arguments = Selection.Arguments;
await ExecuteResolverPipelineAsync(cancellationToken).ConfigureAwait(false);
return true;
}
// if this field has arguments that contain variables we first need to coerce them
// before we can start executing the resolver.
if (Selection.Arguments.TryCoerceArguments(_resolverContext, out var coercedArgs))
{
_resolverContext.Arguments = coercedArgs;
await ExecuteResolverPipelineAsync(cancellationToken).ConfigureAwait(false);
return true;
}
}
catch (Exception ex)
{
if (!cancellationToken.IsCancellationRequested)
{
// If cancellation has not been requested for the request we assume this to
// be a GraphQL resolver error and report it as such.
// This will let the error handler produce a GraphQL error and
// we set the result to null.
ResolverContext.ReportError(ex);
ResolverContext.Result = null;
}
}
return false;
}
private async ValueTask ExecuteResolverPipelineAsync(CancellationToken cancellationToken)
{
await _resolverContext.ResolverPipeline!(_resolverContext).ConfigureAwait(false);
var result = _resolverContext.Result;
if (result is null)
{
return;
}
if (result is IError error)
{
_resolverContext.ReportError(error);
_resolverContext.Result = null;
return;
}
// if we are not a list we do not need any further result processing.
if (!_selection.IsList)
{
return;
}
if (_selection.HasStreamDirective(_operationContext.IncludeFlags))
{
_resolverContext.Result = await CreateStreamResultAsync(result).ConfigureAwait(false);
return;
}
if (_selection.HasStreamResult)
{
_resolverContext.Result = await CreateListFromStreamAsync(result).ConfigureAwait(false);
return;
}
switch (_resolverContext.Result)
{
case IExecutable executable:
_resolverContext.Result = await executable
.ToListAsync(cancellationToken)
.ConfigureAwait(false);
break;
case IQueryable queryable:
_resolverContext.Result = await Task.Run(() =>
{
var items = new List<object?>();
foreach (var o in queryable)
{
items.Add(o);
if (cancellationToken.IsCancellationRequested)
{
break;
}
}
return items;
}, cancellationToken);
break;
}
}
private async ValueTask<List<object?>> CreateStreamResultAsync(object result)
{
var stream = StreamHelper.CreateStream(result);
var streamDirective = _selection.GetStreamDirective(_resolverContext.Variables)!;
var enumerator = stream.GetAsyncEnumerator(_resolverContext.RequestAborted);
var next = true;
try
{
var list = new List<object?>();
var initialCount = streamDirective.InitialCount;
var count = 0;
if (initialCount > 0)
{
while (next)
{
count++;
next = await enumerator.MoveNextAsync().ConfigureAwait(false);
list.Add(enumerator.Current);
if (count >= initialCount)
{
break;
}
}
}
if (next)
{
// if the stream has more items than the initial requested items then we will
// defer the rest of the stream.
_operationContext.DeferredScheduler.Register(
new DeferredStream(
Selection,
streamDirective.Label,
_resolverContext.Path.Clone(),
_resolverContext.Parent<object>(),
count - 1,
enumerator,
_resolverContext.ScopedContextData));
}
return list;
}
finally
{
if (!next)
{
// if there is no deferred work we will just dispose the enumerator.
// in the case we have deferred work, the deferred stream handler is
// responsible of handling the dispose.
await enumerator.DisposeAsync().ConfigureAwait(false);
}
}
}
private async ValueTask<List<object?>> CreateListFromStreamAsync(object result)
{
var enumerable = StreamHelper.CreateStream(_resolverContext.Result!);
var list = new List<object?>();
await foreach (var item in enumerable
.WithCancellation(_resolverContext.RequestAborted)
.ConfigureAwait(false))
{
list.Add(item);
}
return list;
}
public void CompleteUnsafe()
{
if (!this.IsCompleted())
{
Status = _completionStatus;
_operationContext.Scheduler.Complete(this);
_objectPool.Return(this);
}
}
}
internal static class StreamHelper
{
private delegate IAsyncEnumerable<object?> Factory(object result);
private static readonly MethodInfo _createStreamFromAsyncEnumerable =
typeof(StreamHelper).GetMethod(
nameof(CreateStreamFromAsyncEnumerable),
BindingFlags.Static | BindingFlags.NonPublic)!;
private static readonly MethodInfo _createStreamFromEnumerable =
typeof(StreamHelper).GetMethod(
nameof(CreateStreamFromEnumerable),
BindingFlags.Static | BindingFlags.NonPublic)!;
private static readonly ConcurrentDictionary<Type, Factory> _streamFactories = new();
public static IAsyncEnumerable<object?> CreateStream(object result)
{
var resultType = result.GetType();
var factory = _streamFactories.GetOrAdd(resultType, t => CreateFactory(t));
return factory.Invoke(result);
}
private static Factory CreateFactory(Type resultType)
{
var resultTypeInfo = CreateResultTypeInfo(resultType);
var resultParameter = Expression.Parameter(typeof(object), "result");
var method = resultTypeInfo.IsAsyncEnumerable
? _createStreamFromAsyncEnumerable.MakeGenericMethod(resultTypeInfo.ElementType)
: _createStreamFromEnumerable.MakeGenericMethod(resultTypeInfo.ElementType);
var castResult = Expression.Convert(resultParameter, resultType);
var callMethod = Expression.Call(method, castResult);
return Expression.Lambda<Factory>(callMethod, resultParameter).Compile();
}
private static ResultTypeInfo CreateResultTypeInfo(Type resultType)
{
var interfaces = resultType.GetInterfaces();
Type? elementType = null;
for (var index = 0; index < interfaces.Length; index++)
{
var interfaceType = interfaces[index];
if (interfaceType.IsGenericType)
{
var arguments = interfaceType.GetGenericArguments();
if (arguments.Length == 1)
{
var typeDefinition = interfaceType.GetGenericTypeDefinition();
if (typeDefinition == typeof(IAsyncEnumerable<>))
{
return new ResultTypeInfo(arguments[0], true);
}
if(elementType is null && typeDefinition == typeof(IEnumerable<>))
{
elementType = arguments[0];
}
}
}
}
if (elementType is not null)
{
return new ResultTypeInfo(elementType, false);
}
// TODO : EXCEPTION
throw new GraphQLException("The result type is not streamable!");
}
private static IAsyncEnumerable<object?> CreateStreamFromAsyncEnumerable<T>(
IAsyncEnumerable<T> asyncEnumerable)
=> new AsyncEnumerableFromAsyncEnumerable<T>(asyncEnumerable);
private static IAsyncEnumerable<object?> CreateStreamFromEnumerable<T>(
IEnumerable<T> enumerable)
=> enumerable is IAsyncEnumerable<T> asyncEnumerable
? CreateStreamFromAsyncEnumerable(asyncEnumerable)
: new AsyncEnumerableFromEnumerable<T>(enumerable);
private readonly ref struct ResultTypeInfo
{
public ResultTypeInfo(Type elementType, bool isAsyncEnumerable)
{
IsAsyncEnumerable = isAsyncEnumerable;
ElementType = elementType;
}
public Type ElementType { get; }
public bool IsAsyncEnumerable { get; }
}
private sealed class AsyncEnumerableFromEnumerable<T> : IAsyncEnumerable<object?>
{
private readonly IEnumerable<T> _enumerable;
public AsyncEnumerableFromEnumerable(IEnumerable<T> enumerable)
{
_enumerable = enumerable;
}
public IAsyncEnumerator<object?> GetAsyncEnumerator(
CancellationToken cancellationToken = default)
=> new AsyncEnumerableFromEnumerator(_enumerable, cancellationToken);
private sealed class AsyncEnumerableFromEnumerator : IAsyncEnumerator<object?>
{
private readonly IEnumerable<T> _enumerator;
private readonly CancellationToken _cancellationToken;
private List<T>? _list;
private int _index;
public AsyncEnumerableFromEnumerator(
IEnumerable<T> enumerator,
CancellationToken cancellationToken)
{
_enumerator = enumerator;
_cancellationToken = cancellationToken;
}
public object? Current { get; private set; }
public async ValueTask<bool> MoveNextAsync()
{
_list ??= await Task.Factory.StartNew(
() => _enumerator.ToList(),
_cancellationToken,
TaskCreationOptions.None,
TaskScheduler.Default);
if (_index >= _list.Count)
{
return false;
}
Current = _list[_index++];
return true;
}
public ValueTask DisposeAsync() => default;
}
}
private sealed class AsyncEnumerableFromAsyncEnumerable<T> : IAsyncEnumerable<object?>
{
private readonly IAsyncEnumerable<T> _enumerable;
public AsyncEnumerableFromAsyncEnumerable(IAsyncEnumerable<T> enumerable)
{
_enumerable = enumerable;
}
public async IAsyncEnumerator<object?> GetAsyncEnumerator(
CancellationToken cancellationToken = default)
{
await foreach (var element in
_enumerable.WithCancellation(cancellationToken).ConfigureAwait(false))
{
yield return element;
}
}
}
}