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\GraphQL; |
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 Symfony\Component\EventDispatcher\EventDispatcherInterface; |
24
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
25
|
|
|
|
26
|
|
|
class Executor |
27
|
|
|
{ |
28
|
|
|
const DEFAULT_EXECUTOR = 'GraphQL\\GraphQL::executeAndReturnResult'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Schema[] |
32
|
|
|
*/ |
33
|
|
|
private $schemas; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EventDispatcherInterface|null |
37
|
|
|
*/ |
38
|
|
|
private $dispatcher; |
39
|
|
|
|
40
|
|
|
/** @var bool */ |
41
|
|
|
private $throwException; |
42
|
|
|
|
43
|
|
|
/** @var ErrorHandler|null */ |
44
|
|
|
private $errorHandler; |
45
|
|
|
|
46
|
|
|
/** @var bool */ |
47
|
|
|
private $hasDebugInfo; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var callable |
51
|
|
|
*/ |
52
|
|
|
private $executor; |
53
|
|
|
|
54
|
13 |
|
public function __construct( |
55
|
|
|
EventDispatcherInterface $dispatcher = null, |
56
|
|
|
$throwException = false, |
57
|
|
|
ErrorHandler $errorHandler = null, |
58
|
|
|
$hasDebugInfo = false, |
59
|
|
|
callable $executor = null |
60
|
|
|
) |
61
|
|
|
{ |
62
|
13 |
|
$this->dispatcher = $dispatcher; |
63
|
13 |
|
$this->throwException = (bool) $throwException; |
64
|
13 |
|
$this->errorHandler = $errorHandler; |
65
|
13 |
|
$hasDebugInfo ? $this->enabledDebugInfo() : $this->disabledDebugInfo(); |
66
|
13 |
|
$this->executor = $executor; |
67
|
13 |
|
if (null === $this->executor) { |
68
|
5 |
|
$this->executor = self::DEFAULT_EXECUTOR; |
69
|
5 |
|
} |
70
|
|
|
|
71
|
13 |
|
} |
72
|
|
|
|
73
|
2 |
|
public function setExecutor(callable $executor) |
74
|
|
|
{ |
75
|
2 |
|
$this->executor = $executor; |
76
|
|
|
|
77
|
2 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
13 |
|
public function addSchema($name, Schema $schema) |
81
|
|
|
{ |
82
|
13 |
|
$this->schemas[$name] = $schema; |
83
|
|
|
|
84
|
13 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function enabledDebugInfo() |
88
|
|
|
{ |
89
|
1 |
|
$this->hasDebugInfo = true; |
90
|
|
|
|
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
13 |
|
public function disabledDebugInfo() |
95
|
|
|
{ |
96
|
13 |
|
$this->hasDebugInfo = false; |
97
|
|
|
|
98
|
13 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
44 |
|
public function hasDebugInfo() |
102
|
|
|
{ |
103
|
44 |
|
return $this->hasDebugInfo; |
104
|
|
|
} |
105
|
|
|
|
106
|
8 |
|
public function setMaxQueryDepth($maxQueryDepth) |
107
|
|
|
{ |
108
|
|
|
/** @var QueryDepth $queryDepth */ |
109
|
8 |
|
$queryDepth = DocumentValidator::getRule('QueryDepth'); |
110
|
8 |
|
$queryDepth->setMaxQueryDepth($maxQueryDepth); |
111
|
8 |
|
} |
112
|
|
|
|
113
|
8 |
|
public function setMaxQueryComplexity($maxQueryComplexity) |
114
|
|
|
{ |
115
|
|
|
/** @var QueryComplexity $queryComplexity */ |
116
|
8 |
|
$queryComplexity = DocumentValidator::getRule('QueryComplexity'); |
117
|
8 |
|
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity); |
118
|
8 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param bool $throwException |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
23 |
|
public function setThrowException($throwException) |
126
|
|
|
{ |
127
|
23 |
|
$this->throwException = (bool) $throwException; |
128
|
|
|
|
129
|
23 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
47 |
|
public function execute(array $data, array $context = [], $schemaName = null) |
133
|
|
|
{ |
134
|
47 |
|
if (null !== $this->dispatcher) { |
135
|
43 |
|
$event = new ExecutorContextEvent($context); |
136
|
43 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
137
|
43 |
|
$context = $event->getExecutorContext(); |
138
|
43 |
|
} |
139
|
|
|
|
140
|
47 |
|
$schema = $this->getSchema($schemaName); |
141
|
|
|
|
142
|
46 |
|
$startTime = microtime(true); |
143
|
46 |
|
$startMemoryUsage = memory_get_usage(true); |
144
|
|
|
|
145
|
46 |
|
$executionResult = call_user_func( |
146
|
46 |
|
$this->executor, |
147
|
46 |
|
$schema, |
148
|
46 |
|
isset($data[ParserInterface::PARAM_QUERY]) ? $data[ParserInterface::PARAM_QUERY] : null, |
149
|
46 |
|
$context, |
150
|
46 |
|
$context, |
151
|
46 |
|
$data[ParserInterface::PARAM_VARIABLES], |
152
|
46 |
|
isset($data[ParserInterface::PARAM_OPERATION_NAME]) ? $data[ParserInterface::PARAM_OPERATION_NAME] : null |
153
|
46 |
|
); |
154
|
|
|
|
155
|
46 |
|
if (!is_object($executionResult) || !$executionResult instanceof ExecutionResult) { |
156
|
2 |
|
throw new \RuntimeException(sprintf('Execution result should be an object instantiating "%s".', 'GraphQL\\Executor\\ExecutionResult')); |
157
|
|
|
} |
158
|
|
|
|
159
|
44 |
|
if ($this->hasDebugInfo()) { |
160
|
1 |
|
$executionResult->extensions['debug'] = [ |
161
|
1 |
|
'executionTime' => sprintf('%d ms', round(microtime(true) - $startTime, 1) * 1000), |
162
|
1 |
|
'memoryUsage' => sprintf('%.2F MiB', (memory_get_usage(true) - $startMemoryUsage) / 1024 / 1024), |
163
|
|
|
]; |
164
|
1 |
|
} |
165
|
|
|
|
166
|
44 |
|
if (null !== $this->errorHandler) { |
167
|
42 |
|
$this->errorHandler->handleErrors($executionResult, $this->throwException); |
168
|
42 |
|
} |
169
|
|
|
|
170
|
44 |
|
return $executionResult; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string|null $name |
175
|
|
|
* |
176
|
|
|
* @return Schema |
177
|
|
|
*/ |
178
|
48 |
|
public function getSchema($name = null) |
179
|
|
|
{ |
180
|
48 |
|
if (empty($this->schemas)) { |
181
|
1 |
|
throw new \RuntimeException('At least one schema should be declare.'); |
182
|
|
|
} |
183
|
|
|
|
184
|
47 |
|
if (null === $name) { |
185
|
44 |
|
$schema = array_values($this->schemas)[0]; |
186
|
44 |
|
} else { |
187
|
3 |
|
if (!isset($this->schemas[$name])) { |
188
|
1 |
|
throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name)); |
189
|
|
|
} |
190
|
2 |
|
$schema = $this->schemas[$name]; |
191
|
|
|
} |
192
|
|
|
|
193
|
46 |
|
return $schema; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|