/
Schema.php
603 lines (535 loc) · 17.8 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
declare(strict_types=1);
namespace GraphQL\Type;
use Generator;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\GraphQL;
use GraphQL\Language\AST\SchemaDefinitionNode;
use GraphQL\Language\AST\SchemaTypeExtensionNode;
use GraphQL\Type\Definition\AbstractType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\ImplementingType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Utils\InterfaceImplementations;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
use InvalidArgumentException;
use Traversable;
use function array_map;
use function get_class;
use function implode;
use function is_array;
use function is_callable;
use function sprintf;
/**
* Schema Definition (see [related docs](type-system/schema.md))
*
* A Schema is created by supplying the root types of each type of operation:
* query, mutation (optional) and subscription (optional). A schema definition is
* then supplied to the validator and executor. Usage Example:
*
* $schema = new GraphQL\Type\Schema([
* 'query' => $MyAppQueryRootType,
* 'mutation' => $MyAppMutationRootType,
* ]);
*
* Or using Schema Config instance:
*
* $config = GraphQL\Type\SchemaConfig::create()
* ->setQuery($MyAppQueryRootType)
* ->setMutation($MyAppMutationRootType);
*
* $schema = new GraphQL\Type\Schema($config);
*/
class Schema
{
/** @var SchemaConfig */
private $config;
/**
* Contains currently resolved schema types
*
* @var Type[]
*/
private $resolvedTypes = [];
/**
* Lazily initialised.
*
* @var array<string, InterfaceImplementations>
*/
private $implementationsMap;
/**
* True when $resolvedTypes contain all possible schema types
*
* @var bool
*/
private $fullyLoaded = false;
/** @var Error[] */
private $validationErrors;
/** @var SchemaTypeExtensionNode[] */
public $extensionASTNodes = [];
/**
* @param mixed[]|SchemaConfig $config
*
* @api
*/
public function __construct($config)
{
if (is_array($config)) {
$config = SchemaConfig::create($config);
}
// If this schema was built from a source known to be valid, then it may be
// marked with assumeValid to avoid an additional type system validation.
if ($config->getAssumeValid()) {
$this->validationErrors = [];
} else {
// Otherwise check for common mistakes during construction to produce
// clear and early error messages.
Utils::invariant(
$config instanceof SchemaConfig,
'Schema constructor expects instance of GraphQL\Type\SchemaConfig or an array with keys: %s; but got: %s',
implode(
', ',
[
'query',
'mutation',
'subscription',
'types',
'directives',
'typeLoader',
]
),
Utils::getVariableType($config)
);
Utils::invariant(
! $config->types || is_array($config->types) || is_callable($config->types),
'"types" must be array or callable if provided but got: ' . Utils::getVariableType($config->types)
);
Utils::invariant(
$config->directives === null || is_array($config->directives),
'"directives" must be Array if provided but got: ' . Utils::getVariableType($config->directives)
);
}
$this->config = $config;
$this->extensionASTNodes = $config->extensionASTNodes;
if ($config->query !== null) {
$this->resolvedTypes[$config->query->name] = $config->query;
}
if ($config->mutation !== null) {
$this->resolvedTypes[$config->mutation->name] = $config->mutation;
}
if ($config->subscription !== null) {
$this->resolvedTypes[$config->subscription->name] = $config->subscription;
}
if (is_array($this->config->types)) {
foreach ($this->resolveAdditionalTypes() as $type) {
if (isset($this->resolvedTypes[$type->name])) {
Utils::invariant(
$type === $this->resolvedTypes[$type->name],
sprintf(
'Schema must contain unique named types but contains multiple types named "%s" (see http://webonyx.github.io/graphql-php/type-system/#type-registry).',
$type
)
);
}
$this->resolvedTypes[$type->name] = $type;
}
}
$this->resolvedTypes += Type::getStandardTypes() + Introspection::getTypes();
if ($this->config->typeLoader) {
return;
}
// Perform full scan of the schema
$this->getTypeMap();
}
/**
* @return Generator
*/
private function resolveAdditionalTypes()
{
$types = $this->config->types ?? [];
if (is_callable($types)) {
$types = $types();
}
if (! is_array($types) && ! $types instanceof Traversable) {
throw new InvariantViolation(sprintf(
'Schema types callable must return array or instance of Traversable but got: %s',
Utils::getVariableType($types)
));
}
foreach ($types as $index => $type) {
$type = self::resolveType($type);
if (! $type instanceof Type) {
throw new InvariantViolation(sprintf(
'Each entry of schema types must be instance of GraphQL\Type\Definition\Type but entry at %s is %s',
$index,
Utils::printSafe($type)
));
}
yield $type;
}
}
/**
* Returns array of all types in this schema. Keys of this array represent type names, values are instances
* of corresponding type definitions
*
* This operation requires full schema scan. Do not use in production environment.
*
* @return array<string, Type>
*
* @api
*/
public function getTypeMap() : array
{
if (! $this->fullyLoaded) {
$this->resolvedTypes = $this->collectAllTypes();
$this->fullyLoaded = true;
}
return $this->resolvedTypes;
}
/**
* @return Type[]
*/
private function collectAllTypes()
{
$typeMap = [];
foreach ($this->resolvedTypes as $type) {
$typeMap = TypeInfo::extractTypes($type, $typeMap);
}
foreach ($this->getDirectives() as $directive) {
if (! ($directive instanceof Directive)) {
continue;
}
$typeMap = TypeInfo::extractTypesFromDirectives($directive, $typeMap);
}
// When types are set as array they are resolved in constructor
if (is_callable($this->config->types)) {
foreach ($this->resolveAdditionalTypes() as $type) {
$typeMap = TypeInfo::extractTypes($type, $typeMap);
}
}
return $typeMap;
}
/**
* Returns a list of directives supported by this schema
*
* @return Directive[]
*
* @api
*/
public function getDirectives()
{
return $this->config->directives ?? GraphQL::getStandardDirectives();
}
/**
* @param string $operation
*
* @return ObjectType|null
*/
public function getOperationType($operation)
{
switch ($operation) {
case 'query':
return $this->getQueryType();
case 'mutation':
return $this->getMutationType();
case 'subscription':
return $this->getSubscriptionType();
default:
return null;
}
}
/**
* Returns schema query type
*
* @return ObjectType
*
* @api
*/
public function getQueryType() : ?Type
{
return $this->config->query;
}
/**
* Returns schema mutation type
*
* @return ObjectType|null
*
* @api
*/
public function getMutationType() : ?Type
{
return $this->config->mutation;
}
/**
* Returns schema subscription
*
* @return ObjectType|null
*
* @api
*/
public function getSubscriptionType() : ?Type
{
return $this->config->subscription;
}
/**
* @return SchemaConfig
*
* @api
*/
public function getConfig()
{
return $this->config;
}
/**
* Returns type by its name
*
* @api
*/
public function getType(string $name) : ?Type
{
if (! isset($this->resolvedTypes[$name])) {
$type = $this->loadType($name);
if (! $type) {
return null;
}
$this->resolvedTypes[$name] = self::resolveType($type);
}
return $this->resolvedTypes[$name];
}
public function hasType(string $name) : bool
{
return $this->getType($name) !== null;
}
private function loadType(string $typeName) : ?Type
{
$typeLoader = $this->config->typeLoader;
if (! isset($typeLoader)) {
return $this->defaultTypeLoader($typeName);
}
$type = $typeLoader($typeName);
if (! $type instanceof Type) {
// Unless you know what you're doing, kindly resist the temptation to refactor or simplify this block. The
// twisty logic here is tuned for performance, and meant to prioritize the "happy path" (the result returned
// from the type loader is already a Type), and only checks for callable if that fails. If the result is
// neither a Type nor a callable, then we throw an exception.
if (is_callable($type)) {
$type = $type();
if (! $type instanceof Type) {
$this->throwNotAType($type, $typeName);
}
} else {
$this->throwNotAType($type, $typeName);
}
}
if ($type->name !== $typeName) {
throw new InvariantViolation(
sprintf('Type loader is expected to return type "%s", but it returned "%s"', $typeName, $type->name)
);
}
return $type;
}
protected function throwNotAType($type, string $typeName)
{
throw new InvariantViolation(
sprintf(
'Type loader is expected to return a callable or valid type "%s", but it returned %s',
$typeName,
Utils::printSafe($type)
)
);
}
private function defaultTypeLoader(string $typeName) : ?Type
{
// Default type loader simply falls back to collecting all types
$typeMap = $this->getTypeMap();
return $typeMap[$typeName] ?? null;
}
/**
* @param Type|callable():Type $type
*/
public static function resolveType($type) : Type
{
if ($type instanceof Type) {
return $type;
}
return $type();
}
/**
* Returns all possible concrete types for given abstract type
* (implementations for interfaces and members of union type for unions)
*
* This operation requires full schema scan. Do not use in production environment.
*
* @param InterfaceType|UnionType $abstractType
*
* @return array<Type&ObjectType>
*
* @api
*/
public function getPossibleTypes(Type $abstractType) : array
{
return $abstractType instanceof UnionType
? $abstractType->getTypes()
: $this->getImplementations($abstractType)->objects();
}
/**
* Returns all types that implement a given interface type.
*
* This operations requires full schema scan. Do not use in production environment.
*
* @api
*/
public function getImplementations(InterfaceType $abstractType) : InterfaceImplementations
{
return $this->collectImplementations()[$abstractType->name];
}
/**
* @return array<string, InterfaceImplementations>
*/
private function collectImplementations() : array
{
if (! isset($this->implementationsMap)) {
/** @var array<string, array<string, Type>> $foundImplementations */
$foundImplementations = [];
foreach ($this->getTypeMap() as $type) {
if ($type instanceof InterfaceType) {
if (! isset($foundImplementations[$type->name])) {
$foundImplementations[$type->name] = ['objects' => [], 'interfaces' => []];
}
foreach ($type->getInterfaces() as $iface) {
if (! isset($foundImplementations[$iface->name])) {
$foundImplementations[$iface->name] = ['objects' => [], 'interfaces' => []];
}
$foundImplementations[$iface->name]['interfaces'][] = $type;
}
} elseif ($type instanceof ObjectType) {
foreach ($type->getInterfaces() as $iface) {
if (! isset($foundImplementations[$iface->name])) {
$foundImplementations[$iface->name] = ['objects' => [], 'interfaces' => []];
}
$foundImplementations[$iface->name]['objects'][] = $type;
}
}
}
$this->implementationsMap = array_map(
static function (array $implementations) : InterfaceImplementations {
return new InterfaceImplementations($implementations['objects'], $implementations['interfaces']);
},
$foundImplementations
);
}
return $this->implementationsMap;
}
/**
* @deprecated as of 14.4.0 use isSubType instead, will be removed in 15.0.0.
*
* Returns true if object type is concrete type of given abstract type
* (implementation for interfaces and members of union type for unions)
*
* @api
* @codeCoverageIgnore
*/
public function isPossibleType(AbstractType $abstractType, ObjectType $possibleType) : bool
{
return $this->isSubType($abstractType, $possibleType);
}
/**
* Returns true if the given type is a sub type of the given abstract type.
*
* @param UnionType|InterfaceType $abstractType
* @param ObjectType|InterfaceType $maybeSubType
*
* @api
*/
public function isSubType(AbstractType $abstractType, ImplementingType $maybeSubType) : bool
{
if ($abstractType instanceof InterfaceType) {
return $maybeSubType->implementsInterface($abstractType);
}
if ($abstractType instanceof UnionType) {
return $abstractType->isPossibleType($maybeSubType);
}
throw new InvalidArgumentException(sprintf('$abstractType must be of type UnionType|InterfaceType got: %s.', get_class($abstractType)));
}
/**
* Returns instance of directive by name
*
* @api
*/
public function getDirective(string $name) : ?Directive
{
foreach ($this->getDirectives() as $directive) {
if ($directive->name === $name) {
return $directive;
}
}
return null;
}
public function getAstNode() : ?SchemaDefinitionNode
{
return $this->config->getAstNode();
}
/**
* Validates schema.
*
* This operation requires full schema scan. Do not use in production environment.
*
* @throws InvariantViolation
*
* @api
*/
public function assertValid()
{
$errors = $this->validate();
if ($errors) {
throw new InvariantViolation(implode("\n\n", $this->validationErrors));
}
$internalTypes = Type::getStandardTypes() + Introspection::getTypes();
foreach ($this->getTypeMap() as $name => $type) {
if (isset($internalTypes[$name])) {
continue;
}
$type->assertValid();
// Make sure type loader returns the same instance as registered in other places of schema
if (! $this->config->typeLoader) {
continue;
}
Utils::invariant(
$this->loadType($name) === $type,
sprintf(
'Type loader returns different instance for %s than field/argument definitions. Make sure you always return the same instance for the same type name.',
$name
)
);
}
}
/**
* Validates schema.
*
* This operation requires full schema scan. Do not use in production environment.
*
* @return InvariantViolation[]|Error[]
*
* @api
*/
public function validate()
{
// If this Schema has already been validated, return the previous results.
if ($this->validationErrors !== null) {
return $this->validationErrors;
}
// Validate the schema, producing a list of errors.
$context = new SchemaValidationContext($this);
$context->validateRootTypes();
$context->validateDirectives();
$context->validateTypes();
// Persist the results of validation before returning to ensure validation
// does not run multiple times for this schema.
$this->validationErrors = $context->getErrors();
return $this->validationErrors;
}
}