-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathExecutor.php
219 lines (184 loc) · 6.95 KB
/
Executor.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace Overblog\GraphQLBundle\Request;
use ArrayObject;
use Closure;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Type\Schema;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\Rules\QueryDepth;
use Overblog\GraphQLBundle\Definition\Type\ExtensibleSchema;
use Overblog\GraphQLBundle\Event\Events;
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent;
use Overblog\GraphQLBundle\Event\ExecutorContextEvent;
use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
use Overblog\GraphQLBundle\Executor\ExecutorInterface;
use RuntimeException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\Service\ResetInterface;
use function array_keys;
use function sprintf;
class Executor implements ResetInterface
{
public const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter';
/**
* @var array<Closure>
*/
private array $schemaBuilders = [];
/**
* @var array<Schema>
*/
private array $schemas = [];
private EventDispatcherInterface $dispatcher;
private PromiseAdapter $promiseAdapter;
private ExecutorInterface $executor;
/**
* @var callable|null
*/
private $defaultFieldResolver;
public function __construct(
ExecutorInterface $executor,
PromiseAdapter $promiseAdapter,
EventDispatcherInterface $dispatcher,
callable $defaultFieldResolver = null
) {
$this->executor = $executor;
$this->promiseAdapter = $promiseAdapter;
$this->dispatcher = $dispatcher;
$this->defaultFieldResolver = $defaultFieldResolver;
}
public function setExecutor(ExecutorInterface $executor): self
{
$this->executor = $executor;
return $this;
}
public function addSchemaBuilder(string $name, Closure $builder): self
{
$this->schemaBuilders[$name] = $builder;
return $this;
}
public function addSchema(string $name, Schema $schema): self
{
$this->schemas[$name] = $schema;
return $this;
}
public function getSchema(string $name = null): Schema
{
if (empty($this->schemaBuilders) && empty($this->schemas)) {
throw new RuntimeException('At least one schema should be declare.');
}
if (null === $name) {
$name = isset($this->schemas['default']) ? 'default' : array_key_first($this->schemas);
}
if (null === $name) {
$name = isset($this->schemaBuilders['default']) ? 'default' : array_key_first($this->schemaBuilders);
}
if (isset($this->schemas[$name])) {
$schema = $this->schemas[$name];
} elseif (isset($this->schemaBuilders[$name])) {
$schema = call_user_func($this->schemaBuilders[$name]);
$this->addSchema((string) $name, $schema);
} else {
throw new NotFoundHttpException(sprintf('Could not find "%s" schema.', $name));
}
return $schema;
}
public function reset(): void
{
// Remove only ExtensibleSchema and isResettable
$this->schemas = array_filter(
$this->schemas,
fn (Schema $schema) => $schema instanceof ExtensibleSchema && !$schema->isResettable()
);
}
public function getSchemasNames(): array
{
return array_merge(array_keys($this->schemaBuilders), array_keys($this->schemas));
}
public function setMaxQueryDepth(int $maxQueryDepth): void
{
/** @var QueryDepth $queryDepth */
$queryDepth = DocumentValidator::getRule(QueryDepth::class);
$queryDepth->setMaxQueryDepth($maxQueryDepth);
}
public function setMaxQueryComplexity(int $maxQueryComplexity): void
{
/** @var QueryComplexity $queryComplexity */
$queryComplexity = DocumentValidator::getRule(QueryComplexity::class);
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
}
public function enableIntrospectionQuery(): void
{
DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED));
}
public function disableIntrospectionQuery(): void
{
DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::ENABLED));
}
/**
* @param array|ArrayObject|object|null $rootValue
*/
public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
{
$schema = $this->getSchema($schemaName);
/** @var string $schemaName */
$schemaName = array_search($schema, $this->schemas);
$executorArgumentsEvent = $this->preExecute(
$schemaName,
$schema,
$request[ParserInterface::PARAM_QUERY] ?? null,
new ArrayObject(),
$rootValue,
$request[ParserInterface::PARAM_VARIABLES],
$request[ParserInterface::PARAM_OPERATION_NAME] ?? null
);
$executorArgumentsEvent->getSchema()->processExtensions();
$result = $this->executor->execute(
$this->promiseAdapter,
$executorArgumentsEvent->getSchema(),
$executorArgumentsEvent->getRequestString(),
$executorArgumentsEvent->getRootValue(),
$executorArgumentsEvent->getContextValue(),
$executorArgumentsEvent->getVariableValue(),
$executorArgumentsEvent->getOperationName(),
$this->defaultFieldResolver
);
$result = $this->postExecute($result, $executorArgumentsEvent);
return $result;
}
/**
* @param mixed $rootValue
*/
private function preExecute(
string $schemaName,
Schema $schema,
string $requestString,
ArrayObject $contextValue,
$rootValue = null,
array $variableValue = null,
string $operationName = null
): ExecutorArgumentsEvent {
// @phpstan-ignore-next-line (only for Symfony 4.4)
$this->dispatcher->dispatch(new ExecutorContextEvent($contextValue), Events::EXECUTOR_CONTEXT);
/** @var ExecutorArgumentsEvent $object */
// @phpstan-ignore-next-line (only for Symfony 4.4)
$object = $this->dispatcher->dispatch(
// @phpstan-ignore-next-line
ExecutorArgumentsEvent::create($schemaName, $schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
Events::PRE_EXECUTOR
);
return $object;
}
private function postExecute(ExecutionResult $result, ExecutorArgumentsEvent $executorArguments): ExecutionResult
{
// @phpstan-ignore-next-line (only for Symfony 4.4)
return $this->dispatcher->dispatch(
new ExecutorResultEvent($result, $executorArguments),
Events::POST_EXECUTOR
)->getResult();
}
}