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
|
|
|
|