-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
QueryRequestBuilder.cs
367 lines (309 loc) · 10.9 KB
/
QueryRequestBuilder.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
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Language;
using HotChocolate.Properties;
#nullable enable
namespace HotChocolate.Execution
{
public class QueryRequestBuilder : IQueryRequestBuilder
{
private IQuery? _query;
private string? _queryName;
private string? _queryHash;
private string? _operationName;
private IReadOnlyDictionary<string, object?>? _readOnlyVariableValues;
private Dictionary<string, object?>? _variableValuesDict;
private object? _initialValue;
private IReadOnlyDictionary<string, object?>? _readOnlyProperties;
private Dictionary<string, object?>? _properties;
private IReadOnlyDictionary<string, object?>? _readOnlyExtensions;
private Dictionary<string, object?>? _extensions;
private IServiceProvider? _services;
private OperationType[]? _allowedOperations;
public IQueryRequestBuilder SetQuery(string sourceText)
{
if (string.IsNullOrEmpty(sourceText))
{
throw new ArgumentException(
AbstractionResources.QueryRequestBuilder_QueryIsNullOrEmpty,
nameof(sourceText));
}
_query = new QuerySourceText(sourceText);
return this;
}
public IQueryRequestBuilder SetQuery(DocumentNode document)
{
if (document is null)
{
throw new ArgumentNullException(nameof(document));
}
_query = new QueryDocument(document);
return this;
}
public IQueryRequestBuilder SetQueryId(string? queryName)
{
_queryName = queryName;
return this;
}
public IQueryRequestBuilder SetQueryHash(string? queryHash)
{
_queryHash = queryHash;
return this;
}
public IQueryRequestBuilder SetOperation(string? operationName)
{
_operationName = operationName;
return this;
}
public IQueryRequestBuilder SetInitialValue(object? initialValue)
{
_initialValue = initialValue;
return this;
}
public IQueryRequestBuilder SetServices(
IServiceProvider? services)
{
_services = services;
return this;
}
public IQueryRequestBuilder TrySetServices(
IServiceProvider? services)
{
_services ??= services;
return this;
}
public IQueryRequestBuilder SetAllowedOperations(
OperationType[]? allowedOperations)
{
_allowedOperations = allowedOperations;
return this;
}
public IQueryRequestBuilder SetVariableValues(
Dictionary<string, object?>? variableValues) =>
SetVariableValues((IDictionary<string, object?>)variableValues);
public IQueryRequestBuilder SetVariableValues(
IDictionary<string, object?>? variableValues)
{
_variableValuesDict = variableValues is null
? null
: new Dictionary<string, object?>(variableValues);
_readOnlyVariableValues = null;
return this;
}
public IQueryRequestBuilder SetVariableValues(
IReadOnlyDictionary<string, object?>? variableValues)
{
_variableValuesDict = null;
_readOnlyVariableValues = variableValues;
return this;
}
public IQueryRequestBuilder SetVariableValue(string name, object? value)
{
InitializeVariables();
_variableValuesDict[name] = value;
return this;
}
public IQueryRequestBuilder AddVariableValue(
string name, object? value)
{
InitializeVariables();
_variableValuesDict.Add(name, value);
return this;
}
public IQueryRequestBuilder TryAddVariableValue(
string name, object? value)
{
InitializeVariables();
if (!_variableValuesDict.ContainsKey(name))
{
_variableValuesDict.Add(name, value);
}
return this;
}
public IQueryRequestBuilder SetProperties(
Dictionary<string, object?>? properties) =>
SetProperties((IDictionary<string, object?>?)properties);
public IQueryRequestBuilder SetProperties(
IDictionary<string, object?>? properties)
{
_properties = properties is null
? null
: new Dictionary<string, object?>(properties);
_readOnlyProperties = null;
return this;
}
public IQueryRequestBuilder SetProperties(
IReadOnlyDictionary<string, object?>? properties)
{
_properties = null;
_readOnlyProperties = properties;
return this;
}
public IQueryRequestBuilder SetProperty(string name, object? value)
{
InitializeProperties();
_properties[name] = value;
return this;
}
public IQueryRequestBuilder AddProperty(
string name, object? value)
{
InitializeProperties();
_properties.Add(name, value);
return this;
}
public IQueryRequestBuilder TryAddProperty(
string name, object? value)
{
InitializeProperties();
if (!_properties.ContainsKey(name))
{
_properties.Add(name, value);
}
return this;
}
public IQueryRequestBuilder SetExtensions(
Dictionary<string, object?>? extensions) =>
SetExtensions((IDictionary<string, object?>?)extensions);
public IQueryRequestBuilder SetExtensions(
IDictionary<string, object?>? extensions)
{
_extensions = extensions is null
? null
: new Dictionary<string, object?>(extensions);
_readOnlyExtensions = null;
return this;
}
public IQueryRequestBuilder SetExtensions(
IReadOnlyDictionary<string, object?>? extensions)
{
_extensions = null;
_readOnlyExtensions = extensions;
return this;
}
public IQueryRequestBuilder SetExtension(string name, object? value)
{
InitializeExtensions();
_extensions[name] = value;
return this;
}
public IQueryRequestBuilder AddExtension(
string name, object? value)
{
InitializeExtensions();
_extensions.Add(name, value);
return this;
}
public IQueryRequestBuilder TryAddExtension(
string name, object? value)
{
InitializeExtensions();
if (!_extensions.ContainsKey(name))
{
_extensions.Add(name, value);
}
return this;
}
public IReadOnlyQueryRequest Create()
{
return new QueryRequest
(
query: _query,
queryId: _queryName,
queryHash: _queryHash,
operationName: _operationName,
initialValue: _initialValue,
services: _services,
variableValues: GetVariableValues(),
contextData: GetProperties(),
extensions: GetExtensions(),
allowedOperations: _allowedOperations
);
}
private IReadOnlyDictionary<string, object?> GetVariableValues()
{
return _variableValuesDict ?? _readOnlyVariableValues;
}
private void InitializeVariables()
{
if (_variableValuesDict is null)
{
_variableValuesDict = _readOnlyVariableValues is null
? new Dictionary<string, object?>()
: _readOnlyVariableValues.ToDictionary(
t => t.Key, t => t.Value);
_readOnlyVariableValues = null;
}
}
private IReadOnlyDictionary<string, object?> GetProperties()
{
return _properties ?? _readOnlyProperties;
}
private void InitializeProperties()
{
if (_properties is null)
{
_properties = _readOnlyProperties is null
? new Dictionary<string, object?>()
: _readOnlyProperties.ToDictionary(
t => t.Key, t => t.Value);
_readOnlyProperties = null;
}
}
private IReadOnlyDictionary<string, object?> GetExtensions()
{
return _extensions ?? _readOnlyExtensions;
}
private void InitializeExtensions()
{
if (_extensions is null)
{
_extensions = _readOnlyExtensions is null
? new Dictionary<string, object?>()
: _readOnlyExtensions.ToDictionary(
t => t.Key, t => t.Value);
_readOnlyExtensions = null;
}
}
public static IReadOnlyQueryRequest Create(string query) =>
New().SetQuery(query).Create();
public static QueryRequestBuilder New() => new();
public static QueryRequestBuilder From(IQueryRequest request)
{
var builder = new QueryRequestBuilder
{
_query = request.Query,
_queryName = request.QueryId,
_queryHash = request.QueryHash,
_operationName = request.OperationName,
_readOnlyVariableValues = request.VariableValues,
_initialValue = request.InitialValue,
_readOnlyProperties = request.ContextData,
_readOnlyExtensions = request.Extensions,
_services = request.Services
};
if (builder._query is null && builder._queryName is null)
{
throw new QueryRequestBuilderException(
AbstractionResources.QueryRequestBuilder_QueryIsNull);
}
return builder;
}
public static QueryRequestBuilder From(GraphQLRequest request)
{
QueryRequestBuilder builder = New();
builder
.SetQueryId(request.QueryId)
.SetQueryHash(request.QueryHash)
.SetOperation(request.OperationName)
.SetVariableValues(request.Variables)
.SetExtensions(request.Extensions);
if (request.Query is not null)
{
builder.SetQuery(request.Query);
}
return builder;
}
}
}