|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\InvalidTypeException; |
|
6
|
|
|
use Digia\GraphQL\Error\InvariantException; |
|
7
|
|
|
use Digia\GraphQL\Type\Definition\AbstractTypeInterface; |
|
8
|
|
|
use Digia\GraphQL\Type\Definition\CompositeTypeInterface; |
|
9
|
|
|
use Digia\GraphQL\Type\Definition\Directive; |
|
10
|
|
|
use Digia\GraphQL\Type\Definition\EnumType; |
|
11
|
|
|
use Digia\GraphQL\Type\Definition\InputObjectType; |
|
12
|
|
|
use Digia\GraphQL\Type\Definition\InputTypeInterface; |
|
13
|
|
|
use Digia\GraphQL\Type\Definition\InterfaceType; |
|
14
|
|
|
use Digia\GraphQL\Type\Definition\LeafTypeInterface; |
|
15
|
|
|
use Digia\GraphQL\Type\Definition\ListType; |
|
16
|
|
|
use Digia\GraphQL\Type\Definition\NamedTypeInterface; |
|
17
|
|
|
use Digia\GraphQL\Type\Definition\NonNullType; |
|
18
|
|
|
use Digia\GraphQL\Type\Definition\ObjectType; |
|
19
|
|
|
use Digia\GraphQL\Type\Definition\OutputTypeInterface; |
|
20
|
|
|
use Digia\GraphQL\Type\Definition\ScalarType; |
|
21
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
|
22
|
|
|
use Digia\GraphQL\Type\Definition\UnionType; |
|
23
|
|
|
use Digia\GraphQL\Type\Definition\WrappingTypeInterface; |
|
24
|
|
|
use function Digia\GraphQL\Util\invariant; |
|
25
|
|
|
use function Digia\GraphQL\Util\toString; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param $thunk |
|
29
|
|
|
* @return null|array |
|
30
|
|
|
*/ |
|
31
|
|
|
function resolveThunk($thunk): ?array |
|
32
|
|
|
{ |
|
33
|
|
|
return \is_callable($thunk) ? $thunk() : $thunk; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param mixed $value |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
function isAssocArray($value): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (!\is_array($value)) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
if (empty($value)) { |
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
$keys = \array_keys($value); |
|
49
|
|
|
return $keys !== \array_keys($keys); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $type |
|
54
|
|
|
* @throws InvariantException |
|
55
|
|
|
*/ |
|
56
|
|
|
function assertType($type) |
|
57
|
|
|
{ |
|
58
|
|
|
invariant( |
|
59
|
|
|
$type instanceof TypeInterface, |
|
60
|
|
|
\sprintf('Expected %s to be a GraphQL type.', toString($type)) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param TypeInterface $type |
|
66
|
|
|
* @throws InvariantException |
|
67
|
|
|
*/ |
|
68
|
|
|
function assertScalarType(TypeInterface $type) |
|
69
|
|
|
{ |
|
70
|
|
|
invariant( |
|
71
|
|
|
$type instanceof ScalarType, |
|
72
|
|
|
\sprintf('Expected %s to be a GraphQL Scalar type.', toString($type)) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param TypeInterface $type |
|
78
|
|
|
* @throws InvariantException |
|
79
|
|
|
*/ |
|
80
|
|
|
function assertObjectType(TypeInterface $type) |
|
81
|
|
|
{ |
|
82
|
|
|
invariant( |
|
83
|
|
|
$type instanceof ObjectType, |
|
84
|
|
|
\sprintf('Expected %s to be a GraphQL Object type.', toString($type)) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param TypeInterface $type |
|
90
|
|
|
* @throws InvariantException |
|
91
|
|
|
*/ |
|
92
|
|
|
function assertInterfaceType(TypeInterface $type) |
|
93
|
|
|
{ |
|
94
|
|
|
invariant( |
|
95
|
|
|
$type instanceof InterfaceType, |
|
96
|
|
|
\sprintf('Expected %s to be a GraphQL Interface type.', toString($type)) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param TypeInterface $type |
|
102
|
|
|
* @throws InvariantException |
|
103
|
|
|
*/ |
|
104
|
|
|
function assertUnionType(TypeInterface $type) |
|
105
|
|
|
{ |
|
106
|
|
|
invariant( |
|
107
|
|
|
$type instanceof UnionType, |
|
108
|
|
|
\sprintf('Expected %s to be a GraphQL Union type.', toString($type)) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param TypeInterface $type |
|
114
|
|
|
* @throws InvariantException |
|
115
|
|
|
*/ |
|
116
|
|
|
function assertEnumType(TypeInterface $type) |
|
117
|
|
|
{ |
|
118
|
|
|
invariant( |
|
119
|
|
|
$type instanceof EnumType, |
|
120
|
|
|
\sprintf('Expected %s to be a GraphQL Enum type.', toString($type)) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param TypeInterface $type |
|
126
|
|
|
* @throws InvariantException |
|
127
|
|
|
*/ |
|
128
|
|
|
function assertInputObjectType(TypeInterface $type) |
|
129
|
|
|
{ |
|
130
|
|
|
invariant( |
|
131
|
|
|
$type instanceof InputObjectType, |
|
132
|
|
|
\sprintf('Expected %s to be a GraphQL InputObject type.', toString($type)) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param TypeInterface $type |
|
138
|
|
|
* @throws InvariantException |
|
139
|
|
|
*/ |
|
140
|
|
|
function assertListType(TypeInterface $type) |
|
141
|
|
|
{ |
|
142
|
|
|
invariant( |
|
143
|
|
|
$type instanceof ListType, |
|
144
|
|
|
\sprintf('Expected %s to be a GraphQL List type.', toString($type)) |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param TypeInterface $type |
|
150
|
|
|
* @throws InvariantException |
|
151
|
|
|
*/ |
|
152
|
|
|
function assertNonNullType(TypeInterface $type) |
|
153
|
|
|
{ |
|
154
|
|
|
invariant( |
|
155
|
|
|
$type instanceof NonNullType, |
|
156
|
|
|
\sprintf('Expected %s to be a GraphQL NonNull type.', toString($type)) |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Whether a type is an input type cannot be determined with `instanceof` |
|
162
|
|
|
* because lists and non-nulls can also be output types if the wrapped type is an output type. |
|
163
|
|
|
* |
|
164
|
|
|
* @param TypeInterface|null $type |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
function isInputType(?TypeInterface $type): bool |
|
168
|
|
|
{ |
|
169
|
|
|
return null !== $type && ($type instanceof InputTypeInterface || ($type instanceof WrappingTypeInterface && isInputType($type->getOfType()))); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param TypeInterface $type |
|
174
|
|
|
* @throws InvariantException |
|
175
|
|
|
*/ |
|
176
|
|
|
function assertInputType(TypeInterface $type) |
|
177
|
|
|
{ |
|
178
|
|
|
invariant( |
|
179
|
|
|
isInputType($type), |
|
180
|
|
|
\sprintf('Expected %s to be a GraphQL input type.', toString($type)) |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Whether a type is an output type cannot be determined with `instanceof` |
|
186
|
|
|
* because lists and non-nulls can also be output types if the wrapped type is an output type. |
|
187
|
|
|
* |
|
188
|
|
|
* @param TypeInterface|null $type |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
|
|
function isOutputType(?TypeInterface $type): bool |
|
192
|
|
|
{ |
|
193
|
|
|
return null !== $type && ($type instanceof OutputTypeInterface || ($type instanceof WrappingTypeInterface && isOutputType($type->getOfType()))); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param TypeInterface $type |
|
198
|
|
|
* @throws InvariantException |
|
199
|
|
|
*/ |
|
200
|
|
|
function assertOutputType(TypeInterface $type) |
|
201
|
|
|
{ |
|
202
|
|
|
invariant( |
|
203
|
|
|
isOutputType($type), |
|
204
|
|
|
\sprintf('Expected %s to be a GraphQL output type.', toString($type)) |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param TypeInterface $type |
|
210
|
|
|
* @throws InvariantException |
|
211
|
|
|
*/ |
|
212
|
|
|
function assertLeafType(TypeInterface $type) |
|
213
|
|
|
{ |
|
214
|
|
|
invariant( |
|
215
|
|
|
$type instanceof LeafTypeInterface, |
|
216
|
|
|
\sprintf('Expected %s to be a GraphQL leaf type.', toString($type)) |
|
217
|
|
|
); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param TypeInterface $type |
|
222
|
|
|
* @throws InvariantException |
|
223
|
|
|
*/ |
|
224
|
|
|
function assertCompositeType(TypeInterface $type) |
|
225
|
|
|
{ |
|
226
|
|
|
invariant( |
|
227
|
|
|
$type instanceof CompositeTypeInterface, |
|
228
|
|
|
\sprintf('Expected %s to be a GraphQL composite type.', toString($type)) |
|
229
|
|
|
); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param TypeInterface $type |
|
234
|
|
|
* @throws InvariantException |
|
235
|
|
|
*/ |
|
236
|
|
|
function assertAbstractType(TypeInterface $type) |
|
237
|
|
|
{ |
|
238
|
|
|
invariant( |
|
239
|
|
|
$type instanceof AbstractTypeInterface, |
|
240
|
|
|
\sprintf('Expected %s to be a GraphQL abstract type.', toString($type)) |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param TypeInterface $type |
|
246
|
|
|
* @throws InvariantException |
|
247
|
|
|
*/ |
|
248
|
|
|
function assertWrappingType(TypeInterface $type) |
|
249
|
|
|
{ |
|
250
|
|
|
invariant( |
|
251
|
|
|
$type instanceof WrappingTypeInterface, |
|
252
|
|
|
\sprintf('Expected %s to be a GraphQL wrapping type.', toString($type)) |
|
253
|
|
|
); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param TypeInterface $type |
|
258
|
|
|
* @return bool |
|
259
|
|
|
*/ |
|
260
|
|
|
function isNullableType(TypeInterface $type): bool |
|
261
|
|
|
{ |
|
262
|
|
|
return !($type instanceof NonNullType); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param TypeInterface $type |
|
267
|
|
|
* @return TypeInterface |
|
268
|
|
|
* @throws InvariantException |
|
269
|
|
|
*/ |
|
270
|
|
|
function assertNullableType(TypeInterface $type): TypeInterface |
|
271
|
|
|
{ |
|
272
|
|
|
invariant( |
|
273
|
|
|
isNullableType($type), |
|
274
|
|
|
\sprintf('Expected %s to be a GraphQL nullable type.', toString($type)) |
|
275
|
|
|
); |
|
276
|
|
|
|
|
277
|
|
|
return $type; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @param TypeInterface|null $type |
|
282
|
|
|
* @return TypeInterface|null |
|
283
|
|
|
*/ |
|
284
|
|
|
function getNullableType(?TypeInterface $type): ?TypeInterface |
|
285
|
|
|
{ |
|
286
|
|
|
if (null === $type) { |
|
287
|
|
|
return null; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
return $type instanceof NonNullType ? $type->getOfType() : $type; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @param TypeInterface $type |
|
295
|
|
|
* @throws InvariantException |
|
296
|
|
|
*/ |
|
297
|
|
|
function assertNamedType(TypeInterface $type) |
|
298
|
|
|
{ |
|
299
|
|
|
invariant( |
|
300
|
|
|
$type instanceof NamedTypeInterface, |
|
301
|
|
|
\sprintf('Expected %s to be a GraphQL named type.', toString($type)) |
|
302
|
|
|
); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @param TypeInterface|null $type |
|
307
|
|
|
* @return NamedTypeInterface|null |
|
308
|
|
|
*/ |
|
309
|
|
|
function getNamedType(?TypeInterface $type): ?NamedTypeInterface |
|
310
|
|
|
{ |
|
311
|
|
|
if (!$type) { |
|
312
|
|
|
return null; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$unwrappedType = $type; |
|
316
|
|
|
|
|
317
|
|
|
while ($unwrappedType instanceof WrappingTypeInterface) { |
|
318
|
|
|
$unwrappedType = $unwrappedType->getOfType(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
return $unwrappedType; |
|
|
|
|
|
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param array $config |
|
326
|
|
|
* @return ScalarType |
|
327
|
|
|
*/ |
|
328
|
|
|
function GraphQLScalarType(array $config = []): ScalarType |
|
329
|
|
|
{ |
|
330
|
|
|
return new ScalarType($config); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @param array $config |
|
335
|
|
|
* @return EnumType |
|
336
|
|
|
*/ |
|
337
|
|
|
function GraphQLEnumType(array $config = []): EnumType |
|
338
|
|
|
{ |
|
339
|
|
|
return new EnumType($config); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @param array $config |
|
344
|
|
|
* @return InputObjectType |
|
345
|
|
|
*/ |
|
346
|
|
|
function GraphQLInputObjectType(array $config = []): InputObjectType |
|
347
|
|
|
{ |
|
348
|
|
|
return new InputObjectType($config); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @param array $config |
|
353
|
|
|
* @return InterfaceType |
|
354
|
|
|
*/ |
|
355
|
|
|
function GraphQLInterfaceType(array $config = []): InterfaceType |
|
356
|
|
|
{ |
|
357
|
|
|
return new InterfaceType($config); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* @param array $config |
|
362
|
|
|
* @return ObjectType |
|
363
|
|
|
*/ |
|
364
|
|
|
function GraphQLObjectType(array $config = []): ObjectType |
|
365
|
|
|
{ |
|
366
|
|
|
return new ObjectType($config); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* @param array $config |
|
371
|
|
|
* @return UnionType |
|
372
|
|
|
*/ |
|
373
|
|
|
function GraphQLUnionType(array $config = []): UnionType |
|
374
|
|
|
{ |
|
375
|
|
|
return new UnionType($config); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* @param array $config |
|
380
|
|
|
* @return Schema |
|
381
|
|
|
*/ |
|
382
|
|
|
function GraphQLSchema(array $config = []): Schema |
|
383
|
|
|
{ |
|
384
|
|
|
return new Schema($config); |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* @param array $config |
|
389
|
|
|
* @return Directive |
|
390
|
|
|
*/ |
|
391
|
|
|
function GraphQLDirective(array $config = []): Directive |
|
392
|
|
|
{ |
|
393
|
|
|
return new Directive($config); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* @param TypeInterface $ofType |
|
398
|
|
|
* @return ListType |
|
399
|
|
|
*/ |
|
400
|
|
|
function GraphQLList(TypeInterface $ofType): ListType |
|
401
|
|
|
{ |
|
402
|
|
|
return new ListType($ofType); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* @param TypeInterface $ofType |
|
407
|
|
|
* @return NonNullType |
|
408
|
|
|
* @throws InvalidTypeException |
|
409
|
|
|
*/ |
|
410
|
|
|
function GraphQLNonNull(TypeInterface $ofType): NonNullType |
|
411
|
|
|
{ |
|
412
|
|
|
return new NonNullType($ofType); |
|
413
|
|
|
} |
|
414
|
|
|
|