-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
ResultBuilder.cs
223 lines (185 loc) · 5.69 KB
/
ResultBuilder.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using HotChocolate.Execution.Properties;
namespace HotChocolate.Execution.Processing;
internal sealed partial class ResultBuilder
{
private readonly object _syncErrors = new();
private readonly List<IError> _errors = new();
private readonly HashSet<ISelection> _fieldErrors = new();
private readonly List<NonNullViolation> _nonNullViolations = new();
private readonly object _syncExtensions = new();
private readonly Dictionary<string, object?> _extensions = new();
private readonly Dictionary<string, object?> _contextData = new();
private readonly List<Func<ValueTask>> _cleanupTasks = new();
private ResultMemoryOwner _resultOwner = default!;
private ObjectResult? _data;
private Path? _path;
private string? _label;
private bool? _hasNext;
public IReadOnlyList<IError> Errors => _errors;
public void SetData(ObjectResult data)
=> _data = data;
public void SetExtension(string key, object? value)
{
lock (_syncExtensions)
{
_extensions[key] = value;
}
}
public void SetContextData(string key, object? value)
{
lock (_syncExtensions)
{
_contextData[key] = value;
}
}
/// <summary>
/// Register cleanup tasks that will be executed after resolver execution is finished.
/// </summary>
/// <param name="action">
/// Cleanup action.
/// </param>
public void RegisterForCleanup(Func<ValueTask> action)
{
lock (_syncExtensions)
{
_cleanupTasks.Add(action);
}
}
public void SetPath(Path? path)
=> _path = path;
public void SetLabel(string? label)
=> _label = label;
public void SetHasNext(bool value)
=> _hasNext = value;
public void AddError(IError error, ISelection? selection = null)
{
lock (_syncErrors)
{
_errors.Add(error);
if (selection is { })
{
_fieldErrors.Add(selection);
}
}
}
public void AddNonNullViolation(ISelection selection, Path path, ObjectResult parent)
{
lock (_syncErrors)
{
_nonNullViolations.Add(new NonNullViolation(selection, path, parent));
}
}
public IQueryResult BuildResult()
{
if (!ApplyNonNullViolations(_errors, _nonNullViolations, _fieldErrors))
{
// The non-null violation cased the whole result being deleted.
_data = null;
_resultOwner.Dispose();
}
if (_data is null && _errors.Count == 0 && _hasNext is not false)
{
throw new InvalidOperationException(
Resources.ResultHelper_BuildResult_InvalidResult);
}
if (_errors.Count > 0)
{
_errors.Sort(ErrorComparer.Default);
}
var result = new QueryResult
(
_data,
_errors.Count == 0 ? null : new List<IError>(_errors),
CreateExtensionData(_extensions),
CreateExtensionData(_contextData),
_label,
_path,
_hasNext,
_cleanupTasks.Count > 0
? _cleanupTasks.ToArray()
: Array.Empty<Func<ValueTask>>()
);
if (_data is not null)
{
result.RegisterForCleanup(_resultOwner);
}
return result;
}
public IQueryResultBuilder BuildResultBuilder()
{
if (!ApplyNonNullViolations(_errors, _nonNullViolations, _fieldErrors))
{
// The non-null violation cased the whole result being deleted.
_data = null;
_resultOwner.Dispose();
}
if (_data is null && _errors.Count == 0 && _hasNext is not false)
{
throw new InvalidOperationException(
Resources.ResultHelper_BuildResult_InvalidResult);
}
var builder = QueryResultBuilder.New();
builder.SetData(_data);
if (_errors.Count > 0)
{
_errors.Sort(ErrorComparer.Default);
builder.AddErrors(_errors);
}
builder.SetExtensions(CreateExtensionData(_extensions));
builder.SetContextData(CreateExtensionData(_contextData));
builder.SetLabel(_label);
builder.SetPath(_path);
if (_data is not null)
{
builder.RegisterForCleanup(_resultOwner);
}
return builder;
}
private static IReadOnlyDictionary<string, object?>? CreateExtensionData(
Dictionary<string, object?> data)
{
if (data.Count == 0)
{
return null;
}
return ImmutableDictionary.CreateRange(data);
}
public void DiscardResult()
=> _resultOwner.Dispose();
private sealed class ErrorComparer : IComparer<IError>
{
public int Compare(IError? x, IError? y)
{
if (ReferenceEquals(x, y))
{
return 0;
}
if (ReferenceEquals(null, y))
{
return 1;
}
if (ReferenceEquals(null, x))
{
return -1;
}
if (y.Locations?.Count > 0)
{
if (x.Locations?.Count > 0)
{
return x.Locations[0].CompareTo(y.Locations[0]);
}
return 1;
}
if (x.Locations?.Count > 0)
{
return -1;
}
return 0;
}
public static readonly ErrorComparer Default = new();
}
}