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\Language\Source; |
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
|
|
|
|
25
|
|
|
class Executor |
26
|
|
|
{ |
27
|
|
|
private $schema; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EventDispatcherInterface|null |
31
|
|
|
*/ |
32
|
|
|
private $dispatcher; |
33
|
|
|
|
34
|
|
|
/** @var bool */ |
35
|
|
|
private $throwException; |
36
|
|
|
|
37
|
|
|
/** @var ErrorHandler|null */ |
38
|
|
|
private $errorHandler; |
39
|
|
|
|
40
|
36 |
|
public function __construct(Schema $schema, EventDispatcherInterface $dispatcher = null, $throwException = false, ErrorHandler $errorHandler = null) |
41
|
|
|
{ |
42
|
36 |
|
$this->schema = $schema; |
43
|
36 |
|
$this->dispatcher = $dispatcher; |
44
|
36 |
|
$this->throwException = (bool) $throwException; |
45
|
36 |
|
$this->errorHandler = $errorHandler; |
46
|
36 |
|
} |
47
|
|
|
|
48
|
36 |
|
public function setMaxQueryDepth($maxQueryDepth) |
49
|
|
|
{ |
50
|
|
|
/** @var QueryDepth $queryDepth */ |
51
|
36 |
|
$queryDepth = DocumentValidator::getRule('QueryDepth'); |
52
|
36 |
|
$queryDepth->setMaxQueryDepth($maxQueryDepth); |
53
|
36 |
|
} |
54
|
|
|
|
55
|
36 |
|
public function setMaxQueryComplexity($maxQueryComplexity) |
56
|
|
|
{ |
57
|
|
|
/** @var QueryComplexity $queryComplexity */ |
58
|
36 |
|
$queryComplexity = DocumentValidator::getRule('QueryComplexity'); |
59
|
36 |
|
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity); |
60
|
36 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param bool $throwException |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
22 |
|
public function setThrowException($throwException) |
68
|
|
|
{ |
69
|
22 |
|
$this->throwException = (bool) $throwException; |
70
|
|
|
|
71
|
22 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
36 |
|
public function execute(array $data, array $context = []) |
75
|
|
|
{ |
76
|
36 |
|
if (null !== $this->dispatcher) { |
77
|
36 |
|
$event = new ExecutorContextEvent($context); |
78
|
36 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
79
|
36 |
|
$context = $event->getExecutorContext(); |
80
|
36 |
|
} |
81
|
|
|
|
82
|
36 |
|
$executionResult = GraphQL::executeAndReturnResult( |
83
|
36 |
|
$this->schema, |
84
|
36 |
|
isset($data['query']) ? $data['query'] : null, |
85
|
36 |
|
$context, |
86
|
36 |
|
$data['variables'], |
87
|
36 |
|
$data['operationName'] |
88
|
36 |
|
); |
89
|
|
|
|
90
|
36 |
|
if (null !== $this->errorHandler) { |
91
|
36 |
|
$this->errorHandler->handleErrors($executionResult, $this->throwException); |
92
|
36 |
|
} |
93
|
|
|
|
94
|
36 |
|
return $executionResult; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|