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\TypeLoaderInterface; |
15
|
|
|
use Railt\Events\DispatcherInterface; |
16
|
|
|
use Railt\Http\RequestInterface; |
17
|
|
|
use Railt\Http\Response; |
18
|
|
|
use Railt\Http\ResponseInterface; |
19
|
|
|
use Railt\Reflection\Abstraction\DocumentTypeInterface; |
20
|
|
|
use Railt\Adapters\AdapterInterface; |
21
|
|
|
use Railt\Routing\Router; |
22
|
|
|
use Railt\Adapters\Webonyx\Builder\SchemaTypeBuilder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Adapter |
26
|
|
|
* @package Railt\Adapters\Webonyx |
27
|
|
|
*/ |
28
|
|
|
class Adapter implements AdapterInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var DocumentTypeInterface |
32
|
|
|
*/ |
33
|
|
|
private $document; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var DispatcherInterface |
37
|
|
|
*/ |
38
|
|
|
private $events; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Router |
42
|
|
|
*/ |
43
|
|
|
private $router; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Loader|TypeLoaderInterface |
47
|
|
|
*/ |
48
|
|
|
private $loader; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Webonyx constructor. |
52
|
|
|
* @param DocumentTypeInterface $document |
53
|
|
|
* @param DispatcherInterface $events |
54
|
|
|
* @param Router $router |
55
|
|
|
*/ |
56
|
|
|
public function __construct(DocumentTypeInterface $document, DispatcherInterface $events, Router $router) |
57
|
|
|
{ |
58
|
|
|
$this->document = $document; |
59
|
|
|
$this->events = $events; |
60
|
|
|
$this->router = $router; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Router |
65
|
|
|
*/ |
66
|
|
|
public function getRouter(): Router |
67
|
|
|
{ |
68
|
|
|
return $this->router; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return DispatcherInterface |
73
|
|
|
*/ |
74
|
|
|
public function getEvents(): DispatcherInterface |
75
|
|
|
{ |
76
|
|
|
return $this->events; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return TypeLoaderInterface |
81
|
|
|
*/ |
82
|
|
|
public function getLoader(): TypeLoaderInterface |
83
|
|
|
{ |
84
|
|
|
if ($this->loader === null) { |
85
|
|
|
$this->loader = new Loader($this->document, $this); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->loader; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public static function isSupported(): bool |
96
|
|
|
{ |
97
|
|
|
return class_exists(GraphQL::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param RequestInterface $request |
102
|
|
|
* @return Response|ResponseInterface |
103
|
|
|
* @throws \LogicException |
104
|
|
|
* @throws \GraphQL\Error\InvariantViolation |
105
|
|
|
*/ |
106
|
|
|
public function request(RequestInterface $request): ResponseInterface |
107
|
|
|
{ |
108
|
|
|
$schema = $this->buildSchema(); |
109
|
|
|
|
110
|
|
|
$value = $this->executeSchema($request, $schema); |
111
|
|
|
|
112
|
|
|
return new Response((array)($value['data'] ?? []), (array)($value['errors'] ?? [])); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Schema |
117
|
|
|
* @throws \LogicException |
118
|
|
|
*/ |
119
|
|
|
private function buildSchema(): Schema |
120
|
|
|
{ |
121
|
|
|
$schema = $this->document->getSchema(); |
122
|
|
|
|
123
|
|
|
return $this->getLoader()->make($schema, SchemaTypeBuilder::class); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param RequestInterface $request |
128
|
|
|
* @param Schema $schema |
129
|
|
|
* @return array |
130
|
|
|
* @throws \GraphQL\Error\InvariantViolation |
131
|
|
|
*/ |
132
|
|
|
private function executeSchema(RequestInterface $request, Schema $schema): array |
133
|
|
|
{ |
134
|
|
|
return GraphQL::execute( |
135
|
|
|
$schema, |
136
|
|
|
$request->getQuery(), |
137
|
|
|
null, |
138
|
|
|
null, |
139
|
|
|
$request->getVariables(), |
140
|
|
|
$request->getOperation() |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|