-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathGraphQLExecutor.cs
164 lines (130 loc) · 5.21 KB
/
GraphQLExecutor.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using GraphQL.Execution;
using GraphQL.Validation;
using GraphQL.Validation.Complexity;
// ReSharper disable once CheckNamespace
namespace GraphQL.Conventions
{
public class GraphQLExecutor : IGraphQLExecutor<ExecutionResult>
{
private readonly GraphQLEngine _engine;
private readonly IRequestDeserializer _requestDeserializer;
private object _rootObject;
private IUserContext _userContext;
private string _queryString;
private string _operationName;
private Inputs _inputs;
private CancellationToken _cancellationToken;
private IDependencyInjector _dependencyInjector;
private bool _enableValidation = true;
private bool _enableProfiling;
private IEnumerable<IValidationRule> _validationRules;
private LegacyComplexityConfiguration _complexityConfiguration;
private ComplexityOptions _complexityOptions;
private IEnumerable<IDocumentExecutionListener> _documentExecutionListeners;
public GraphQLExecutor(GraphQLEngine engine, IRequestDeserializer requestDeserializer)
{
_engine = engine;
_requestDeserializer = requestDeserializer;
}
public IGraphQLExecutor<ExecutionResult> WithRequest(string requestBody)
{
var query = _requestDeserializer.GetQueryFromRequestBody(requestBody);
_queryString = query.QueryString;
_operationName = query.OperationName;
return WithVariables(query.Variables);
}
public IGraphQLExecutor<ExecutionResult> WithQueryString(string queryString)
{
_queryString = queryString;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithOperationName(string operationName)
{
_operationName = operationName;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithVariables(Dictionary<string, object> inputs)
{
return WithVariables(new Inputs(inputs ?? new Dictionary<string, object>()));
}
public IGraphQLExecutor<ExecutionResult> WithVariables(Inputs inputs)
{
_inputs = inputs;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithRootObject(object rootValue)
{
_rootObject = rootValue;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithUserContext(IUserContext userContext)
{
_userContext = userContext;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithCancellationToken(CancellationToken token)
{
_cancellationToken = token;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithDependencyInjector(IDependencyInjector injector)
{
_dependencyInjector = injector;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithValidationRules(IEnumerable<IValidationRule> rules)
{
_validationRules = rules;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithListeners(params IDocumentExecutionListener[] listeners)
{
_documentExecutionListeners = listeners;
return this;
}
public IGraphQLExecutor<ExecutionResult> EnableValidation(bool enableValidation = true)
{
_enableValidation = enableValidation;
return this;
}
[Obsolete("Please use the WithComplexityOptions method instead.")]
public IGraphQLExecutor<ExecutionResult> WithComplexityConfiguration(LegacyComplexityConfiguration complexityConfiguration)
{
_complexityConfiguration = complexityConfiguration;
return this;
}
public IGraphQLExecutor<ExecutionResult> WithComplexityOptions(ComplexityOptions complexityOptions)
{
_complexityOptions = complexityOptions;
return this;
}
public IGraphQLExecutor<ExecutionResult> DisableValidation()
{
return EnableValidation(false);
}
public IGraphQLExecutor<ExecutionResult> EnableProfiling(bool enableProfiling = true)
{
_enableProfiling = enableProfiling;
return this;
}
public IGraphQLExecutor<ExecutionResult> DisableProfiling()
{
return EnableProfiling(false);
}
public Task<ExecutionResult> ExecuteAsync()
=> _engine.ExecuteAsync(
_rootObject, _queryString, _operationName, _inputs, _userContext, _dependencyInjector,
enableValidation: _enableValidation,
enableProfiling: _enableProfiling,
rules: _validationRules,
complexityConfiguration: _complexityConfiguration,
complexityOptions: _complexityOptions,
cancellationToken: _cancellationToken,
listeners: _documentExecutionListeners);
public Task<IValidationResult> ValidateAsync() => _engine.ValidateAsync(_queryString);
}
}