1
|
|
|
<?php |
2
|
|
|
namespace GraphQL\Type\Definition; |
3
|
|
|
|
4
|
|
|
use GraphQL\Error\InvariantViolation; |
5
|
|
|
use GraphQL\Language\AST\TypeDefinitionNode; |
6
|
|
|
use GraphQL\Type\Introspection; |
7
|
|
|
use GraphQL\Utils\Utils; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Registry of standard GraphQL types |
11
|
|
|
* and a base class for all other types. |
12
|
|
|
* |
13
|
|
|
* @package GraphQL\Type\Definition |
14
|
|
|
*/ |
15
|
|
|
abstract class Type implements \JsonSerializable |
16
|
|
|
{ |
17
|
|
|
const STRING = 'String'; |
18
|
|
|
const INT = 'Int'; |
19
|
|
|
const BOOLEAN = 'Boolean'; |
20
|
|
|
const FLOAT = 'Float'; |
21
|
|
|
const ID = 'ID'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $internalTypes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $builtInTypes; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @api |
35
|
|
|
* @return IDType |
36
|
|
|
*/ |
37
|
390 |
|
public static function id() |
38
|
|
|
{ |
39
|
390 |
|
return self::getInternalType(self::ID); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @api |
44
|
|
|
* @return StringType |
45
|
|
|
*/ |
46
|
767 |
|
public static function string() |
47
|
|
|
{ |
48
|
767 |
|
return self::getInternalType(self::STRING); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @api |
53
|
|
|
* @return BooleanType |
54
|
|
|
*/ |
55
|
481 |
|
public static function boolean() |
56
|
|
|
{ |
57
|
481 |
|
return self::getInternalType(self::BOOLEAN); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @api |
62
|
|
|
* @return IntType |
63
|
|
|
*/ |
64
|
558 |
|
public static function int() |
65
|
|
|
{ |
66
|
558 |
|
return self::getInternalType(self::INT); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @api |
71
|
|
|
* @return FloatType |
72
|
|
|
*/ |
73
|
368 |
|
public static function float() |
74
|
|
|
{ |
75
|
368 |
|
return self::getInternalType(self::FLOAT); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @api |
80
|
|
|
* @param Type|ObjectType|InterfaceType|UnionType|ScalarType|InputObjectType|EnumType|ListOfType|NonNull $wrappedType |
81
|
|
|
* @return ListOfType |
82
|
|
|
*/ |
83
|
564 |
|
public static function listOf($wrappedType) |
84
|
|
|
{ |
85
|
564 |
|
return new ListOfType($wrappedType); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @api |
90
|
|
|
* @param ObjectType|InterfaceType|UnionType|ScalarType|InputObjectType|EnumType|ListOfType $wrappedType |
91
|
|
|
* @return NonNull |
92
|
|
|
*/ |
93
|
619 |
|
public static function nonNull($wrappedType) |
94
|
|
|
{ |
95
|
619 |
|
return new NonNull($wrappedType); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $name |
100
|
|
|
* @return array|IDType|StringType|FloatType|IntType|BooleanType |
101
|
|
|
*/ |
102
|
887 |
|
private static function getInternalType($name = null) |
103
|
|
|
{ |
104
|
887 |
|
if (null === self::$internalTypes) { |
|
|
|
|
105
|
|
|
self::$internalTypes = [ |
106
|
1 |
|
self::ID => new IDType(), |
107
|
1 |
|
self::STRING => new StringType(), |
108
|
1 |
|
self::FLOAT => new FloatType(), |
109
|
1 |
|
self::INT => new IntType(), |
110
|
1 |
|
self::BOOLEAN => new BooleanType() |
111
|
|
|
]; |
112
|
|
|
} |
113
|
887 |
|
return $name ? self::$internalTypes[$name] : self::$internalTypes; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns all builtin scalar types |
118
|
|
|
* |
119
|
|
|
* @return Type[] |
120
|
|
|
*/ |
121
|
776 |
|
public static function getInternalTypes() |
122
|
|
|
{ |
123
|
776 |
|
return self::getInternalType(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns all builtin in types including base scalar and |
128
|
|
|
* introspection types |
129
|
|
|
* |
130
|
|
|
* @return Type[] |
131
|
|
|
*/ |
132
|
101 |
|
public static function getAllBuiltInTypes() |
133
|
|
|
{ |
134
|
101 |
|
if (null === self::$builtInTypes) { |
|
|
|
|
135
|
1 |
|
self::$builtInTypes = array_merge( |
136
|
1 |
|
Introspection::getTypes(), |
137
|
1 |
|
self::getInternalTypes() |
138
|
|
|
); |
139
|
|
|
} |
140
|
101 |
|
return self::$builtInTypes; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Checks if the type is a builtin type |
145
|
|
|
* |
146
|
|
|
* @param Type $type |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
49 |
|
public static function isBuiltInType(Type $type) |
150
|
|
|
{ |
151
|
49 |
|
return in_array($type->name, array_keys(self::getAllBuiltInTypes())); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @api |
156
|
|
|
* @param Type $type |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
385 |
|
public static function isInputType($type) |
160
|
|
|
{ |
161
|
385 |
|
return $type instanceof InputType && |
162
|
|
|
( |
163
|
354 |
|
!$type instanceof WrappingType || |
164
|
385 |
|
self::getNamedType($type) instanceof InputType |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @api |
170
|
|
|
* @param Type $type |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
589 |
|
public static function isOutputType($type) |
174
|
|
|
{ |
175
|
589 |
|
return $type instanceof OutputType && |
176
|
|
|
( |
177
|
574 |
|
!$type instanceof WrappingType || |
178
|
589 |
|
self::getNamedType($type) instanceof OutputType |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @api |
184
|
|
|
* @param $type |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
123 |
|
public static function isLeafType($type) |
188
|
|
|
{ |
189
|
123 |
|
return $type instanceof LeafType; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @api |
194
|
|
|
* @param Type $type |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
514 |
|
public static function isCompositeType($type) |
198
|
|
|
{ |
199
|
514 |
|
return $type instanceof CompositeType; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @api |
204
|
|
|
* @param Type $type |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
26 |
|
public static function isAbstractType($type) |
208
|
|
|
{ |
209
|
26 |
|
return $type instanceof AbstractType; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @api |
214
|
|
|
* @param Type $type |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
720 |
|
public static function isType($type) |
218
|
|
|
{ |
219
|
720 |
|
return $type instanceof Type; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param mixed $type |
224
|
|
|
* @return mixed |
225
|
|
|
*/ |
226
|
617 |
|
public static function assertType($type) |
227
|
|
|
{ |
228
|
617 |
|
Utils::invariant( |
229
|
617 |
|
self::isType($type), |
230
|
617 |
|
'Expected ' . Utils::printSafe($type) . ' to be a GraphQL type.' |
231
|
|
|
); |
232
|
|
|
|
233
|
617 |
|
return $type; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @api |
238
|
|
|
* @param Type $type |
239
|
|
|
* @return ObjectType|InterfaceType|UnionType|ScalarType|InputObjectType|EnumType|ListOfType |
240
|
|
|
*/ |
241
|
10 |
|
public static function getNullableType($type) |
242
|
|
|
{ |
243
|
10 |
|
return $type instanceof NonNull ? $type->getWrappedType() : $type; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @api |
248
|
|
|
* @param Type $type |
249
|
|
|
* @return ObjectType|InterfaceType|UnionType|ScalarType|InputObjectType|EnumType |
250
|
|
|
*/ |
251
|
605 |
|
public static function getNamedType($type) |
252
|
|
|
{ |
253
|
605 |
|
if (null === $type) { |
254
|
110 |
|
return null; |
255
|
|
|
} |
256
|
590 |
|
while ($type instanceof WrappingType) { |
257
|
302 |
|
$type = $type->getWrappedType(); |
258
|
|
|
} |
259
|
590 |
|
return $type; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @var string |
264
|
|
|
*/ |
265
|
|
|
public $name; |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @var string|null |
269
|
|
|
*/ |
270
|
|
|
public $description; |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @var TypeDefinitionNode|null |
274
|
|
|
*/ |
275
|
|
|
public $astNode; |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @var array |
279
|
|
|
*/ |
280
|
|
|
public $config; |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return null|string |
284
|
|
|
*/ |
285
|
36 |
|
protected function tryInferName() |
286
|
|
|
{ |
287
|
36 |
|
if ($this->name) { |
288
|
34 |
|
return $this->name; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// If class is extended - infer name from className |
292
|
|
|
// QueryType -> Type |
293
|
|
|
// SomeOtherType -> SomeOther |
294
|
2 |
|
$tmp = new \ReflectionClass($this); |
295
|
2 |
|
$name = $tmp->getShortName(); |
296
|
|
|
|
297
|
2 |
|
if ($tmp->getNamespaceName() !== __NAMESPACE__) { |
298
|
1 |
|
return preg_replace('~Type$~', '', $name); |
299
|
|
|
} |
300
|
1 |
|
return null; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @throws InvariantViolation |
305
|
|
|
*/ |
306
|
23 |
|
public function assertValid() |
307
|
|
|
{ |
308
|
23 |
|
Utils::assertValidName($this->name); |
309
|
23 |
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
844 |
|
public function toString() |
315
|
|
|
{ |
316
|
844 |
|
return $this->name; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return string |
321
|
|
|
*/ |
322
|
1 |
|
public function jsonSerialize() |
323
|
|
|
{ |
324
|
1 |
|
return $this->toString(); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
762 |
|
public function __toString() |
331
|
|
|
{ |
332
|
|
|
try { |
333
|
762 |
|
return $this->toString(); |
334
|
|
|
} catch (\Exception $e) { |
335
|
|
|
echo $e; |
336
|
|
|
} catch (\Throwable $e) { |
337
|
|
|
echo $e; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|