Completed
Pull Request — master (#101)
by Christoffer
02:57
created

VariablesAreInputTypesRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A enterNode() 0 18 3
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\NodeInterface;
7
use Digia\GraphQL\Language\Node\VariableDefinitionNode;
8
use function Digia\GraphQL\printNode;
9
use function Digia\GraphQL\Type\isInputType;
10
use function Digia\GraphQL\Util\typeFromAST;
11
use function Digia\GraphQL\Validation\nonInputTypeOnVariableMessage;
12
13
/**
14
 * Variables are input types
15
 *
16
 * A GraphQL operation is only valid if all the variables it defines are of
17
 * input types (scalar, enum, or input object).
18
 */
19
class VariablesAreInputTypesRule extends AbstractRule
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function enterNode(NodeInterface $node): ?NodeInterface
25
    {
26
        if ($node instanceof VariableDefinitionNode) {
27
            $type = typeFromAST($this->validationContext->getSchema(), $node->getType());
28
29
            if (!isInputType($type)) {
30
                $variableName = $node->getVariable()->getNameValue();
31
32
                $this->validationContext->reportError(
33
                    new ValidationException(
34
                        nonInputTypeOnVariableMessage($variableName, printNode($node->getType())),
35
                        [$node->getType()]
36
                    )
37
                );
38
            }
39
        }
40
41
        return $node;
42
    }
43
}
44