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