1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Adapters\Webonyx; |
11
|
|
|
|
12
|
|
|
use GraphQL\GraphQL; |
13
|
|
|
use GraphQL\Schema; |
14
|
|
|
use Railt\Adapters\AdapterInterface; |
15
|
|
|
use Railt\Adapters\TypeLoaderInterface; |
16
|
|
|
use Railt\Adapters\Webonyx\Builder\SchemaTypeBuilder; |
17
|
|
|
use Railt\Container\ContainerInterface; |
18
|
|
|
use Railt\Events\DispatcherInterface; |
19
|
|
|
use Railt\Http\RequestInterface; |
20
|
|
|
use Railt\Http\Response; |
21
|
|
|
use Railt\Http\ResponseInterface; |
22
|
|
|
use Railt\Reflection\Abstraction\DocumentTypeInterface; |
23
|
|
|
use Railt\Routing\Router; |
24
|
|
|
use Railt\Support\Debuggable; |
25
|
|
|
use Railt\Support\DebuggableInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Adapter |
29
|
|
|
* |
30
|
|
|
* @package Railt\Adapters\Webonyx |
31
|
|
|
*/ |
32
|
|
|
class Adapter implements AdapterInterface, DebuggableInterface |
33
|
|
|
{ |
34
|
|
|
use Debuggable; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DocumentTypeInterface |
38
|
|
|
*/ |
39
|
|
|
private $document; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var DispatcherInterface |
43
|
|
|
*/ |
44
|
|
|
private $events; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Router |
48
|
|
|
*/ |
49
|
|
|
private $router; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Loader|TypeLoaderInterface |
53
|
|
|
*/ |
54
|
|
|
private $loader; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Webonyx constructor. |
58
|
|
|
* |
59
|
|
|
* @param DocumentTypeInterface $document |
60
|
|
|
* @param DispatcherInterface $events |
61
|
|
|
* @param Router $router |
62
|
|
|
*/ |
63
|
|
|
public function __construct(DocumentTypeInterface $document, DispatcherInterface $events, Router $router) |
64
|
|
|
{ |
65
|
|
|
$this->document = $document; |
66
|
|
|
$this->events = $events; |
67
|
|
|
$this->router = $router; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return ContainerInterface |
72
|
|
|
*/ |
73
|
|
|
public function getContainer(): ContainerInterface |
74
|
|
|
{ |
75
|
|
|
// TODO: Implement the receipt of the container directly; not through the Router. |
76
|
|
|
return $this->router->getContainer(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public static function isSupported(): bool |
83
|
|
|
{ |
84
|
|
|
return class_exists(GraphQL::class); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Router |
89
|
|
|
*/ |
90
|
|
|
public function getRouter(): Router |
91
|
|
|
{ |
92
|
|
|
return $this->router; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return DispatcherInterface |
97
|
|
|
*/ |
98
|
|
|
public function getEvents(): DispatcherInterface |
99
|
|
|
{ |
100
|
|
|
return $this->events; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param RequestInterface $request |
105
|
|
|
* @return Response|ResponseInterface |
106
|
|
|
* @throws \LogicException |
107
|
|
|
* @throws \GraphQL\Error\InvariantViolation |
108
|
|
|
*/ |
109
|
|
|
public function request(RequestInterface $request): ResponseInterface |
110
|
|
|
{ |
111
|
|
|
$schema = $this->buildSchema(); |
112
|
|
|
|
113
|
|
|
$value = $this->executeSchema($request, $schema); |
114
|
|
|
|
115
|
|
|
[$data, $errors] = [ |
|
|
|
|
116
|
|
|
(array)($value['data'] ?? []), |
117
|
|
|
(array)($value['errors'] ?? []), |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$response = new Response($data, $errors); |
121
|
|
|
$response->debugMode($this->debug); |
122
|
|
|
|
123
|
|
|
return $response; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Schema |
128
|
|
|
* @throws \LogicException |
129
|
|
|
*/ |
130
|
|
|
private function buildSchema(): Schema |
131
|
|
|
{ |
132
|
|
|
$schema = $this->document->getSchema(); |
133
|
|
|
|
134
|
|
|
return $this->getLoader()->make($schema, SchemaTypeBuilder::class); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return TypeLoaderInterface |
139
|
|
|
*/ |
140
|
|
|
public function getLoader(): TypeLoaderInterface |
141
|
|
|
{ |
142
|
|
|
if ($this->loader === null) { |
143
|
|
|
$this->loader = new Loader($this->document, $this); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->loader; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param RequestInterface $request |
151
|
|
|
* @param Schema $schema |
152
|
|
|
* @return array |
153
|
|
|
* @throws \GraphQL\Error\InvariantViolation |
154
|
|
|
*/ |
155
|
|
|
private function executeSchema(RequestInterface $request, Schema $schema): array |
156
|
|
|
{ |
157
|
|
|
return GraphQL::execute( |
158
|
|
|
$schema, |
159
|
|
|
$request->getQuery(), |
160
|
|
|
null, |
161
|
|
|
null, |
162
|
|
|
$request->getVariables(), |
163
|
|
|
$request->getOperation() |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.