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 Symfony\Component\EventDispatcher\EventDispatcherInterface; |
26
|
|
|
|
27
|
|
|
class Executor |
28
|
|
|
{ |
29
|
|
|
private $schema; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EventDispatcherInterface|null |
33
|
|
|
*/ |
34
|
|
|
private $dispatcher; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
private $throwException; |
38
|
|
|
|
39
|
|
|
/** @var ErrorHandler|null */ |
40
|
|
|
private $errorHandler; |
41
|
|
|
|
42
|
|
|
/** @var callable[] */ |
43
|
|
|
private $validationRules; |
44
|
|
|
|
45
|
27 |
|
public function __construct(Schema $schema, EventDispatcherInterface $dispatcher = null, $throwException = false, ErrorHandler $errorHandler = null) |
46
|
|
|
{ |
47
|
27 |
|
$this->schema = $schema; |
48
|
27 |
|
$this->dispatcher = $dispatcher; |
49
|
27 |
|
$this->throwException = (bool) $throwException; |
50
|
27 |
|
$this->errorHandler = $errorHandler; |
51
|
27 |
|
$this->validationRules = DocumentValidator::allRules(); |
52
|
27 |
|
} |
53
|
|
|
|
54
|
27 |
|
public function addValidatorRule(callable $validatorRule) |
55
|
1 |
|
{ |
56
|
27 |
|
$this->validationRules[] = $validatorRule; |
57
|
27 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param bool $throwException |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
23 |
|
public function setThrowException($throwException) |
65
|
|
|
{ |
66
|
23 |
|
$this->throwException = (bool) $throwException; |
67
|
|
|
|
68
|
23 |
|
return $this; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
27 |
|
public function execute(array $data, array $context = []) |
72
|
|
|
{ |
73
|
27 |
|
if (null !== $this->dispatcher) { |
74
|
27 |
|
$event = new ExecutorContextEvent($context); |
75
|
27 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
76
|
27 |
|
$context = $event->getExecutorContext(); |
77
|
27 |
|
} |
78
|
|
|
|
79
|
27 |
|
$executionResult = $this->executeAndReturnResult( |
80
|
27 |
|
$this->schema, |
81
|
27 |
|
isset($data['query']) ? $data['query'] : null, |
82
|
27 |
|
$context, |
83
|
27 |
|
$data['variables'], |
84
|
27 |
|
$data['operationName'] |
85
|
27 |
|
); |
86
|
|
|
|
87
|
27 |
|
if (null !== $this->errorHandler) { |
88
|
27 |
|
$this->errorHandler->handleErrors($executionResult, $this->throwException); |
89
|
27 |
|
} |
90
|
|
|
|
91
|
27 |
|
return $executionResult; |
92
|
|
|
} |
93
|
|
|
|
94
|
27 |
|
private function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null) |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
27 |
|
$source = new Source($requestString ?: '', 'GraphQL request'); |
98
|
27 |
|
$documentAST = GraphQLParser::parse($source); |
99
|
27 |
|
$validationErrors = DocumentValidator::validate($schema, $documentAST, $this->validationRules); |
100
|
|
|
|
101
|
27 |
|
if (!empty($validationErrors)) { |
102
|
1 |
|
return new ExecutionResult(null, $validationErrors); |
103
|
|
|
} |
104
|
|
|
|
105
|
26 |
|
return GraphQLExecutor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName); |
106
|
|
|
} catch (Error $e) { |
107
|
|
|
return new ExecutionResult(null, [$e]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|