1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Definition; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
8
|
|
|
use LogicException; |
9
|
|
|
use Overblog\GraphQLBundle\Resolver\MutationResolver; |
10
|
|
|
use Overblog\GraphQLBundle\Resolver\QueryResolver; |
11
|
|
|
use Overblog\GraphQLBundle\Resolver\TypeResolver; |
12
|
|
|
use ReflectionException; |
13
|
|
|
use ReflectionMethod; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Container for special services to be passed to all generated types. |
17
|
|
|
*/ |
18
|
|
|
final class GraphQLServices |
19
|
|
|
{ |
20
|
|
|
private array $services; |
21
|
|
|
private TypeResolver $types; |
22
|
|
|
private QueryResolver $queryResolver; |
23
|
|
|
private MutationResolver $mutationResolver; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
TypeResolver $typeResolver, |
27
|
|
|
QueryResolver $queryResolver, |
28
|
|
|
MutationResolver $mutationResolver, |
29
|
|
|
array $services = [] |
30
|
|
|
) { |
31
|
|
|
$this->types = $typeResolver; |
32
|
|
|
$this->queryResolver = $queryResolver; |
33
|
|
|
$this->mutationResolver = $mutationResolver; |
34
|
|
|
$this->services = $services; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function get(string $name) |
41
|
|
|
{ |
42
|
|
|
if (!isset($this->services[$name])) { |
43
|
|
|
throw new LogicException(sprintf('GraphQL service "%s" could not be located. You should define it.', $name)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->services[$name]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get all GraphQL services. |
51
|
|
|
*/ |
52
|
|
|
public function getAll(): array |
53
|
|
|
{ |
54
|
|
|
return $this->services; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function has(string $name): bool |
58
|
|
|
{ |
59
|
|
|
return isset($this->services[$name]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed ...$args |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function query(string $alias, ...$args) |
68
|
|
|
{ |
69
|
|
|
return $this->queryResolver->resolve([$alias, $args]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param mixed ...$args |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
* @throws ReflectionException |
77
|
|
|
*/ |
78
|
|
|
public function mutation(string $alias, ...$args) |
79
|
|
|
{ |
80
|
|
|
// TODO: remove the following if-block in 1.0 |
81
|
|
|
if (1 === count($args) && is_array($args[0])) { |
82
|
|
|
$aliases = $this->mutationResolver->getAliases(); |
83
|
|
|
|
84
|
|
|
if (isset($aliases[$alias])) { |
85
|
|
|
// Check if proxy resolver has the same amount of params |
86
|
|
|
$numOfParams = (new ReflectionMethod($aliases[$alias]))->getNumberOfParameters(); |
87
|
|
|
if (count($args[0]) === $numOfParams) { |
88
|
|
|
$args = $args[0]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->mutationResolver->resolve([$alias, $args]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getType(string $typeName): ?Type |
97
|
|
|
{ |
98
|
|
|
return $this->types->resolve($typeName); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|