|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\SchemaExtension; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ExtensionException; |
|
6
|
|
|
use Digia\GraphQL\Error\InvariantException; |
|
7
|
|
|
use Digia\GraphQL\Language\Node\DocumentNode; |
|
8
|
|
|
use Digia\GraphQL\Type\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
|
|
|
|