|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Definition\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
|
8
|
|
|
use Overblog\GraphQLBundle\Definition\Type\ExtensibleSchema; |
|
9
|
|
|
use Overblog\GraphQLBundle\Definition\Type\SchemaExtension\ValidatorExtension; |
|
10
|
|
|
use Overblog\GraphQLBundle\Resolver\TypeResolver; |
|
11
|
|
|
|
|
12
|
|
|
class SchemaBuilder |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var TypeResolver */ |
|
15
|
|
|
private $typeResolver; |
|
16
|
|
|
|
|
17
|
|
|
/** @var bool */ |
|
18
|
|
|
private $enableValidation; |
|
19
|
|
|
|
|
20
|
96 |
|
public function __construct(TypeResolver $typeResolver, bool $enableValidation = false) |
|
21
|
|
|
{ |
|
22
|
96 |
|
$this->typeResolver = $typeResolver; |
|
23
|
96 |
|
$this->enableValidation = $enableValidation; |
|
24
|
96 |
|
} |
|
25
|
|
|
|
|
26
|
96 |
|
public function getBuilder(string $name, ?string $queryAlias, ?string $mutationAlias = null, ?string $subscriptionAlias = null, array $types = []): callable |
|
27
|
|
|
{ |
|
28
|
|
|
return function () use ($name, $queryAlias, $mutationAlias, $subscriptionAlias, $types): ExtensibleSchema { |
|
29
|
88 |
|
static $schema = null; |
|
30
|
88 |
|
if (null === $schema) { |
|
31
|
88 |
|
$schema = $this->create($name, $queryAlias, $mutationAlias, $subscriptionAlias, $types); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
87 |
|
return $schema; |
|
35
|
96 |
|
}; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param string|null $queryAlias |
|
41
|
|
|
* @param string|null $mutationAlias |
|
42
|
|
|
* @param string|null $subscriptionAlias |
|
43
|
|
|
* @param string[] $types |
|
44
|
|
|
* |
|
45
|
|
|
* @return ExtensibleSchema |
|
46
|
|
|
*/ |
|
47
|
88 |
|
public function create(string $name, ?string $queryAlias, ?string $mutationAlias = null, ?string $subscriptionAlias = null, array $types = []): ExtensibleSchema |
|
48
|
|
|
{ |
|
49
|
88 |
|
$this->typeResolver->setCurrentSchemaName($name); |
|
50
|
88 |
|
$query = $this->typeResolver->resolve($queryAlias); |
|
51
|
87 |
|
$mutation = $this->typeResolver->resolve($mutationAlias); |
|
52
|
87 |
|
$subscription = $this->typeResolver->resolve($subscriptionAlias); |
|
53
|
|
|
|
|
54
|
87 |
|
$schema = new ExtensibleSchema($this->buildSchemaArguments($name, $query, $mutation, $subscription, $types)); |
|
|
|
|
|
|
55
|
87 |
|
$extensions = []; |
|
56
|
|
|
|
|
57
|
87 |
|
if ($this->enableValidation) { |
|
58
|
81 |
|
$extensions[] = new ValidatorExtension(); |
|
59
|
|
|
} |
|
60
|
87 |
|
$schema->setExtensions($extensions); |
|
61
|
|
|
|
|
62
|
87 |
|
return $schema; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
87 |
|
private function buildSchemaArguments(string $schemaName, Type $query, ?Type $mutation, ?Type $subscription, array $types = []): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
87 |
|
'query' => $query, |
|
69
|
87 |
|
'mutation' => $mutation, |
|
70
|
87 |
|
'subscription' => $subscription, |
|
71
|
|
|
'typeLoader' => function ($name) use ($schemaName) { |
|
72
|
86 |
|
$this->typeResolver->setCurrentSchemaName($schemaName); |
|
73
|
|
|
|
|
74
|
86 |
|
return $this->typeResolver->resolve($name); |
|
75
|
87 |
|
}, |
|
76
|
|
|
'types' => function () use ($types, $schemaName) { |
|
77
|
85 |
|
$this->typeResolver->setCurrentSchemaName($schemaName); |
|
78
|
|
|
|
|
79
|
85 |
|
return \array_map([$this->typeResolver, 'resolve'], $types); |
|
80
|
87 |
|
}, |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|