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

ExecutableDefinitionsRule::enterDocument()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\DocumentNode;
7
use Digia\GraphQL\Language\Node\ExecutableDefinitionNodeInterface;
8
use Digia\GraphQL\Language\Node\NodeInterface;
9
use Digia\GraphQL\Language\Node\SchemaDefinitionNode;
10
use function Digia\GraphQL\Validation\nonExecutableDefinitionMessage;
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 ExecutableDefinitionsRule extends AbstractRule
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    protected function enterDocument(DocumentNode $node): ?NodeInterface
24
    {
25
        foreach ($node->getDefinitions() as $definition) {
26
            if (!$definition instanceof ExecutableDefinitionNodeInterface) {
27
                /** @noinspection PhpUndefinedMethodInspection */
28
                $this->context->reportError(
29
                    new ValidationException(
30
                        nonExecutableDefinitionMessage(
31
                            $definition instanceof SchemaDefinitionNode ? 'schema' : $definition->getNameValue()
32
                        ),
33
                        [$definition]
34
                    )
35
                );
36
            }
37
        }
38
39
        return $node;
40
    }
41
}
42