Failed Conditions
Push — master ( bf4e7d...c70528 )
by Vladimir
09:31
created

Schema::resolveAdditionalTypes()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 11.9809

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
rs 8.8333
ccs 8
cts 15
cp 0.5333
cc 7
nc 8
nop 0
crap 11.9809
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Type;
6
7
use Generator;
8
use GraphQL\Error\Error;
9
use GraphQL\Error\InvariantViolation;
10
use GraphQL\GraphQL;
11
use GraphQL\Language\AST\SchemaDefinitionNode;
12
use GraphQL\Language\AST\SchemaTypeExtensionNode;
13
use GraphQL\Type\Definition\AbstractType;
14
use GraphQL\Type\Definition\Directive;
15
use GraphQL\Type\Definition\InterfaceType;
16
use GraphQL\Type\Definition\ObjectType;
17
use GraphQL\Type\Definition\Type;
18
use GraphQL\Type\Definition\UnionType;
19
use GraphQL\Utils\TypeInfo;
20
use GraphQL\Utils\Utils;
21
use Traversable;
22
use function array_values;
23
use function implode;
24
use function is_array;
25
use function is_callable;
26
use function sprintf;
27
28
/**
29
 * Schema Definition (see [related docs](type-system/schema.md))
30
 *
31
 * A Schema is created by supplying the root types of each type of operation:
32
 * query, mutation (optional) and subscription (optional). A schema definition is
33
 * then supplied to the validator and executor. Usage Example:
34
 *
35
 *     $schema = new GraphQL\Type\Schema([
36
 *       'query' => $MyAppQueryRootType,
37
 *       'mutation' => $MyAppMutationRootType,
38
 *     ]);
39
 *
40
 * Or using Schema Config instance:
41
 *
42
 *     $config = GraphQL\Type\SchemaConfig::create()
43
 *         ->setQuery($MyAppQueryRootType)
44
 *         ->setMutation($MyAppMutationRootType);
45
 *
46
 *     $schema = new GraphQL\Type\Schema($config);
47
 */
