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\Error; |
15
|
|
|
use GraphQL\Executor\ExecutionResult; |
16
|
|
|
use GraphQL\Executor\Executor as GraphQLExecutor; |
17
|
|
|
use GraphQL\GraphQL; |
18
|
|
|
use GraphQL\Language\Parser as GraphQLParser; |
19
|
|
|
use GraphQL\Language\Source; |
20
|
|
|
use GraphQL\Schema; |
21
|
|
|
use GraphQL\Validator\DocumentValidator; |
22
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
23
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
24
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorContextEvent; |
25
|
|
|
use Overblog\GraphQLBundle\Request\Validator\Rule\QueryComplexity; |
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
27
|
|
|
|
28
|
|
|
class Executor |
29
|
|
|
{ |
30
|
|
|
private $schema; |
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 callable[] */ |
44
|
|
|
private $validationRules; |
45
|
|
|
|
46
|
36 |
|
public function __construct(Schema $schema, EventDispatcherInterface $dispatcher = null, $throwException = false, ErrorHandler $errorHandler = null) |
47
|
|
|
{ |
48
|
36 |
|
$this->schema = $schema; |
49
|
36 |
|
$this->dispatcher = $dispatcher; |
50
|
36 |
|
$this->throwException = (bool) $throwException; |
51
|
36 |
|
$this->errorHandler = $errorHandler; |
52
|
36 |
|
$this->validationRules = DocumentValidator::allRules(); |
53
|
36 |
|
} |
54
|
|
|
|
55
|
36 |
|
public function addValidatorRule(callable $validatorRule) |
56
|
|
|
{ |
57
|
36 |
|
$this->validationRules[] = $validatorRule; |
58
|
36 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param bool $throwException |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
23 |
|
public function setThrowException($throwException) |
66
|
1 |
|
{ |
67
|
23 |
|
$this->throwException = (bool) $throwException; |
68
|
|
|
|
69
|
23 |
|
return $this; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
36 |
|
public function execute(array $data, array $context = []) |
73
|
|
|
{ |
74
|
36 |
|
if (null !== $this->dispatcher) { |
75
|
36 |
|
$event = new ExecutorContextEvent($context); |
76
|
36 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
77
|
36 |
|
$context = $event->getExecutorContext(); |
78
|
36 |
|
} |
79
|
|
|
|
80
|
36 |
|
$executionResult = $this->executeAndReturnResult( |
81
|
36 |
|
$this->schema, |
82
|
36 |
|
isset($data['query']) ? $data['query'] : null, |
83
|
36 |
|
$context, |
84
|
36 |
|
$data['variables'], |
85
|
36 |
|
$data['operationName'] |
86
|
36 |
|
); |
87
|
|
|
|
88
|
36 |
|
if (null !== $this->errorHandler) { |
89
|
36 |
|
$this->errorHandler->handleErrors($executionResult, $this->throwException); |
90
|
36 |
|
} |
91
|
|
|
|
92
|
36 |
|
return $executionResult; |
93
|
|
|
} |
94
|
|
|
|
95
|
36 |
|
private function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null) |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
36 |
|
$source = new Source($requestString ?: '', 'GraphQL request'); |
99
|
36 |
|
$documentAST = GraphQLParser::parse($source); |
100
|
|
|
//todo set using service |
101
|
36 |
|
QueryComplexity::setRawVariableValues($variableValues); |
102
|
|
|
|
103
|
36 |
|
$validationErrors = DocumentValidator::validate($schema, $documentAST, $this->validationRules); |
104
|
|
|
|
105
|
36 |
|
if (!empty($validationErrors)) { |
106
|
2 |
|
return new ExecutionResult(null, $validationErrors); |
107
|
|
|
} |
108
|
|
|
|
109
|
34 |
|
return GraphQLExecutor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName); |
110
|
|
|
} catch (Error $e) { |
111
|
|
|
return new ExecutionResult(null, [$e]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|