1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\Handler\CallableMiddleware; |
6
|
|
|
use Digia\GraphQL\Error\Handler\ErrorHandler; |
7
|
|
|
use Digia\GraphQL\Error\Handler\ErrorHandlerInterface; |
8
|
|
|
use Digia\GraphQL\Error\InvariantException; |
9
|
|
|
use Digia\GraphQL\Execution\ExecutionResult; |
10
|
|
|
use Digia\GraphQL\Language\Node\DocumentNode; |
11
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
12
|
|
|
use Digia\GraphQL\Language\Node\TypeNodeInterface; |
13
|
|
|
use Digia\GraphQL\Language\Node\ValueNodeInterface; |
14
|
|
|
use Digia\GraphQL\Language\Source; |
15
|
|
|
use Digia\GraphQL\Language\StringSourceBuilder; |
16
|
|
|
use Digia\GraphQL\Language\SyntaxErrorException; |
17
|
|
|
use Digia\GraphQL\Schema\Resolver\ResolverRegistryInterface; |
18
|
|
|
use Digia\GraphQL\Schema\Schema; |
19
|
|
|
use Digia\GraphQL\Schema\Validation\SchemaValidationException; |
20
|
|
|
use Digia\GraphQL\Validation\ValidationException; |
21
|
|
|
use React\Promise\PromiseInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string|Source $source |
25
|
|
|
* @param array|ResolverRegistryInterface $resolverRegistry |
26
|
|
|
* @param array $options |
27
|
|
|
* @return Schema |
28
|
|
|
* @throws InvariantException |
29
|
|
|
*/ |
30
|
|
|
function buildSchema($source, $resolverRegistry = [], array $options = []): Schema |
31
|
|
|
{ |
32
|
|
|
if (\is_string($source)) { |
33
|
|
|
$source = (new StringSourceBuilder($source))->build(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return GraphQL::buildSchema($source, $resolverRegistry, $options); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Schema $schema |
41
|
|
|
* @param string|Source $source |
42
|
|
|
* @param array|ResolverRegistryInterface $resolverRegistry |
43
|
|
|
* @param array $options |
44
|
|
|
* @return Schema |
45
|
|
|
* @throws InvariantException |
46
|
|
|
*/ |
47
|
|
|
function extendSchema(Schema $schema, $source, $resolverRegistry = [], array $options = []): Schema |
48
|
|
|
{ |
49
|
|
|
if (\is_string($source)) { |
50
|
|
|
$source = (new StringSourceBuilder($source))->build(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return GraphQL::extendSchema($schema, $source, $resolverRegistry, $options); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Schema $schema |
58
|
|
|
* @return SchemaValidationException[] |
59
|
|
|
*/ |
60
|
|
|
function validateSchema(Schema $schema): array |
61
|
|
|
{ |
62
|
|
|
return GraphQL::validateSchema($schema); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string|Source $source |
67
|
|
|
* @param array $options |
68
|
|
|
* @return DocumentNode |
69
|
|
|
* @throws InvariantException |
70
|
|
|
* @throws SyntaxErrorException |
71
|
|
|
*/ |
72
|
|
|
function parse($source, array $options = []): DocumentNode |
73
|
|
|
{ |
74
|
|
|
if (\is_string($source)) { |
75
|
|
|
$source = (new StringSourceBuilder($source))->build(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return GraphQL::parse($source, $options); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string|Source $source |
83
|
|
|
* @param array $options |
84
|
|
|
* @return ValueNodeInterface |
85
|
|
|
* @throws InvariantException |
86
|
|
|
* @throws SyntaxErrorException |
87
|
|
|
*/ |
88
|
|
|
function parseValue($source, array $options = []): ValueNodeInterface |
89
|
|
|
{ |
90
|
|
|
if (\is_string($source)) { |
91
|
|
|
$source = (new StringSourceBuilder($source))->build(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return GraphQL::parseValue($source, $options); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string|Source $source |
99
|
|
|
* @param array $options |
100
|
|
|
* @return TypeNodeInterface |
101
|
|
|
* @throws InvariantException |
102
|
|
|
* @throws SyntaxErrorException |
103
|
|
|
*/ |
104
|
|
|
function parseType($source, array $options = []): TypeNodeInterface |
105
|
|
|
{ |
106
|
|
|
if (\is_string($source)) { |
107
|
|
|
$source = (new StringSourceBuilder($source))->build(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return GraphQL::parseType($source, $options); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Schema $schema |
115
|
|
|
* @param DocumentNode $document |
116
|
|
|
* @return array|ValidationException[] |
117
|
|
|
*/ |
118
|
|
|
function validate(Schema $schema, DocumentNode $document): array |
119
|
|
|
{ |
120
|
|
|
return GraphQL::validate($schema, $document); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Schema $schema |
125
|
|
|
* @param DocumentNode $document |
126
|
|
|
* @param mixed|null $rootValue |
127
|
|
|
* @param mixed|null $contextValue |
128
|
|
|
* @param array $variableValues |
129
|
|
|
* @param mixed|null $operationName |
130
|
|
|
* @param callable|null $fieldResolver |
131
|
|
|
* @param ErrorHandlerInterface|null $errorHandler |
132
|
|
|
* @return ExecutionResult |
133
|
|
|
*/ |
134
|
|
|
function execute( |
135
|
|
|
Schema $schema, |
136
|
|
|
DocumentNode $document, |
137
|
|
|
$rootValue = null, |
138
|
|
|
$contextValue = null, |
139
|
|
|
array $variableValues = [], |
140
|
|
|
$operationName = null, |
141
|
|
|
callable $fieldResolver = null, |
142
|
|
|
$errorHandler = null |
143
|
|
|
): ExecutionResult { |
144
|
|
|
return GraphQL::execute( |
145
|
|
|
$schema, |
146
|
|
|
$document, |
147
|
|
|
$rootValue, |
148
|
|
|
$contextValue, |
149
|
|
|
$variableValues, |
150
|
|
|
$operationName, |
151
|
|
|
$fieldResolver, |
152
|
|
|
$errorHandler |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param Schema $schema |
158
|
|
|
* @param DocumentNode $document |
159
|
|
|
* @param mixed|null $rootValue |
160
|
|
|
* @param mixed|null $contextValue |
161
|
|
|
* @param array $variableValues |
162
|
|
|
* @param mixed|null $operationName |
163
|
|
|
* @param callable|null $fieldResolver |
164
|
|
|
* @param ErrorHandlerInterface|null $errorHandler |
165
|
|
|
* @return PromiseInterface<ExecutionResult> |
166
|
|
|
*/ |
167
|
|
|
function promiseExecute( |
168
|
|
|
Schema $schema, |
169
|
|
|
DocumentNode $document, |
170
|
|
|
$rootValue = null, |
171
|
|
|
$contextValue = null, |
172
|
|
|
array $variableValues = [], |
173
|
|
|
$operationName = null, |
174
|
|
|
callable $fieldResolver = null, |
175
|
|
|
$errorHandler = null |
176
|
|
|
): PromiseInterface { |
177
|
|
|
return GraphQL::promiseExecute( |
178
|
|
|
$schema, |
179
|
|
|
$document, |
180
|
|
|
$rootValue, |
181
|
|
|
$contextValue, |
182
|
|
|
$variableValues, |
183
|
|
|
$operationName, |
184
|
|
|
$fieldResolver, |
185
|
|
|
$errorHandler |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param NodeInterface $node |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
function printNode(NodeInterface $node): string |
194
|
|
|
{ |
195
|
|
|
return GraphQL::print($node); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param Schema $schema |
200
|
|
|
* @param string $source |
201
|
|
|
* @param mixed $rootValue |
202
|
|
|
* @param mixed $contextValue |
203
|
|
|
* @param array $variableValues |
204
|
|
|
* @param string|null $operationName |
205
|
|
|
* @param callable|null $fieldResolver |
206
|
|
|
* @param ErrorHandlerInterface|callable|null $errorHandler |
207
|
|
|
* @return array |
208
|
|
|
* @throws InvariantException |
209
|
|
|
* @throws SyntaxErrorException |
210
|
|
|
*/ |
211
|
|
|
function graphql( |
212
|
|
|
Schema $schema, |
213
|
|
|
string $source, |
214
|
|
|
$rootValue = null, |
215
|
|
|
$contextValue = null, |
216
|
|
|
array $variableValues = [], |
217
|
|
|
?string $operationName = null, |
218
|
|
|
?callable $fieldResolver = null, |
219
|
|
|
$errorHandler = null |
220
|
|
|
): array { |
221
|
|
|
if (\is_callable($errorHandler)) { |
222
|
|
|
$errorHandler = new ErrorHandler([new CallableMiddleware($errorHandler)]); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$result = GraphQL::process( |
226
|
|
|
$schema, |
227
|
|
|
$source, |
228
|
|
|
$rootValue, |
229
|
|
|
$contextValue, |
230
|
|
|
$variableValues, |
231
|
|
|
$operationName, |
232
|
|
|
$fieldResolver, |
233
|
|
|
$errorHandler |
|
|
|
|
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
return $result->toArray(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param Schema $schema |
241
|
|
|
* @param string $source |
242
|
|
|
* @param mixed $rootValue |
243
|
|
|
* @param mixed $contextValue |
244
|
|
|
* @param array $variableValues |
245
|
|
|
* @param string|null $operationName |
246
|
|
|
* @param callable|null $fieldResolver |
247
|
|
|
* @param ErrorHandlerInterface|callable|null $errorHandler |
248
|
|
|
* @return PromiseInterface<array> |
249
|
|
|
* @throws InvariantException |
250
|
|
|
* @throws SyntaxErrorException |
251
|
|
|
*/ |
252
|
|
|
function graphqlAsync( |
253
|
|
|
Schema $schema, |
254
|
|
|
string $source, |
255
|
|
|
$rootValue = null, |
256
|
|
|
$contextValue = null, |
257
|
|
|
array $variableValues = [], |
258
|
|
|
?string $operationName = null, |
259
|
|
|
?callable $fieldResolver = null, |
260
|
|
|
$errorHandler = null |
261
|
|
|
): PromiseInterface { |
262
|
|
|
if (\is_callable($errorHandler)) { |
263
|
|
|
$errorHandler = new ErrorHandler([new CallableMiddleware($errorHandler)]); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$result = GraphQL::processAsync( |
267
|
|
|
$schema, |
268
|
|
|
$source, |
269
|
|
|
$rootValue, |
270
|
|
|
$contextValue, |
271
|
|
|
$variableValues, |
272
|
|
|
$operationName, |
273
|
|
|
$fieldResolver, |
274
|
|
|
$errorHandler |
|
|
|
|
275
|
|
|
); |
276
|
|
|
|
277
|
|
|
return $result->then(function (ExecutionResult $result) { |
278
|
|
|
return $result->toArray(); |
279
|
|
|
}); |
280
|
|
|
} |
281
|
|
|
|