|
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 Overblog\GraphQLBundle\Error\ErrorHandler; |
|
17
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
|
18
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorContextEvent; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
20
|
|
|
|
|
21
|
|
|
class Executor |
|
22
|
|
|
{ |
|
23
|
|
|
private $schema; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var EventDispatcherInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $dispatcher; |
|
29
|
|
|
|
|
30
|
|
|
/** @var bool */ |
|
31
|
|
|
private $throwException; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ErrorHandler */ |
|
34
|
|
|
private $errorHandler; |
|
35
|
|
|
|
|
36
|
26 |
|
public function __construct(Schema $schema, EventDispatcherInterface $dispatcher, $throwException, ErrorHandler $errorHandler) |
|
37
|
1 |
|
{ |
|
38
|
26 |
|
$this->schema = $schema; |
|
39
|
26 |
|
$this->dispatcher = $dispatcher; |
|
40
|
26 |
|
$this->throwException = (bool) $throwException; |
|
41
|
26 |
|
$this->errorHandler = $errorHandler; |
|
42
|
26 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param bool $throwException |
|
46
|
|
|
* |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
22 |
|
public function setThrowException($throwException) |
|
50
|
|
|
{ |
|
51
|
22 |
|
$this->throwException = (bool) $throwException; |
|
52
|
|
|
|
|
53
|
22 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
26 |
|
public function execute(array $data, array $context = []) |
|
57
|
|
|
{ |
|
58
|
26 |
|
$event = new ExecutorContextEvent($context); |
|
59
|
26 |
|
$this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event); |
|
60
|
|
|
|
|
61
|
26 |
|
$executionResult = GraphQL::executeAndReturnResult( |
|
62
|
26 |
|
$this->schema, |
|
63
|
26 |
|
isset($data['query']) ? $data['query'] : null, |
|
64
|
26 |
|
$event->getExecutorContext(), |
|
65
|
26 |
|
$data['variables'], |
|
66
|
26 |
|
$data['operationName'] |
|
67
|
26 |
|
); |
|
68
|
|
|
|
|
69
|
26 |
|
$this->errorHandler->handleErrors($executionResult, $this->throwException); |
|
70
|
|
|
|
|
71
|
26 |
|
return $executionResult; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|