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