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

SchemaExtender   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A extend() 0 17 2
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