|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Definition\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\Type; |
|
6
|
|
|
use Overblog\GraphQLBundle\Definition\Type\ExtensibleSchema; |
|
7
|
|
|
use Overblog\GraphQLBundle\Definition\Type\SchemaExtension\DecoratorExtension; |
|
8
|
|
|
use Overblog\GraphQLBundle\Definition\Type\SchemaExtension\ValidatorExtension; |
|
9
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverMapInterface; |
|
10
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverMaps; |
|
11
|
|
|
use Overblog\GraphQLBundle\Resolver\TypeResolver; |
|
12
|
|
|
|
|
13
|
|
|
class SchemaBuilder |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var TypeResolver */ |
|
16
|
|
|
private $typeResolver; |
|
17
|
|
|
|
|
18
|
|
|
/** @var bool */ |
|
19
|
|
|
private $enableValidation; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(TypeResolver $typeResolver, $enableValidation = false) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->typeResolver = $typeResolver; |
|
24
|
|
|
$this->enableValidation = $enableValidation; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param null|string $queryAlias |
|
29
|
|
|
* @param null|string $mutationAlias |
|
30
|
|
|
* @param null|string $subscriptionAlias |
|
31
|
|
|
* @param ResolverMapInterface[] $resolverMaps |
|
32
|
|
|
* @param string[] $types |
|
33
|
|
|
* |
|
34
|
|
|
* @return ExtensibleSchema |
|
35
|
|
|
*/ |
|
36
|
|
|
public function create($queryAlias = null, $mutationAlias = null, $subscriptionAlias = null, array $resolverMaps = [], array $types = []) |
|
37
|
|
|
{ |
|
38
|
|
|
$query = $this->typeResolver->resolve($queryAlias); |
|
39
|
|
|
$mutation = $this->typeResolver->resolve($mutationAlias); |
|
40
|
|
|
$subscription = $this->typeResolver->resolve($subscriptionAlias); |
|
41
|
|
|
|
|
42
|
|
|
$schema = new ExtensibleSchema($this->buildSchemaArguments($query, $mutation, $subscription, $types)); |
|
43
|
|
|
$extensions = [new DecoratorExtension(1 === count($resolverMaps) ? current($resolverMaps) : new ResolverMaps($resolverMaps))]; |
|
44
|
|
|
|
|
45
|
|
|
if ($this->enableValidation) { |
|
46
|
|
|
$extensions[] = new ValidatorExtension(); |
|
47
|
|
|
} |
|
48
|
|
|
$schema->setExtensions($extensions); |
|
49
|
|
|
|
|
50
|
|
|
return $schema; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function buildSchemaArguments(Type $query = null, Type $mutation = null, Type $subscription = null, array $types = []) |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
'query' => $query, |
|
57
|
|
|
'mutation' => $mutation, |
|
58
|
|
|
'subscription' => $subscription, |
|
59
|
|
|
'typeLoader' => function ($name) { |
|
60
|
|
|
return $this->typeResolver->resolve($name); |
|
61
|
|
|
}, |
|
62
|
|
|
'types' => function () use ($types) { |
|
63
|
|
|
return array_map([$this->typeResolver, 'getSolution'], $types); |
|
64
|
|
|
}, |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|