-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
ResolverTask.cs
148 lines (135 loc) · 4.83 KB
/
ResolverTask.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HotChocolate.Types;
namespace HotChocolate.Execution.Processing
{
internal sealed partial class ResolverTask : IExecutionTask
{
private readonly MiddlewareContext _context = new MiddlewareContext();
private ValueTask _task;
private IOperationContext _operationContext = default!;
private ISelection _selection = default!;
public bool IsCompleted => _task.IsCompleted;
public void BeginExecute()
{
_operationContext.Execution.TaskStats.TaskStarted();
_task = TryExecuteAndCompleteAsync();
}
private async ValueTask TryExecuteAndCompleteAsync()
{
try
{
using (_operationContext.DiagnosticEvents.ResolveFieldValue(_context))
{
bool success = await TryExecuteAsync().ConfigureAwait(false);
CompleteValue(success);
}
}
finally
{
_operationContext.Execution.TaskStats.TaskCompleted();
_operationContext.Execution.TaskPool.Return(this);
}
}
private async ValueTask<bool> TryExecuteAsync()
{
try
{
if (_selection.Arguments.TryCoerceArguments(
_context.Variables,
_context.ReportError,
out IReadOnlyDictionary<NameString, ArgumentValue>? coercedArgs))
{
_context.Arguments = coercedArgs;
await ExecuteResolverPipelineAsync().ConfigureAwait(false);
return true;
}
}
catch (Exception ex)
{
_context.ReportError(ex);
_context.Result = null;
}
return false;
}
private async ValueTask ExecuteResolverPipelineAsync()
{
await _context.ResolverPipeline(_context).ConfigureAwait(false);
switch (_context.Result)
{
case IExecutable query:
_context.Result = await query.ExecuteAsync(_context.RequestAborted);
break;
case IQueryable queryable:
_context.Result = await Task.Run(() =>
{
var items = new List<object?>();
foreach (object? o in queryable)
{
items.Add(o);
}
return items;
});
break;
case IError error:
_context.ReportError(error);
_context.Result = null;
break;
case IEnumerable<IError> errors:
foreach (IError error in errors)
{
_context.ReportError(error);
}
_context.Result = null;
break;
}
}
private void CompleteValue(bool success)
{
object? completedValue = null;
try
{
// we will only try to complete the resolver value if there are no known errors.
if (success)
{
if (ValueCompletion.TryComplete(
_operationContext,
_context,
_context.Path,
_context.Field.Type,
_context.Result,
out completedValue) &&
!_context.Field.Type.IsLeafType() &&
completedValue is IHasResultDataParent result)
{
result.Parent = _context.ResultMap;
}
}
}
catch (Exception ex)
{
_context.ReportError(ex);
_context.Result = null;
}
if (completedValue is null && _context.Field.Type.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(
_context.FieldSelection,
_context.Path,
_context.ResultMap);
}
else
{
_context.ResultMap.SetValue(
_context.ResponseIndex,
_context.ResponseName,
completedValue,
_context.Field.Type.IsNullableType());
}
}
}
}