-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
ResolverTask.Execute.cs
263 lines (230 loc) · 8.37 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
using System;
using System.Collections.Generic;
using System.Linq;
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);
if (_resolverContext.Result is null)
{
return;
}
if (_resolverContext.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.IsStreamable)
{
StreamDirective streamDirective =
Selection.SyntaxNode.Directives.GetStreamDirective(
_resolverContext.Variables)!;
if (streamDirective.If)
{
_resolverContext.Result =
await CreateStreamResultAsync(streamDirective)
.ConfigureAwait(false);
return;
}
}
if (Selection.MaybeStream)
{
_resolverContext.Result =
await CreateListFromStreamAsync()
.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(
StreamDirective streamDirective)
{
IAsyncEnumerable<object?> enumerable = Selection.CreateStream(_resolverContext.Result!);
IAsyncEnumerator<object?> enumerator = enumerable.GetAsyncEnumerator();
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.Scheduler.DeferredWork.Register(
new DeferredStream(
Selection,
streamDirective.Label,
_resolverContext.Path,
_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()
{
IAsyncEnumerable<object?> enumerable = Selection.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);
}
}
}