Test Setup Failed
Push — master ( e8c39a...45e116 )
by Kirill
02:51 queued 12s
created

SchemaExtensionExecutor::enter()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
nc 4
nop 1
dl 0
loc 37
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Executor\Extension;
13
14
use GraphQL\TypeSystem\Schema;
15
use Phplrt\Contracts\Ast\NodeInterface;
16
use Railt\SDL\Ast\Definition\OperationTypeDefinitionNode;
17
use Railt\SDL\Ast\Extension\SchemaExtensionNode;
18
19
/**
20
 * Class SchemaExtensionExecutor
21
 */
22
class SchemaExtensionExecutor extends ExtensionExecutor
23
{
24
    /**
25
     * @param NodeInterface|SchemaExtensionNode $source
26
     * @return mixed|void|null
27
     */
28
    public function enter(NodeInterface $source)
29
    {
30
        if (! $source instanceof SchemaExtensionNode) {
31
            return;
32
        }
33
34
        /** @var Schema $target */
35
        $target = $this->document->getSchema();
36
37
        if (! $target instanceof Schema) {
0 ignored issues
show
introduced by
$target is always a sub-type of GraphQL\TypeSystem\Schema.
Loading history...
38
            // TODO should throw an error
39
            return;
40
        }
41
42
        if ($source->operationTypes) {
43
            /** @var OperationTypeDefinitionNode $operation */
44
            foreach ($source->operationTypes as $operation) {
45
                // TODO check types
46
                $type = $this->fetch($operation->type->name->value);
47
48
                switch ($operation->operation) {
49
                    case 'query':
50
                        $target = $target->withQueryType($type);
51
                        break;
52
53
                    case 'mutation':
54
                        $target = $target->withMutationType($type);
55
                        break;
56
57
                    case 'subscription':
58
                        $target = $target->withSubscriptionType($type);
59
                        break;
60
                }
61
            }
62
        }
63
64
        $this->document->setSchema($target);
65
    }
66
}
67