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
|
9 |
|
public function __construct(EventDispatcherInterface $dispatcher = null, $throwException = false, ErrorHandler $errorHandler = null) |
44
|
|
|
{ |
45
|
9 |
|
$this->dispatcher = $dispatcher; |
46
|
9 |
|
$this->throwException = (bool) $throwException; |
47
|
9 |
|
$this->errorHandler = $errorHandler; |
48
|
9 |
|
} |
49
|
|
|
|
50
|
8 |
|
public function addSchema($name, Schema $schema) |
51
|
|
|
{ |
52
|
8 |
|
$this->schemas[$name] = $schema; |
53
|
|
|
|
54
|
8 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
public function setMaxQueryDepth($maxQueryDepth) |
58
|
|
|
{ |
59
|
|
|
/** @var QueryDepth $queryDepth */ |
60
|
8 |
|
$queryDepth = DocumentValidator::getRule('QueryDepth'); |
61
|
8 |
|
$queryDepth->setMaxQueryDepth($maxQueryDepth); |
62
|
8 |
|
} |
63
|
|
|
|
64
|
8 |
|
public function setMaxQueryComplexity($maxQueryComplexity) |
65
|
|
|
{ |
66
|
|
|
/** @var QueryComplexity $queryComplexity */ |
67
|
8 |
|
$queryComplexity = DocumentValidator::getRule('QueryComplexity'); |
68
|
8 |
|
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity); |
69
|
8 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param bool $throwException |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
23 |
|
public function setThrowException($throwException) |
77
|
|
|
{ |
78
|
23 |
|
$this->throwException = (bool) $throwException; |
79
|
|
|
|
80
|
23 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
43 |
|
public function execute(array $data, array $context = [], $schemaName = null) |
84
|
|
|
{ |
85
|
43 |
|
if (null !== $this->dispatcher) { |
86
|
43 |
|
$event = new ExecutorContextEvent($context); |
87
|
43 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
88
|
43 |
|
$context = $event->getExecutorContext(); |
89
|
43 |
|
} |
90
|
|
|
|
91
|
43 |
|
$schema = $this->getSchema($schemaName); |
92
|
|
|
|
93
|
42 |
|
$executionResult = GraphQL::executeAndReturnResult( |
94
|
42 |
|
$schema, |
95
|
42 |
|
isset($data[ParserInterface::PARAM_QUERY]) ? $data[ParserInterface::PARAM_QUERY] : null, |
96
|
42 |
|
$context, |
97
|
42 |
|
$context, |
98
|
42 |
|
$data[ParserInterface::PARAM_VARIABLES], |
99
|
42 |
|
isset($data[ParserInterface::PARAM_OPERATION_NAME]) ? $data[ParserInterface::PARAM_OPERATION_NAME] : null |
100
|
42 |
|
); |
101
|
|
|
|
102
|
42 |
|
if (null !== $this->errorHandler) { |
103
|
42 |
|
$this->errorHandler->handleErrors($executionResult, $this->throwException); |
|
|
|
|
104
|
42 |
|
} |
105
|
|
|
|
106
|
42 |
|
return $executionResult; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string|null $name |
111
|
|
|
* |
112
|
|
|
* @return Schema |
113
|
|
|
*/ |
114
|
44 |
|
public function getSchema($name = null) |
115
|
|
|
{ |
116
|
44 |
|
if (empty($this->schemas)) { |
117
|
1 |
|
throw new \RuntimeException('At least one schema should be declare.'); |
118
|
|
|
} |
119
|
|
|
|
120
|
43 |
|
if (null === $name) { |
121
|
40 |
|
$schema = array_values($this->schemas)[0]; |
122
|
40 |
|
} else { |
123
|
3 |
|
if (!isset($this->schemas[$name])) { |
124
|
1 |
|
throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name)); |
125
|
|
|
} |
126
|
2 |
|
$schema = $this->schemas[$name]; |
127
|
|
|
} |
128
|
|
|
|
129
|
42 |
|
return $schema; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.