|
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\PromiseAdapter; |
|
9
|
|
|
use GraphQL\GraphQL; |
|
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\EventDispatcherVersionHelper; |
|
16
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
|
17
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent; |
|
18
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorContextEvent; |
|
19
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorResultEvent; |
|
20
|
|
|
use Overblog\GraphQLBundle\Executor\ExecutorInterface; |
|
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
|
|
|
private $schemas = []; |
|
29
|
|
|
|
|
30
|
|
|
private $dispatcher; |
|
31
|
|
|
|
|
32
|
|
|
private $promiseAdapter; |
|
33
|
|
|
|
|
34
|
|
|
private $executor; |
|
35
|
|
|
|
|
36
|
|
|
private $defaultFieldResolver; |
|
37
|
|
|
|
|
38
|
|
|
private $useExperimentalExecutor; |
|
39
|
|
|
|
|
40
|
106 |
|
public function __construct( |
|
41
|
|
|
ExecutorInterface $executor, |
|
42
|
|
|
PromiseAdapter $promiseAdapter, |
|
43
|
|
|
EventDispatcherInterface $dispatcher, |
|
44
|
|
|
?callable $defaultFieldResolver = null, |
|
45
|
|
|
bool $useExperimental = false |
|
46
|
|
|
) { |
|
47
|
106 |
|
$this->executor = $executor; |
|
48
|
106 |
|
$this->promiseAdapter = $promiseAdapter; |
|
49
|
106 |
|
$this->dispatcher = $dispatcher; |
|
50
|
106 |
|
$this->defaultFieldResolver = $defaultFieldResolver; |
|
51
|
106 |
|
$this->useExperimentalExecutor = $useExperimental; |
|
52
|
106 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setExecutor(ExecutorInterface $executor): self |
|
55
|
|
|
{ |
|
56
|
|
|
$this->executor = $executor; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
98 |
|
public function addSchemaBuilder(string $name, callable $builder): self |
|
62
|
|
|
{ |
|
63
|
98 |
|
$this->schemas[$name] = $builder; |
|
64
|
|
|
|
|
65
|
98 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @param Schema $schema |
|
71
|
|
|
* |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
89 |
|
public function addSchema(string $name, Schema $schema): self |
|
75
|
|
|
{ |
|
76
|
89 |
|
$this->schemas[$name] = $schema; |
|
77
|
|
|
|
|
78
|
89 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string|null $name |
|
83
|
|
|
* |
|
84
|
|
|
* @return Schema |
|
85
|
|
|
*/ |
|
86
|
92 |
|
public function getSchema(?string $name = null): Schema |
|
87
|
|
|
{ |
|
88
|
92 |
|
if (empty($this->schemas)) { |
|
89
|
1 |
|
throw new \RuntimeException('At least one schema should be declare.'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
91 |
|
if (null === $name) { |
|
93
|
|
|
// TODO(mcg-web): Replace by array_key_first PHP 7 >= 7.3.0. |
|
94
|
85 |
|
foreach ($this->schemas as $name => $schema) { |
|
95
|
85 |
|
break; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
91 |
|
if (!isset($this->schemas[$name])) { |
|
99
|
1 |
|
throw new NotFoundHttpException(\sprintf('Could not found "%s" schema.', $name)); |
|
100
|
|
|
} |
|
101
|
90 |
|
$schema = $this->schemas[$name]; |
|
102
|
90 |
|
if (\is_callable($schema)) { |
|
103
|
90 |
|
$schema = $schema(); |
|
104
|
89 |
|
$this->addSchema($name, $schema); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
89 |
|
return $schema; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
105 |
|
public function setMaxQueryDepth($maxQueryDepth): void |
|
111
|
|
|
{ |
|
112
|
|
|
/** @var QueryDepth $queryDepth */ |
|
113
|
105 |
|
$queryDepth = DocumentValidator::getRule('QueryDepth'); |
|
114
|
105 |
|
$queryDepth->setMaxQueryDepth($maxQueryDepth); |
|
115
|
105 |
|
} |
|
116
|
|
|
|
|
117
|
105 |
|
public function setMaxQueryComplexity($maxQueryComplexity): void |
|
118
|
|
|
{ |
|
119
|
|
|
/** @var QueryComplexity $queryComplexity */ |
|
120
|
105 |
|
$queryComplexity = DocumentValidator::getRule('QueryComplexity'); |
|
121
|
105 |
|
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity); |
|
122
|
105 |
|
} |
|
123
|
|
|
|
|
124
|
104 |
|
public function enableIntrospectionQuery(): void |
|
125
|
|
|
{ |
|
126
|
104 |
|
DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED)); |
|
127
|
104 |
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
public function disableIntrospectionQuery(): void |
|
130
|
|
|
{ |
|
131
|
1 |
|
DocumentValidator::addRule(new DisableIntrospection()); |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string|null $schemaName |
|
136
|
|
|
* @param array $request |
|
137
|
|
|
* @param array|\ArrayObject|object|null $rootValue |
|
138
|
|
|
* |
|
139
|
|
|
* @return ExecutionResult |
|
140
|
|
|
*/ |
|
141
|
88 |
|
public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult |
|
142
|
|
|
{ |
|
143
|
88 |
|
$this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor(); |
|
144
|
|
|
|
|
145
|
88 |
|
$executorArgumentsEvent = $this->preExecute( |
|
146
|
88 |
|
$this->getSchema($schemaName), |
|
147
|
86 |
|
$request[ParserInterface::PARAM_QUERY] ?? null, |
|
148
|
86 |
|
new \ArrayObject(), |
|
149
|
|
|
$rootValue, |
|
150
|
86 |
|
$request[ParserInterface::PARAM_VARIABLES], |
|
151
|
86 |
|
$request[ParserInterface::PARAM_OPERATION_NAME] ?? null |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
86 |
|
$executorArgumentsEvent->getSchema()->processExtensions(); |
|
155
|
|
|
|
|
156
|
86 |
|
$result = $this->executor->execute( |
|
157
|
86 |
|
$this->promiseAdapter, |
|
158
|
86 |
|
$executorArgumentsEvent->getSchema(), |
|
159
|
86 |
|
$executorArgumentsEvent->getRequestString(), |
|
160
|
86 |
|
$executorArgumentsEvent->getRootValue(), |
|
161
|
86 |
|
$executorArgumentsEvent->getContextValue(), |
|
|
|
|
|
|
162
|
86 |
|
$executorArgumentsEvent->getVariableValue(), |
|
163
|
86 |
|
$executorArgumentsEvent->getOperationName(), |
|
164
|
86 |
|
$this->defaultFieldResolver |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
86 |
|
$result = $this->postExecute($result); |
|
168
|
|
|
|
|
169
|
85 |
|
return $result; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
86 |
|
private function preExecute( |
|
173
|
|
|
Schema $schema, |
|
174
|
|
|
?string $requestString, |
|
175
|
|
|
\ArrayObject $contextValue, |
|
176
|
|
|
$rootValue = null, |
|
177
|
|
|
?array $variableValue = null, |
|
178
|
|
|
?string $operationName = null |
|
179
|
|
|
): ExecutorArgumentsEvent { |
|
180
|
86 |
|
EventDispatcherVersionHelper::dispatch( |
|
181
|
86 |
|
$this->dispatcher, |
|
182
|
86 |
|
new ExecutorContextEvent($contextValue), |
|
183
|
86 |
|
Events::EXECUTOR_CONTEXT |
|
184
|
|
|
); |
|
185
|
|
|
|
|
186
|
86 |
|
return EventDispatcherVersionHelper::dispatch( |
|
187
|
86 |
|
$this->dispatcher, |
|
188
|
86 |
|
ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName), |
|
189
|
86 |
|
Events::PRE_EXECUTOR |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
86 |
|
private function postExecute(ExecutionResult $result): ExecutionResult |
|
194
|
|
|
{ |
|
195
|
86 |
|
return EventDispatcherVersionHelper::dispatch( |
|
196
|
86 |
|
$this->dispatcher, |
|
197
|
86 |
|
new ExecutorResultEvent($result), |
|
198
|
86 |
|
Events::POST_EXECUTOR |
|
199
|
85 |
|
)->getResult(); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|