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