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