Passed
Pull Request — master (#164)
by Christoffer
02:18
created

SchemaExtender::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Schema\Extension;
4
5
use Digia\GraphQL\Error\ExtensionException;
6
use Digia\GraphQL\Error\InvariantException;
7
use Digia\GraphQL\Language\Node\DocumentNode;
8
use Digia\GraphQL\Schema\SchemaInterface;
9
use Psr\SimpleCache\InvalidArgumentException;
10
use function Digia\GraphQL\Type\GraphQLSchema;
11
12
class SchemaExtender implements SchemaExtenderInterface
13
{
14
    /**
15
     * @var ExtensionContextCreatorInterface
16
     */
17
    protected $contextCreator;
18
19
    /**
20
     * SchemaExtender constructor.
21
     * @param ExtensionContextCreatorInterface $contextCreator
22
     */
23
    public function __construct(ExtensionContextCreatorInterface $contextCreator)
24
    {
25
        $this->contextCreator = $contextCreator;
26
    }
27
28
    /**
29
     * @param SchemaInterface $schema
30
     * @param DocumentNode    $document
31
     * @return SchemaInterface
32
     * @throws InvariantException
33
     * @throws ExtensionException
34
     * @throws InvalidArgumentException
35
     */
36
    public function extend(SchemaInterface $schema, DocumentNode $document): SchemaInterface
37
    {
38
        $context = $this->contextCreator->create($schema, $document);
39
40
        // If this document contains no new types, extensions, or directives then
41
        // return the same unmodified GraphQLSchema instance.
42
        if (!$context->isSchemaExtended()) {
43
            return $schema;
44
        }
45
46
        return GraphQLSchema([
47
            'query'        => $context->getExtendedQueryType(),
48
            'mutation'     => $context->getExtendedMutationType(),
49
            'subscription' => $context->getExtendedSubscriptionType(),
50
            'types'        => $context->getExtendedTypes(),
51
            'directives'   => $context->getExtendedDirectives(),
52
            'astNode'      => $schema->getAstNode(),
53
        ]);
54
    }
55
}
56