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

enterOperationDefinition()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
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
    protected function enterOperationDefinition(OperationDefinitionNode $node): ?NodeInterface
21
    {
22
        if ($node->getOperation() !== 'subscription') {
23
            return $node;
24
        }
25
26
        $selectionSet = $node->getSelectionSet();
27
28
        if (null !== $selectionSet) {
29
            $selections = $selectionSet->getSelections();
30
            if (\count($selections) !== 1) {
31
                $this->context->reportError(
32
                    new ValidationException(singleFieldOnlyMessage((string)$node), \array_slice($selections, 1))
33
                );
34
            }
35
        }
36
37
        return $node;
38
    }
39
}
40