Passed
Pull Request — master (#54)
by Christoffer
02:06
created

ExecutableDefinitionRule::enterNode()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 2
nop 1
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\AST\Node\DocumentNode;
7
use Digia\GraphQL\Language\AST\Node\ExecutableDefinitionNodeInterface;
8
use Digia\GraphQL\Language\AST\Node\NamedTypeNode;
9
use Digia\GraphQL\Language\AST\Node\NodeInterface;
10
use Digia\GraphQL\Language\AST\Node\SchemaDefinitionNode;
11
12
/**
13
 * Executable definitions
14
 *
15
 * A GraphQL document is only valid for execution if all definitions are either
16
 * operation or fragment definitions.
17
 */
18
class ExecutableDefinitionRule extends AbstractRule
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function enterNode(NodeInterface $node): ?NodeInterface
24
    {
25
        if ($node instanceof DocumentNode) {
26
            /** @var NamedTypeNode $definition */
27
            foreach ($node->getDefinitions() as $definition) {
28
                if (!$definition instanceof ExecutableDefinitionNodeInterface) {
29
                    $this->validationContext->reportError(
30
                        new ValidationException(
31
                            nonExecutableDefinitionMessage(
32
                                $definition instanceof SchemaDefinitionNode ? 'schema' : $definition->getNameValue()
33
                            ),
34
                            [$definition]
35
                        )
36
                    );
37
                }
38
            }
39
        }
40
41
        return $node;
42
    }
43
}
44