1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Request; |
13
|
|
|
|
14
|
|
|
use GraphQL\Executor\ExecutionResult; |
15
|
|
|
use GraphQL\Executor\Promise\Promise; |
16
|
|
|
use GraphQL\Schema; |
17
|
|
|
use GraphQL\Validator\DocumentValidator; |
18
|
|
|
use GraphQL\Validator\Rules\QueryComplexity; |
19
|
|
|
use GraphQL\Validator\Rules\QueryDepth; |
20
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
21
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
22
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorContextEvent; |
23
|
|
|
use Overblog\GraphQLBundle\Executor\ExecutorInterface; |
24
|
|
|
use Overblog\GraphQLBundle\Executor\Promise\PromiseAdapterInterface; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
26
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
27
|
|
|
|
28
|
|
|
class Executor |
29
|
|
|
{ |
30
|
|
|
const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Schema[] |
34
|
|
|
*/ |
35
|
|
|
private $schemas; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var EventDispatcherInterface|null |
39
|
|
|
*/ |
40
|
|
|
private $dispatcher; |
41
|
|
|
|
42
|
|
|
/** @var bool */ |
43
|
|
|
private $throwException; |
44
|
|
|
|
45
|
|
|
/** @var ErrorHandler|null */ |
46
|
|
|
private $errorHandler; |
47
|
|
|
|
48
|
|
|
/** @var bool */ |
49
|
|
|
private $hasDebugInfo; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ExecutorInterface |
53
|
|
|
*/ |
54
|
|
|
private $executor; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var PromiseAdapterInterface |
58
|
|
|
*/ |
59
|
|
|
private $promiseAdapter; |
60
|
|
|
|
61
|
14 |
|
public function __construct( |
62
|
|
|
ExecutorInterface $executor, |
63
|
|
|
EventDispatcherInterface $dispatcher = null, |
64
|
|
|
$throwException = false, |
65
|
|
|
ErrorHandler $errorHandler = null, |
66
|
|
|
$hasDebugInfo = false, |
67
|
|
|
PromiseAdapterInterface $promiseAdapter = null |
68
|
|
|
) { |
69
|
14 |
|
$this->executor = $executor; |
70
|
14 |
|
$this->dispatcher = $dispatcher; |
71
|
14 |
|
$this->throwException = (bool) $throwException; |
72
|
14 |
|
$this->errorHandler = $errorHandler; |
73
|
14 |
|
$hasDebugInfo ? $this->enabledDebugInfo() : $this->disabledDebugInfo(); |
74
|
14 |
|
$this->promiseAdapter = $promiseAdapter; |
75
|
14 |
|
} |
76
|
|
|
|
77
|
2 |
|
public function setExecutor(ExecutorInterface $executor) |
78
|
|
|
{ |
79
|
2 |
|
$this->executor = $executor; |
80
|
|
|
|
81
|
2 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
14 |
|
public function addSchema($name, Schema $schema) |
85
|
|
|
{ |
86
|
14 |
|
$this->schemas[$name] = $schema; |
87
|
|
|
|
88
|
14 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function enabledDebugInfo() |
92
|
|
|
{ |
93
|
1 |
|
$this->hasDebugInfo = true; |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
14 |
|
public function disabledDebugInfo() |
99
|
|
|
{ |
100
|
14 |
|
$this->hasDebugInfo = false; |
101
|
|
|
|
102
|
14 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
46 |
|
public function hasDebugInfo() |
106
|
|
|
{ |
107
|
46 |
|
return $this->hasDebugInfo; |
108
|
|
|
} |
109
|
|
|
|
110
|
9 |
|
public function setMaxQueryDepth($maxQueryDepth) |
111
|
|
|
{ |
112
|
|
|
/** @var QueryDepth $queryDepth */ |
113
|
9 |
|
$queryDepth = DocumentValidator::getRule('QueryDepth'); |
114
|
9 |
|
$queryDepth->setMaxQueryDepth($maxQueryDepth); |
115
|
9 |
|
} |
116
|
|
|
|
117
|
9 |
|
public function setMaxQueryComplexity($maxQueryComplexity) |
118
|
|
|
{ |
119
|
|
|
/** @var QueryComplexity $queryComplexity */ |
120
|
9 |
|
$queryComplexity = DocumentValidator::getRule('QueryComplexity'); |
121
|
9 |
|
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity); |
122
|
9 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param bool $throwException |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
25 |
|
public function setThrowException($throwException) |
130
|
|
|
{ |
131
|
25 |
|
$this->throwException = (bool) $throwException; |
132
|
|
|
|
133
|
25 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
49 |
|
public function execute(array $data, array $context = [], $schemaName = null) |
137
|
|
|
{ |
138
|
49 |
|
if (null !== $this->dispatcher) { |
139
|
45 |
|
$event = new ExecutorContextEvent($context); |
140
|
45 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
141
|
45 |
|
$context = $event->getExecutorContext(); |
142
|
45 |
|
} |
143
|
|
|
|
144
|
49 |
|
$schema = $this->getSchema($schemaName); |
145
|
|
|
|
146
|
48 |
|
$startTime = microtime(true); |
147
|
48 |
|
$startMemoryUsage = memory_get_usage(true); |
148
|
|
|
|
149
|
48 |
|
$this->executor->setPromiseAdapter($this->promiseAdapter); |
150
|
|
|
|
151
|
48 |
|
$result = $this->executor->execute( |
152
|
48 |
|
$schema, |
153
|
48 |
|
isset($data[ParserInterface::PARAM_QUERY]) ? $data[ParserInterface::PARAM_QUERY] : null, |
154
|
48 |
|
$context, |
155
|
48 |
|
$context, |
156
|
48 |
|
$data[ParserInterface::PARAM_VARIABLES], |
157
|
48 |
|
isset($data[ParserInterface::PARAM_OPERATION_NAME]) ? $data[ParserInterface::PARAM_OPERATION_NAME] : null |
158
|
48 |
|
); |
159
|
|
|
|
160
|
48 |
|
if (!is_object($result) || (!$result instanceof ExecutionResult && !$result instanceof Promise)) { |
161
|
2 |
|
throw new \RuntimeException( |
162
|
2 |
|
sprintf( |
163
|
2 |
|
'Execution result should be an object instantiating "%s" or "%s".', |
164
|
2 |
|
'GraphQL\\Executor\\ExecutionResult', |
165
|
|
|
'GraphQL\\Executor\\Promise\\Promise' |
166
|
2 |
|
) |
167
|
2 |
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
46 |
|
if ($this->promiseAdapter && $this->promiseAdapter->isThenable($result)) { |
171
|
10 |
|
$result = $this->promiseAdapter->wait($result); |
|
|
|
|
172
|
10 |
|
} |
173
|
|
|
|
174
|
46 |
|
return $this->prepareResult($result, $startTime, $startMemoryUsage); |
175
|
|
|
} |
176
|
|
|
|
177
|
46 |
|
private function prepareResult($result, $startTime, $startMemoryUsage) |
178
|
|
|
{ |
179
|
46 |
|
if ($this->hasDebugInfo()) { |
180
|
1 |
|
$result->extensions['debug'] = [ |
181
|
1 |
|
'executionTime' => sprintf('%d ms', round(microtime(true) - $startTime, 3) * 1000), |
182
|
1 |
|
'memoryUsage' => sprintf('%.2F MiB', (memory_get_usage(true) - $startMemoryUsage) / 1024 / 1024), |
183
|
|
|
]; |
184
|
1 |
|
} |
185
|
|
|
|
186
|
46 |
|
if (null !== $this->errorHandler) { |
187
|
44 |
|
$this->errorHandler->handleErrors($result, $this->throwException); |
188
|
44 |
|
} |
189
|
|
|
|
190
|
46 |
|
return $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string|null $name |
195
|
|
|
* |
196
|
|
|
* @return Schema |
197
|
|
|
*/ |
198
|
50 |
|
public function getSchema($name = null) |
199
|
|
|
{ |
200
|
50 |
|
if (empty($this->schemas)) { |
201
|
1 |
|
throw new \RuntimeException('At least one schema should be declare.'); |
202
|
|
|
} |
203
|
|
|
|
204
|
49 |
|
if (null === $name) { |
205
|
46 |
|
$schema = array_values($this->schemas)[0]; |
206
|
46 |
|
} else { |
207
|
3 |
|
if (!isset($this->schemas[$name])) { |
208
|
1 |
|
throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name)); |
209
|
|
|
} |
210
|
2 |
|
$schema = $this->schemas[$name]; |
211
|
|
|
} |
212
|
|
|
|
213
|
48 |
|
return $schema; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.