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