1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
8
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
9
|
|
|
use Overblog\GraphQLBundle\Event\TypeLoadedEvent; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
|
12
|
|
|
class TypeResolver extends AbstractResolver |
13
|
|
|
{ |
14
|
|
|
private $cache = []; |
15
|
|
|
|
16
|
|
|
/** @var string|null */ |
17
|
|
|
private $currentSchemaName; |
18
|
|
|
|
19
|
|
|
/** @var EventDispatcherInterface */ |
20
|
|
|
private $dispatcher; |
21
|
|
|
|
22
|
102 |
|
public function setDispatcher(EventDispatcherInterface $dispatcher): void |
23
|
|
|
{ |
24
|
102 |
|
$this->dispatcher = $dispatcher; |
25
|
102 |
|
} |
26
|
|
|
|
27
|
93 |
|
public function setCurrentSchemaName(?string $currentSchemaName): void |
28
|
|
|
{ |
29
|
93 |
|
$this->currentSchemaName = $currentSchemaName; |
30
|
93 |
|
} |
31
|
|
|
|
32
|
106 |
|
protected function onLoadSolution($solution): void |
33
|
|
|
{ |
34
|
106 |
|
if (null !== $this->dispatcher) { |
35
|
98 |
|
$this->dispatcher->dispatch(Events::TYPE_LOADED, new TypeLoadedEvent($solution, $this->currentSchemaName)); |
36
|
|
|
} |
37
|
106 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $alias |
41
|
|
|
* |
42
|
|
|
* @return Type |
43
|
|
|
*/ |
44
|
106 |
|
public function resolve($alias): ?Type |
45
|
|
|
{ |
46
|
106 |
|
if (null === $alias) { |
47
|
92 |
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
106 |
|
if (!isset($this->cache[$alias])) { |
51
|
106 |
|
$type = $this->string2Type($alias); |
52
|
103 |
|
$this->cache[$alias] = $type; |
53
|
|
|
} |
54
|
|
|
|
55
|
103 |
|
return $this->cache[$alias]; |
56
|
|
|
} |
57
|
|
|
|
58
|
106 |
|
private function string2Type(string $alias): Type |
59
|
|
|
{ |
60
|
106 |
|
if (null !== ($type = $this->wrapTypeIfNeeded($alias))) { |
61
|
6 |
|
return $type; |
62
|
|
|
} |
63
|
|
|
|
64
|
105 |
|
return $this->baseType($alias); |
65
|
|
|
} |
66
|
|
|
|
67
|
105 |
|
private function baseType($alias): Type |
68
|
|
|
{ |
69
|
105 |
|
$type = $this->getSolution($alias); |
70
|
104 |
|
if (null === $type) { |
71
|
4 |
|
throw new UnresolvableException( |
72
|
4 |
|
\sprintf('Could not found type with alias "%s". Do you forget to define it?', $alias) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
103 |
|
return $type; |
77
|
|
|
} |
78
|
|
|
|
79
|
106 |
|
private function wrapTypeIfNeeded($alias): ?Type |
80
|
|
|
{ |
81
|
|
|
// Non-Null |
82
|
106 |
|
if ('!' === $alias[\strlen($alias) - 1]) { |
83
|
4 |
|
return Type::nonNull($this->string2Type(\substr($alias, 0, -1))); |
84
|
|
|
} |
85
|
|
|
// List |
86
|
106 |
|
if ($this->hasNeedListOfWrapper($alias)) { |
87
|
5 |
|
return Type::listOf($this->string2Type(\substr($alias, 1, -1))); |
88
|
|
|
} |
89
|
|
|
|
90
|
105 |
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
106 |
|
private function hasNeedListOfWrapper($alias): bool |
94
|
|
|
{ |
95
|
106 |
|
if ('[' === $alias[0]) { |
96
|
6 |
|
$got = $alias[\strlen($alias) - 1]; |
97
|
6 |
|
if (']' !== $got) { |
98
|
1 |
|
throw new UnresolvableException( |
99
|
1 |
|
\sprintf('Malformed ListOf wrapper type "%s" expected "]" but got "%s".', $alias, \json_encode($got)) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
5 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
105 |
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
107 |
|
protected function supportedSolutionClass(): ?string |
110
|
|
|
{ |
111
|
107 |
|
return Type::class; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|