48
class Schema
49
{
50
    /** @var SchemaConfig */
51
    private $config;
52
53
    /**
54
     * Contains currently resolved schema types
55
     *
56
     * @var Type[]
57
     */
58
    private $resolvedTypes = [];
59
60
    /** @var Type[][]|null */
61
    private $possibleTypeMap;
62
63
    /**
64
     * True when $resolvedTypes contain all possible schema types
65
     *
66
     * @var bool
67
     */
68
    private $fullyLoaded = false;
69
70
    /** @var InvariantViolation[]|null */
71
    private $validationErrors;
72
73
    /** @var SchemaTypeExtensionNode[] */
74
    public $extensionASTNodes;
75
76
    /**
77
     * @param mixed[]|SchemaConfig $config
78
     *
79
     * @api
80
     */
81 871
    public function __construct($config)
82
    {
83 871
        if (is_array($config)) {
84 871
            $config = SchemaConfig::create($config);
85
        }
86
87
        // If this schema was built from a source known to be valid, then it may be
88
        // marked with assumeValid to avoid an additional type system validation.
89 870
        if ($config->getAssumeValid()) {
90
            $this->validationErrors = [];
91
        } else {
92
            // Otherwise check for common mistakes during construction to produce
93
            // clear and early error messages.
94 870
            Utils::invariant(
95 870
                $config instanceof SchemaConfig,
96 870
                'Schema constructor expects instance of GraphQL\Type\SchemaConfig or an array with keys: %s; but got: %s',
97 870
                implode(
98 870
                    ', ',
99
                    [
100 870
                        'query',
101
                        'mutation',
102
                        'subscription',
103
                        'types',
104
                        'directives',
105
                        'typeLoader',
106
                    ]
107
                ),
108 870
                Utils::getVariableType($config)
109
            );
110 870
            Utils::invariant(
111 870
                ! $config->types || is_array($config->types) || is_callable($config->types),
112 870
                '"types" must be array or callable if provided but got: ' . Utils::getVariableType($config->types)
113
            );
114 870
            Utils::invariant(
115 870
                ! $config->directives || is_array($config->directives),
0 ignored issues
show
Bug Best Practice introduced by
The expression $config->directives of type GraphQL\Type\Definition\Directive[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116 870
                '"directives" must be Array if provided but got: ' . Utils::getVariableType($config->directives)
117
            );
118
        }
119
120 870
        $this->config            = $config;
121 870
        $this->extensionASTNodes = $config->extensionASTNodes;
122
123 870
        if ($config->query) {
124 859
            $this->resolvedTypes[$config->query->name] = $config->query;
125
        }
126 870
        if ($config->mutation) {
127 82
            $this->resolvedTypes[$config->mutation->name] = $config->mutation;
128
        }
129 870
        if ($config->subscription) {
130 34
            $this->resolvedTypes[$config->subscription->name] = $config->subscription;
131
        }
132 870
        if (is_array($this->config->types)) {
133 155
            foreach ($this->resolveAdditionalTypes() as $type) {
134 155
                if (isset($this->resolvedTypes[$type->name])) {
135 47
                    Utils::invariant(
136 47
                        $type === $this->resolvedTypes[$type->name],
137 47
                        sprintf(
138 47
                            'Schema must contain unique named types but contains multiple types named "%s" (see http://webonyx.github.io/graphql-php/type-system/#type-registry).',
139 47
                            $type
140
                        )
141
                    );
142
                }
143 155
                $this->resolvedTypes[$type->name] = $type;
144
            }
145
        }
146 869
        $this->resolvedTypes += Type::getStandardTypes() + Introspection::getTypes();
147
148 869
        if ($this->config->typeLoader) {
149 131
            return;
150
        }
151
152
        // Perform full scan of the schema
153 770
        $this->getTypeMap();
154 766
    }
155
156
    /**
157
     * @return Generator
158
     */
159 252
    private function resolveAdditionalTypes()
160
    {
161 252
        $types = $this->config->types ?: [];
162
163 252
        if (is_callable($types)) {
164 103
            $types = $types();
165
        }
166
167 252
        if (! is_array($types) && ! $types instanceof Traversable) {
168
            throw new InvariantViolation(sprintf(
169
                'Schema types callable must return array or instance of Traversable but got: %s',
170
                Utils::getVariableType($types)
171
            ));
172
        }
173
174 252
        foreach ($types as $index => $type) {
175 252
            if (! $type instanceof Type) {
176
                throw new InvariantViolation(sprintf(
177
                    'Each entry of schema types must be instance of GraphQL\Type\Definition\Type but entry at %s is %s',
178
                    $index,
179
                    Utils::printSafe($type)
180
                ));
181
            }
182 252
            yield $type;
183
        }
184 251
    }
185
186
    /**
187
     * Returns array of all types in this schema. Keys of this array represent type names, values are instances
188
     * of corresponding type definitions
189
     *
190
     * This operation requires full schema scan. Do not use in production environment.
191
     *
192
     * @return Type[]
193
     *
194
     * @api
195
     */
196 847
    public function getTypeMap()
197
    {
198 847
        if (! $this->fullyLoaded) {
199 847
            $this->resolvedTypes = $this->collectAllTypes();
200 840
            $this->fullyLoaded   = true;
201
        }
202
203 840
        return $this->resolvedTypes;
204
    }
205
206
    /**
207
     * @return Type[]
208
     */
209 847
    private function collectAllTypes()
210
    {
211 847
        $typeMap = [];
212 847
        foreach ($this->resolvedTypes as $type) {
213 847
            $typeMap = TypeInfo::extractTypes($type, $typeMap);
214
        }
215 840
        foreach ($this->getDirectives() as $directive) {
216 840
            if (! ($directive instanceof Directive)) {
217 1
                continue;
218
            }
219
220 839
            $typeMap = TypeInfo::extractTypesFromDirectives($directive, $typeMap);
0 ignored issues
show
Bug introduced by
It seems like $typeMap can also be of type null; however, parameter $typeMap of GraphQL\Utils\TypeInfo::...ctTypesFromDirectives() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

220
            $typeMap = TypeInfo::extractTypesFromDirectives($directive, /** @scrutinizer ignore-type */ $typeMap);
Loading history...
221
        }
222
        // When types are set as array they are resolved in constructor
223 840
        if (is_callable($this->config->types)) {
224 103
            foreach ($this->resolveAdditionalTypes() as $type) {
225 103
                $typeMap = TypeInfo::extractTypes($type, $typeMap);
226
            }
227
        }
228
229 840
        return $typeMap;
230
    }
231
232
    /**
233
     * Returns a list of directives supported by this schema
234
     *
235
     * @return Directive[]
236
     *
237
     * @api
238
     */
239 854
    public function getDirectives()
240
    {
241 854
        return $this->config->directives ?: GraphQL::getStandardDirectives();
242
    }
243
244
    /**
245
     * @param string $operation
246
     *
247
     * @return ObjectType|null
248
     */
249
    public function getOperationType($operation)
250
    {
251
        switch ($operation) {
252
            case 'query':
253
                return $this->getQueryType();
254
            case 'mutation':
255
                return $this->getMutationType();
256
            case 'subscription':
257
                return $this->getSubscriptionType();
258
            default:
259
                return null;
260
        }
261
    }
262
263
    /**
264
     * Returns schema query type
265
     *
266
     * @return ObjectType
267
     *
268
     * @api
269
     */
270 757
    public function getQueryType()
271
    {
272 757
        return $this->config->query;
273
    }
274
275
    /**
276
     * Returns schema mutation type
277
     *
278
     * @return ObjectType|null
279
     *
280
     * @api
281
     */
282 213
    public function getMutationType()
283
    {
284 213
        return $this->config->mutation;
285
    }
286
287
    /**
288
     * Returns schema subscription
289
     *
290
     * @return ObjectType|null
291
     *
292
     * @api
293
     */
294 206
    public function getSubscriptionType()
295
    {
296 206
        return $this->config->subscription;
297
    }
298
299
    /**
300
     * @return SchemaConfig
301
     *
302
     * @api
303
     */
304 8
    public function getConfig()
305
    {
306 8
        return $this->config;
307
    }
308
309
    /**
310
     * Returns type by it's name
311
     *
312
     * @param string $name
313
     *
314
     * @return Type|null
315
     *
316
     * @api
317
     */
318 537
    public function getType($name)
319
    {
320 537
        if (! isset($this->resolvedTypes[$name])) {
321 90
            $type = $this->loadType($name);
322 85
            if (! $type) {
0 ignored issues
show
introduced by
$type is of type GraphQL\Type\Definition\Type, thus it always evaluated to true.
Loading history...
323 70
                return null;
324
            }
325 15
            $this->resolvedTypes[$name] = $type;
326
        }
327
328 490
        return $this->resolvedTypes[$name];
329
    }
330
331
    /**
332
     * @param string $name
333
     *
334
     * @return bool
335
     */
336 35
    public function hasType($name)
337
    {
338 35
        return $this->getType($name) !== null;
339
    }
340
341
    /**
342
     * @param string $typeName
343
     *
344
     * @return Type
345
     */
346 91
    private function loadType($typeName)
347
    {
348 91
        $typeLoader = $this->config->typeLoader;
349
350 91
        if (! $typeLoader) {
351 70
            return $this->defaultTypeLoader($typeName);
352
        }
353
354 21
        $type = $typeLoader($typeName);
355
356 19
        if (! $type instanceof Type) {
357 2
            throw new InvariantViolation(
358 2
                sprintf(
359 2
                    'Type loader is expected to return valid type "%s", but it returned %s',
360 2
                    $typeName,
361 2
                    Utils::printSafe($type)
362
                )
363
            );
364
        }
365 17
        if ($type->name !== $typeName) {
366 1
            throw new InvariantViolation(
367 1
                sprintf('Type loader is expected to return type "%s", but it returned "%s"', $typeName, $type->name)
368
            );
369
        }
370
371 16
        return $type;
372
    }
373
374
    /**
375
     * @param string $typeName
376
     *
377
     * @return Type
378
     */
379 70
    private function defaultTypeLoader($typeName)
380
    {
381
        // Default type loader simply fallbacks to collecting all types
382 70
        $typeMap = $this->getTypeMap();
383
384 70
        return $typeMap[$typeName] ?? null;
385
    }
386
387
    /**
388
     * Returns all possible concrete types for given abstract type
389
     * (implementations for interfaces and members of union type for unions)
390
     *
391
     * This operation requires full schema scan. Do not use in production environment.
392
     *
393
     * @param InterfaceType|UnionType $abstractType
394
     *
395
     * @return ObjectType[]
396
     *
397
     * @api
398
     */
399 59
    public function getPossibleTypes(AbstractType $abstractType) : array
400
    {
401 59
        $possibleTypeMap = $this->getPossibleTypeMap();
402
403 59
        return isset($possibleTypeMap[$abstractType->name]) ? array_values($possibleTypeMap[$abstractType->name]) : [];
0 ignored issues
show
Bug introduced by
Accessing name on the interface GraphQL\Type\Definition\AbstractType suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
404
    }
405
406
    /**
407
     * @return Type[][]
408
     */
409 59
    private function getPossibleTypeMap()
410
    {
411 59
        if ($this->possibleTypeMap === null) {
412 59
            $this->possibleTypeMap = [];
413 59
            foreach ($this->getTypeMap() as $type) {
414 59
                if ($type instanceof ObjectType) {
415 59
                    foreach ($type->getInterfaces() as $interface) {
416 56
                        if (! ($interface instanceof InterfaceType)) {
417
                            continue;
418
                        }
419
420 59
                        $this->possibleTypeMap[$interface->name][$type->name] = $type;
421
                    }
422 59
                } elseif ($type instanceof UnionType) {
423 20
                    foreach ($type->getTypes() as $innerType) {
424 59
                        $this->possibleTypeMap[$type->name][$innerType->name] = $innerType;
425
                    }
426
                }
427
            }
428
        }
429
430 59
        return $this->possibleTypeMap;
431
    }
432
433
    /**
434
     * Returns true if object type is concrete type of given abstract type
435
     * (implementation for interfaces and members of union type for unions)
436
     *
437
     * @param InterfaceType|UnionType $abstractType
438
     *
439
     * @api
440
     */
441 47
    public function isPossibleType(AbstractType $abstractType, ObjectType $possibleType) : bool
442
    {
443 47
        if ($abstractType instanceof InterfaceType) {
444 32
            return $possibleType->implementsInterface($abstractType);
445
        }
446
447
        /** @var UnionType $abstractType */
448 16
        return $abstractType->isPossibleType($possibleType);
0 ignored issues
show
Bug introduced by
The method isPossibleType() does not exist on GraphQL\Type\Definition\AbstractType. It seems like you code against a sub-type of GraphQL\Type\Definition\AbstractType such as GraphQL\Type\Definition\UnionType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

448
        return $abstractType->/** @scrutinizer ignore-call */ isPossibleType($possibleType);
Loading history...
449
    }
450
451
    /**
452
     * Returns instance of directive by name
453
     *
454
     * @param string $name
455
     *
456
     * @return Directive
457
     *
458
     * @api
459
     */
460 65
    public function getDirective($name)
461
    {
462 65
        foreach ($this->getDirectives() as $directive) {
463 65
            if ($directive->name === $name) {
464 65
                return $directive;
465
            }
466
        }
467
468 25
        return null;
469
    }
470
471
    /**
472
     * @return SchemaDefinitionNode
473
     */
474 62
    public function getAstNode()
475
    {
476 62
        return $this->config->getAstNode();
477
    }
478
479
    /**
480
     * Validates schema.
481
     *
482
     * This operation requires full schema scan. Do not use in production environment.
483
     *
484
     * @throws InvariantViolation
485
     *
486
     * @api
487
     */
488 11
    public function assertValid()
489
    {
490 11
        $errors = $this->validate();
491
492 11
        if ($errors) {
493
            throw new InvariantViolation(implode("\n\n", $this->validationErrors));
494
        }
495
496 11
        $internalTypes = Type::getStandardTypes() + Introspection::getTypes();
497 11
        foreach ($this->getTypeMap() as $name => $type) {
498 11
            if (isset($internalTypes[$name])) {
499 1
                continue;
500
            }
501
502 11
            $type->assertValid();
503
504
            // Make sure type loader returns the same instance as registered in other places of schema
505 11
            if (! $this->config->typeLoader) {
506 10
                continue;
507
            }
508
509 1
            Utils::invariant(
510 1
                $this->loadType($name) === $type,
511 1
                sprintf(
512 1
                    'Type loader returns different instance for %s than field/argument definitions. Make sure you always return the same instance for the same type name.',
513 1
                    $name
514
                )
515
            );
516
        }
517 1
    }
518
519
    /**
520
     * Validates schema.
521
     *
522
     * This operation requires full schema scan. Do not use in production environment.
523
     *
524
     * @return InvariantViolation[]|Error[]
525
     *
526
     * @api
527
     */
528 88
    public function validate()
529
    {
530
        // If this Schema has already been validated, return the previous results.
531 88
        if ($this->validationErrors !== null) {
532
            return $this->validationErrors;
533
        }
534
        // Validate the schema, producing a list of errors.
535 88
        $context = new SchemaValidationContext($this);
536 88
        $context->validateRootTypes();
537 88
        $context->validateDirectives();
538 88
        $context->validateTypes();
539
540
        // Persist the results of validation before returning to ensure validation
541
        // does not run multiple times for this schema.
542 88
        $this->validationErrors = $context->getErrors();
0 ignored issues
show
Documentation Bug introduced by
It seems like $context->getErrors() of type GraphQL\Error\Error[] is incompatible with the declared type GraphQL\Error\InvariantViolation[]|null of property $validationErrors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
543
544 88
        return $this->validationErrors;
545
    }
546
}
547