1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Request; |
6
|
|
|
|
7
|
|
|
use GraphQL\Executor\ExecutionResult; |
8
|
|
|
use GraphQL\Executor\Promise\Promise; |
9
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
10
|
|
|
use GraphQL\Type\Schema; |
11
|
|
|
use GraphQL\Validator\DocumentValidator; |
12
|
|
|
use GraphQL\Validator\Rules\DisableIntrospection; |
13
|
|
|
use GraphQL\Validator\Rules\QueryComplexity; |
14
|
|
|
use GraphQL\Validator\Rules\QueryDepth; |
15
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
16
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent; |
17
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorContextEvent; |
18
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorResultEvent; |
19
|
|
|
use Overblog\GraphQLBundle\Executor\ExecutorInterface; |
20
|
|
|
use Overblog\GraphQLBundle\Executor\Promise\PromiseAdapterInterface; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
23
|
|
|
|
24
|
|
|
class Executor |
25
|
|
|
{ |
26
|
|
|
public const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter'; |
27
|
|
|
|
28
|
|
|
/** @var Schema[] */ |
29
|
|
|
private $schemas = []; |
30
|
|
|
|
31
|
|
|
/** @var EventDispatcherInterface|null */ |
32
|
|
|
private $dispatcher; |
33
|
|
|
|
34
|
|
|
/** @var ExecutorInterface */ |
35
|
|
|
private $executor; |
36
|
|
|
|
37
|
|
|
/** @var PromiseAdapter */ |
38
|
|
|
private $promiseAdapter; |
39
|
|
|
|
40
|
|
|
/** @var callable|null */ |
41
|
|
|
private $defaultFieldResolver; |
42
|
|
|
|
43
|
106 |
|
public function __construct( |
44
|
|
|
ExecutorInterface $executor, |
45
|
|
|
EventDispatcherInterface $dispatcher, |
46
|
|
|
PromiseAdapter $promiseAdapter, |
47
|
|
|
?callable $defaultFieldResolver = null |
48
|
|
|
) { |
49
|
106 |
|
$this->executor = $executor; |
50
|
106 |
|
$this->dispatcher = $dispatcher; |
51
|
106 |
|
$this->promiseAdapter = $promiseAdapter; |
52
|
106 |
|
$this->defaultFieldResolver = $defaultFieldResolver; |
53
|
106 |
|
} |
54
|
|
|
|
55
|
2 |
|
public function setExecutor(ExecutorInterface $executor): self |
56
|
|
|
{ |
57
|
2 |
|
$this->executor = $executor; |
58
|
|
|
|
59
|
2 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function setPromiseAdapter(PromiseAdapter $promiseAdapter): self |
63
|
|
|
{ |
64
|
1 |
|
$this->promiseAdapter = $promiseAdapter; |
65
|
|
|
|
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @param Schema $schema |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
98 |
|
public function addSchema(string $name, Schema $schema): self |
76
|
|
|
{ |
77
|
98 |
|
$this->schemas[$name] = $schema; |
78
|
|
|
|
79
|
98 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string|null $name |
84
|
|
|
* |
85
|
|
|
* @return Schema |
86
|
|
|
*/ |
87
|
91 |
|
public function getSchema(?string $name = null): Schema |
88
|
|
|
{ |
89
|
91 |
|
if (empty($this->schemas)) { |
90
|
1 |
|
throw new \RuntimeException('At least one schema should be declare.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
90 |
|
if (null === $name) { |
94
|
84 |
|
$schema = \array_values($this->schemas)[0]; |
95
|
|
|
} else { |
96
|
6 |
|
if (!isset($this->schemas[$name])) { |
97
|
1 |
|
throw new NotFoundHttpException(\sprintf('Could not found "%s" schema.', $name)); |
98
|
|
|
} |
99
|
5 |
|
$schema = $this->schemas[$name]; |
100
|
|
|
} |
101
|
|
|
|
102
|
89 |
|
return $schema; |
103
|
|
|
} |
104
|
|
|
|
105
|
102 |
|
public function setMaxQueryDepth($maxQueryDepth): void |
106
|
|
|
{ |
107
|
|
|
/** @var QueryDepth $queryDepth */ |
108
|
102 |
|
$queryDepth = DocumentValidator::getRule('QueryDepth'); |
109
|
102 |
|
$queryDepth->setMaxQueryDepth($maxQueryDepth); |
110
|
102 |
|
} |
111
|
|
|
|
112
|
102 |
|
public function setMaxQueryComplexity($maxQueryComplexity): void |
113
|
|
|
{ |
114
|
|
|
/** @var QueryComplexity $queryComplexity */ |
115
|
102 |
|
$queryComplexity = DocumentValidator::getRule('QueryComplexity'); |
116
|
102 |
|
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity); |
117
|
102 |
|
} |
118
|
|
|
|
119
|
100 |
|
public function enableIntrospectionQuery(): void |
120
|
|
|
{ |
121
|
100 |
|
DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED)); |
122
|
100 |
|
} |
123
|
|
|
|
124
|
1 |
|
public function disableIntrospectionQuery(): void |
125
|
|
|
{ |
126
|
1 |
|
DocumentValidator::addRule(new DisableIntrospection()); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string|null $schemaName |
131
|
|
|
* @param array $request |
132
|
|
|
* @param array|\ArrayObject|object|null $rootValue |
133
|
|
|
* |
134
|
|
|
* @return ExecutionResult |
135
|
|
|
*/ |
136
|
87 |
|
public function execute(?string $schemaName = null, array $request, $rootValue = null): ExecutionResult |
137
|
|
|
{ |
138
|
87 |
|
$executorArgumentsEvent = $this->preExecute( |
139
|
87 |
|
$this->getSchema($schemaName), |
140
|
86 |
|
$request[ParserInterface::PARAM_QUERY] ?? null, |
141
|
86 |
|
new \ArrayObject(), |
142
|
86 |
|
$rootValue, |
143
|
86 |
|
$request[ParserInterface::PARAM_VARIABLES], |
144
|
86 |
|
$request[ParserInterface::PARAM_OPERATION_NAME] ?? null |
145
|
|
|
); |
146
|
|
|
|
147
|
85 |
|
$executorArgumentsEvent->getSchema()->processExtensions(); |
148
|
|
|
|
149
|
85 |
|
$result = $this->executor->execute( |
150
|
85 |
|
$executorArgumentsEvent->getSchema(), |
151
|
85 |
|
$executorArgumentsEvent->getRequestString(), |
152
|
85 |
|
$executorArgumentsEvent->getRootValue(), |
153
|
85 |
|
$executorArgumentsEvent->getContextValue(), |
|
|
|
|
154
|
85 |
|
$executorArgumentsEvent->getVariableValue(), |
155
|
85 |
|
$executorArgumentsEvent->getOperationName() |
156
|
|
|
); |
157
|
|
|
|
158
|
85 |
|
$result = $this->postExecute($result); |
159
|
|
|
|
160
|
81 |
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
86 |
|
private function preExecute( |
164
|
|
|
Schema $schema, |
165
|
|
|
?string $requestString, |
166
|
|
|
\ArrayObject $contextValue, |
167
|
|
|
$rootValue = null, |
168
|
|
|
?array $variableValue = null, |
169
|
|
|
?string $operationName = null |
170
|
|
|
): ExecutorArgumentsEvent { |
171
|
86 |
|
$this->checkPromiseAdapter(); |
172
|
|
|
|
173
|
85 |
|
$this->executor->setPromiseAdapter($this->promiseAdapter); |
174
|
|
|
// this is needed when not using only generated types |
175
|
85 |
|
if ($this->defaultFieldResolver) { |
176
|
83 |
|
$this->executor->setDefaultFieldResolver($this->defaultFieldResolver); |
177
|
|
|
} |
178
|
85 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, new ExecutorContextEvent($contextValue)); |
|
|
|
|
179
|
|
|
|
180
|
85 |
|
return $this->dispatcher->dispatch( |
|
|
|
|
181
|
85 |
|
Events::PRE_EXECUTOR, |
182
|
85 |
|
ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param ExecutionResult|Promise $result |
188
|
|
|
* |
189
|
|
|
* @return ExecutionResult |
190
|
|
|
*/ |
191
|
85 |
|
private function postExecute($result): ExecutionResult |
192
|
|
|
{ |
193
|
85 |
|
if ($result instanceof Promise) { |
194
|
83 |
|
$result = $this->promiseAdapter->wait($result); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
85 |
|
$this->checkExecutionResult($result); |
198
|
|
|
|
199
|
83 |
|
return $this->dispatcher->dispatch(Events::POST_EXECUTOR, new ExecutorResultEvent($result))->getResult(); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
86 |
|
private function checkPromiseAdapter(): void |
203
|
|
|
{ |
204
|
86 |
|
if ($this->promiseAdapter && !$this->promiseAdapter instanceof PromiseAdapterInterface && !\is_callable([$this->promiseAdapter, 'wait'])) { |
205
|
1 |
|
throw new \RuntimeException( |
206
|
1 |
|
\sprintf( |
207
|
1 |
|
'PromiseAdapter should be an object instantiating "%s" or "%s" with a "wait" method.', |
208
|
1 |
|
PromiseAdapterInterface::class, |
209
|
1 |
|
PromiseAdapter::class |
210
|
|
|
) |
211
|
|
|
); |
212
|
|
|
} |
213
|
85 |
|
} |
214
|
|
|
|
215
|
85 |
|
private function checkExecutionResult($result): void |
216
|
|
|
{ |
217
|
85 |
|
if (!\is_object($result) || !$result instanceof ExecutionResult) { |
218
|
2 |
|
throw new \RuntimeException( |
219
|
2 |
|
\sprintf('Execution result should be an object instantiating "%s".', ExecutionResult::class) |
220
|
|
|
); |
221
|
|
|
} |
222
|
83 |
|
} |
223
|
|
|
} |
224
|
|
|
|