Completed
Pull Request — master (#92)
by Christoffer
02:43
created

SingleFieldSubscriptionsRule::enterNode()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 1
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\OperationDefinitionNode;
8
use function Digia\GraphQL\Validation\singleFieldOnlyMessage;
9
10
/**
11
 * Subscriptions must only include one field.
12
 *
13
 * A GraphQL subscription is valid only if it contains a single root field.
14
 */
15
class SingleFieldSubscriptionsRule extends AbstractRule
16
{
17
    /**
18
     * @inheritdoc
19
     */
20
    public function enterNode(NodeInterface $node): ?NodeInterface
21
    {
22
        if ($node instanceof OperationDefinitionNode) {
23
            if ($node->getOperation() === 'subscription') {
24
                $selectionSet = $node->getSelectionSet();
25
                if (null !== $selectionSet) {
26
                    $selections = $selectionSet->getSelections();
27
                    if (\count($selections) !== 1) {
28
                        $this->validationContext->reportError(
29
                            new ValidationException(
30
                                singleFieldOnlyMessage((string)$node),
31
                                \array_slice($selections, 1)
32
                            )
33
                        );
34
                    }
35
                }
36
            }
37
        }
38
39
        return $node;
40
    }
41
}
42