1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Portiny\GraphQL\GraphQL; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\Debug; |
8
|
|
|
use GraphQL\Executor\Promise\Promise; |
9
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
10
|
|
|
use GraphQL\GraphQL; |
11
|
|
|
use GraphQL\Type\Definition\ObjectType; |
12
|
|
|
use GraphQL\Type\Schema; |
13
|
|
|
use Portiny\GraphQL\Contract\Http\Request\RequestParserInterface; |
14
|
|
|
use Portiny\GraphQL\Contract\Provider\MutationFieldsProviderInterface; |
15
|
|
|
use Portiny\GraphQL\Contract\Provider\QueryFieldsProviderInterface; |
16
|
|
|
use Throwable; |
17
|
|
|
use Tracy\Debugger; |
18
|
|
|
use Tracy\ILogger; |
19
|
|
|
|
20
|
|
|
final class RequestProcessor |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var MutationFieldsProviderInterface |
24
|
|
|
*/ |
25
|
|
|
private $mutationFieldsProvider; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var QueryFieldsProviderInterface |
29
|
|
|
*/ |
30
|
|
|
private $queryFieldsProvider; |
31
|
|
|
|
32
|
2 |
|
public function __construct( |
33
|
|
|
MutationFieldsProviderInterface $mutationFieldsProvider, |
34
|
|
|
QueryFieldsProviderInterface $queryFieldsProvider |
35
|
|
|
) { |
36
|
2 |
|
$this->mutationFieldsProvider = $mutationFieldsProvider; |
37
|
2 |
|
$this->queryFieldsProvider = $queryFieldsProvider; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed|null $context |
42
|
|
|
* @param array|null $allowedQueries |
43
|
|
|
* @param array|null $allowedMutations |
44
|
|
|
*/ |
45
|
1 |
|
public function process( |
46
|
|
|
RequestParserInterface $requestParser, |
47
|
|
|
array $rootValue = [], |
48
|
|
|
$context = NULL, |
49
|
|
|
?array $allowedQueries = NULL, |
50
|
|
|
?array $allowedMutations = NULL, |
51
|
|
|
?ILogger $logger = NULL |
52
|
|
|
): array { |
53
|
|
|
try { |
54
|
1 |
|
$result = GraphQL::executeQuery( |
55
|
1 |
|
$this->createSchema($allowedQueries, $allowedMutations), |
56
|
1 |
|
$requestParser->getQuery(), |
57
|
1 |
|
$rootValue, |
58
|
1 |
|
$context, |
59
|
1 |
|
$requestParser->getVariables() |
60
|
|
|
); |
61
|
|
|
|
62
|
1 |
|
$output = $result->toArray($this->isDebug()); |
63
|
|
|
} catch (Throwable $exception) { |
64
|
|
|
if ($logger) { |
65
|
|
|
$logger->log($exception); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$output = [ |
69
|
|
|
'error' => [ |
70
|
|
|
'message' => $exception->getMessage(), |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $output; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed|null $context |
80
|
|
|
* @param array|null $allowedQueries |
81
|
|
|
* @param array|null $allowedMutations |
82
|
|
|
*/ |
83
|
|
|
public function processViaPromise( |
84
|
|
|
PromiseAdapter $promiseAdapter, |
85
|
|
|
RequestParserInterface $requestParser, |
86
|
|
|
array $rootValue = [], |
87
|
|
|
$context = NULL, |
88
|
|
|
?array $allowedQueries = NULL, |
89
|
|
|
?array $allowedMutations = NULL, |
90
|
|
|
?ILogger $logger = NULL |
91
|
|
|
): Promise { |
92
|
|
|
try { |
93
|
|
|
return GraphQL::promiseToExecute( |
94
|
|
|
$promiseAdapter, |
95
|
|
|
$this->createSchema($allowedQueries, $allowedMutations), |
96
|
|
|
$requestParser->getQuery(), |
97
|
|
|
$rootValue, |
98
|
|
|
$context, |
99
|
|
|
$requestParser->getVariables() |
100
|
|
|
); |
101
|
|
|
} catch (Throwable $exception) { |
102
|
|
|
if ($logger) { |
103
|
|
|
$logger->log($exception); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $promiseAdapter->createRejected($exception); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
private function createSchema(?array $allowedQueries = NULL, ?array $allowedMutations = NULL): Schema |
111
|
|
|
{ |
112
|
|
|
$configuration = [ |
113
|
1 |
|
'query' => $this->createQueryObject($allowedQueries), |
114
|
|
|
]; |
115
|
|
|
|
116
|
1 |
|
$mutationObject = $this->createMutationObject($allowedMutations); |
117
|
1 |
|
if ($mutationObject->getFields()) { |
118
|
1 |
|
$configuration['mutation'] = $mutationObject; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return new Schema($configuration); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
private function isDebug(): int |
125
|
|
|
{ |
126
|
1 |
|
return ! Debugger::$productionMode ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : 0; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
private function createQueryObject(?array $allowedQueries = NULL): ObjectType |
130
|
|
|
{ |
131
|
1 |
|
return new ObjectType([ |
132
|
1 |
|
'name' => 'Query', |
133
|
1 |
|
'fields' => $this->queryFieldsProvider->convertFieldsToArray($allowedQueries), |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
private function createMutationObject(?array $allowedMutations = NULL): ObjectType |
138
|
|
|
{ |
139
|
1 |
|
return new ObjectType([ |
140
|
1 |
|
'name' => 'Mutation', |
141
|
1 |
|
'fields' => $this->mutationFieldsProvider->convertFieldsToArray($allowedMutations), |
142
|
|
|
]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|