Completed
Pull Request — master (#54)
by Christoffer
02:45 queued 40s
created

ExecutableDefinitionsRule::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
use function Digia\GraphQL\Validation\nonExecutableDefinitionMessage;
12
13
/**
14
 * Executable definitions
15
 *
16
 * A GraphQL document is only valid for execution if all definitions are either
17
 * operation or fragment definitions.
18
 */
19
class ExecutableDefinitionsRule extends AbstractRule
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function enterNode(NodeInterface $node): ?NodeInterface
25
    {
26
        if ($node instanceof DocumentNode) {
27
            /** @var NamedTypeNode $definition */
28
            foreach ($node->getDefinitions() as $definition) {
29
                if (!$definition instanceof ExecutableDefinitionNodeInterface) {
30
                    $this->validationContext->reportError(
31
                        new ValidationException(
32
                            nonExecutableDefinitionMessage(
33
                                $definition instanceof SchemaDefinitionNode ? 'schema' : $definition->getNameValue()
34
                            ),
35
                            [$definition]
36
                        )
37
                    );
38
                }
39
            }
40
        }
41
42
        return $node;
43
    }
44
}
45