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