Passed
Pull Request — master (#171)
by Christoffer
02:11
created

SchemaExtender::createContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\DefinitionBuilderCreatorInterface;
9
use Digia\GraphQL\Schema\SchemaInterface;
10
use Psr\SimpleCache\InvalidArgumentException;
11
use function Digia\GraphQL\Type\newSchema;
12
13
class SchemaExtender implements SchemaExtenderInterface
14
{
15
    /**
16
     * @var DefinitionBuilderCreatorInterface
17
     */
18
    protected $definitionBuilderCreator;
19
20
    /**
21
     * BuilderContextCreator constructor.
22
     * @param DefinitionBuilderCreatorInterface $definitionBuilderCreator
23
     */
24
    public function __construct(DefinitionBuilderCreatorInterface $definitionBuilderCreator)
25
    {
26
        $this->definitionBuilderCreator = $definitionBuilderCreator;
27
    }
28
29
    /**
30
     * @param SchemaInterface $schema
31
     * @param DocumentNode    $document
32
     * @return SchemaInterface
33
     * @throws InvariantException
34
     * @throws ExtensionException
35
     * @throws InvalidArgumentException
36
     */
37
    public function extend(SchemaInterface $schema, DocumentNode $document): SchemaInterface
38
    {
39
        $context = $this->createContext($schema, $document);
40
41
        // If this document contains no new types, extensions, or directives then
42
        // return the same unmodified GraphQLSchema instance.
43
        if (!$context->isSchemaExtended()) {
44
            return $schema;
45
        }
46
47
        return newSchema([
48
            'query'        => $context->getExtendedQueryType(),
49
            'mutation'     => $context->getExtendedMutationType(),
50
            'subscription' => $context->getExtendedSubscriptionType(),
51
            'types'        => $context->getExtendedTypes(),
52
            'directives'   => $context->getExtendedDirectives(),
53
            'astNode'      => $schema->getAstNode(),
54
        ]);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function createContext(SchemaInterface $schema, DocumentNode $document): ExtensionContextInterface
61
    {
62
        $context = new ExtensionContext($schema, $document, $this->definitionBuilderCreator);
63
64
        $context->boot();
65
66
        return $context;
67
    }
68
}
69