-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathRequestHandler.cs
369 lines (328 loc) · 13.5 KB
/
RequestHandler.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using GraphQL.Conventions.Types.Resolution;
using GraphQL.Instrumentation;
using GraphQL.Validation.Complexity;
using Type = System.Type;
namespace GraphQL.Conventions.Web
{
public static class RequestHandler
{
public delegate object ResolveTypeDelegate(Type type);
public static RequestHandlerBuilder New()
{
return new RequestHandlerBuilder();
}
public class RequestHandlerBuilder : IDependencyInjector
{
private readonly List<Type> _schemaTypes = new List<Type>();
private readonly List<Type> _assemblyTypes = new List<Type>();
private readonly List<Type> _exceptionsTreatedAsWarnings = new List<Type>();
private readonly List<Type> _middleware = new List<Type>();
private readonly ITypeResolver _typeResolver = new TypeResolver();
private IDependencyInjector _dependencyInjector;
private ResolveTypeDelegate _resolveTypeDelegate;
private bool _useValidation = true;
private bool _useProfiling;
private FieldResolutionStrategy _fieldResolutionStrategy = FieldResolutionStrategy.Normal;
private LegacyComplexityConfiguration _complexityConfiguration;
private ComplexityOptions _complexityOptions;
internal RequestHandlerBuilder()
{
_dependencyInjector = this;
}
public RequestHandlerBuilder WithDependencyInjector(IDependencyInjector dependencyInjector)
{
_dependencyInjector = dependencyInjector;
return this;
}
public RequestHandlerBuilder WithDependencyInjector(ResolveTypeDelegate resolveTypeDelegate)
{
_resolveTypeDelegate = resolveTypeDelegate;
return this;
}
public RequestHandlerBuilder WithQuery<TQuery>()
{
_schemaTypes.Add(typeof(SchemaDefinition<TQuery>));
return this;
}
public RequestHandlerBuilder WithQuery(Type type)
{
_schemaTypes.Add(typeof(SchemaDefinition<>).MakeGenericType(type));
return this;
}
public RequestHandlerBuilder WithMutation<TMutation>()
{
_schemaTypes.Add(typeof(SchemaDefinitionWithMutation<TMutation>));
return this;
}
public RequestHandlerBuilder WithMutation(Type type)
{
_schemaTypes.Add(typeof(SchemaDefinitionWithMutation<>).MakeGenericType(type));
return this;
}
public RequestHandlerBuilder WithQueryAndMutation<TQuery, TMutation>()
{
_schemaTypes.Add(typeof(SchemaDefinition<TQuery, TMutation>));
return this;
}
public RequestHandlerBuilder WithQueryExtensions(Type typeExtensions)
{
_typeResolver.AddExtensions(typeExtensions);
return this;
}
public RequestHandlerBuilder WithSubscription<TSubscription>()
{
_schemaTypes.Add(typeof(SchemaDefinitionWithSubscription<TSubscription>));
return this;
}
public RequestHandlerBuilder WithSubscription(Type type)
{
_schemaTypes.Add(typeof(SchemaDefinitionWithSubscription<>).MakeGenericType(type));
return this;
}
public RequestHandlerBuilder WithAttributesFromAssembly<TAssemblyType>()
{
return WithAttributesFromAssembly(typeof(TAssemblyType));
}
public RequestHandlerBuilder WithAttributesFromAssembly(Type assemblyType)
{
_assemblyTypes.Add(assemblyType);
return this;
}
public RequestHandlerBuilder WithAttributesFromAssemblies(IEnumerable<Type> assemblyTypes)
{
_assemblyTypes.AddRange(assemblyTypes);
return this;
}
public RequestHandlerBuilder TreatAsWarning<TException>()
{
_exceptionsTreatedAsWarnings.Add(typeof(TException));
return this;
}
public RequestHandlerBuilder WithoutValidation(bool outputViolationsAsWarnings = false)
{
_useValidation = false;
return this;
}
public RequestHandlerBuilder WithProfiling(bool profiling = true)
{
_useProfiling = profiling;
return this;
}
public RequestHandlerBuilder WithFieldResolutionStrategy(FieldResolutionStrategy strategy)
{
_fieldResolutionStrategy = strategy;
return this;
}
[Obsolete("Please use the WithComplexityOptions method instead.")]
public RequestHandlerBuilder WithComplexityConfiguration(LegacyComplexityConfiguration complexityConfiguration)
{
_complexityConfiguration = complexityConfiguration;
return this;
}
public RequestHandlerBuilder WithComplexityOptions(ComplexityOptions complexityOptions)
{
_complexityOptions = complexityOptions;
return this;
}
public RequestHandlerBuilder WithMiddleware<T>()
{
_middleware.Add(typeof(T));
return this;
}
public RequestHandlerBuilder IgnoreTypesFromNamespacesStartingWith(params string[] namespacesToIgnore)
{
_typeResolver.IgnoreTypesFromNamespacesStartingWith(namespacesToIgnore);
return this;
}
public IRequestHandler Generate()
{
return new RequestHandlerImpl(
_dependencyInjector,
_schemaTypes,
_assemblyTypes,
_exceptionsTreatedAsWarnings,
_useValidation,
_useProfiling,
_fieldResolutionStrategy,
_complexityConfiguration,
_complexityOptions,
_middleware,
_typeResolver);
}
public object Resolve(TypeInfo typeInfo)
{
return _resolveTypeDelegate?.Invoke(typeInfo.AsType());
}
}
private class RequestHandlerImpl : IRequestHandler
{
private readonly GraphQLEngine _engine;
private readonly IDependencyInjector _dependencyInjector;
private readonly List<Type> _exceptionsTreatedAsWarnings = new List<Type>();
private readonly bool _useValidation;
private readonly bool _useProfiling;
private readonly LegacyComplexityConfiguration _complexityConfiguration;
private readonly ComplexityOptions _complexityOptions;
internal RequestHandlerImpl(
IDependencyInjector dependencyInjector,
IEnumerable<Type> schemaTypes,
IEnumerable<Type> assemblyTypes,
IEnumerable<Type> exceptionsTreatedAsWarning,
bool useValidation,
bool useProfiling,
FieldResolutionStrategy fieldResolutionStrategy,
LegacyComplexityConfiguration complexityConfiguration,
ComplexityOptions complexityOptions,
IEnumerable<Type> middleware,
ITypeResolver typeResolver)
{
_engine = new GraphQLEngine(typeResolver: typeResolver);
_dependencyInjector = dependencyInjector;
_engine.WithAttributesFromAssemblies(assemblyTypes);
_exceptionsTreatedAsWarnings.AddRange(exceptionsTreatedAsWarning);
_useValidation = useValidation;
_useProfiling = useProfiling;
_engine.WithFieldResolutionStrategy(fieldResolutionStrategy);
_engine.BuildSchema(schemaTypes.ToArray());
_complexityConfiguration = complexityConfiguration;
_complexityOptions = complexityOptions;
foreach (var type in middleware)
{
_engine.WithMiddleware(type);
}
}
public async Task<Response> ProcessRequestAsync(Request request, IUserContext userContext, IDependencyInjector dependencyInjector = null)
{
var start = DateTime.UtcNow;
var result = await _engine
.NewExecutor()
.WithQueryString(request.QueryString)
.WithVariables(request.Variables)
.WithOperationName(request.OperationName)
.WithDependencyInjector(dependencyInjector ?? _dependencyInjector)
.WithUserContext(userContext)
.WithComplexityConfiguration(_complexityConfiguration)
.WithComplexityOptions(_complexityOptions)
.EnableValidation(_useValidation)
.EnableProfiling(_useProfiling)
.ExecuteAsync()
.ConfigureAwait(false);
if (_useProfiling)
{
result.EnrichWithApolloTracing(start);
}
var response = new Response(request, result);
var errors = result?.Errors?.Where(e => !string.IsNullOrWhiteSpace(e?.Message));
foreach (var error in errors ?? new List<ExecutionError>())
{
if (_exceptionsTreatedAsWarnings.Contains(error.InnerException?.GetType()))
{
response.Warnings.Add(error);
}
else
{
response.Errors.Add(error);
}
}
if (result == null)
return response;
result.Errors = new ExecutionErrors();
result.Errors.AddRange(response.Errors);
response.SetBody(_engine.SerializeResult(result));
return response;
}
public async Task<Response> ValidateAsync(Request request)
{
var result = await _engine.ValidateAsync(request.QueryString);
return new Response(request, result);
}
public async Task<string> DescribeSchemaAsync(
bool returnJson = false,
bool includeFieldDescriptions = false,
bool includeFieldDeprecationReasons = true)
{
if (returnJson)
{
var result = await _engine
.NewExecutor()
.WithQueryString(IntrospectionQuery)
.ExecuteAsync();
return _engine.SerializeResult(result);
}
_engine.PrintFieldDescriptions(includeFieldDescriptions);
_engine.PrintFieldDeprecationReasons(includeFieldDeprecationReasons);
return _engine.Describe();
}
#region Queries
// Source: https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js
private const string IntrospectionQuery = @"
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types { ...FullType }
directives {
name
description
args { ...InputValue }
onOperation
onFragment
onField
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args { ...InputValue }
type { ...TypeRef }
isDeprecated
deprecationReason
}
inputFields { ...InputValue }
interfaces { ...TypeRef }
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes { ...TypeRef }
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
";
#endregion
}
}
}