Completed
Pull Request — master (#110)
by Christoffer
02:25
created

KnownTypeNamesRule::enterNamedType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\InputObjectTypeDefinitionNode;
7
use Digia\GraphQL\Language\Node\InterfaceTypeDefinitionNode;
8
use Digia\GraphQL\Language\Node\NamedTypeNode;
9
use Digia\GraphQL\Language\Node\NameNode;
10
use Digia\GraphQL\Language\Node\NodeInterface;
11
use Digia\GraphQL\Language\Node\ObjectTypeDefinitionNode;
12
use Digia\GraphQL\Language\Node\UnionTypeDefinitionNode;
13
use function Digia\GraphQL\Util\suggestionList;
14
use function Digia\GraphQL\Validation\unknownTypeMessage;
15
16
/**
17
 * Known type names
18
 *
19
 * A GraphQL document is only valid if referenced types (specifically
20
 * variable definitions and fragment conditions) are defined by the type schema.
21
 */
22
class KnownTypeNamesRule extends AbstractRule
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    protected function enterNamedType(NamedTypeNode $node): ?NodeInterface
28
    {
29
        $schema   = $this->context->getSchema();
30
        $typeName = $node->getNameValue();
31
        $type     = $schema->getType($typeName);
32
33
        if (null === $type) {
34
            $this->context->reportError(
35
                new ValidationException(
36
                    unknownTypeMessage($typeName, suggestionList($typeName, \array_keys($schema->getTypeMap()))),
37
                    [$node]
38
                )
39
            );
40
        }
41
42
        return $node;
43
    }
44
45
    // TODO: when validating IDL, re-enable these. Experimental version does not add unreferenced types, resulting in false-positive errors. Squelched errors for now.
46
47
    /**
48
     * @inheritdoc
49
     */
50
    protected function enterObjectTypeDefinition(ObjectTypeDefinitionNode $node): ?NodeInterface
51
    {
52
        return null;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    protected function enterInterfaceTypeDefinition(InterfaceTypeDefinitionNode $node): ?NodeInterface
59
    {
60
        return null;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    protected function enterUnionTypeDefinition(UnionTypeDefinitionNode $node): ?NodeInterface
67
    {
68
        return null;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    protected function enterInputObjectTypeDefinition(InputObjectTypeDefinitionNode $node): ?NodeInterface
75
    {
76
        return null;
77
    }
78
}
79