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
|
93 |
|
public function setDispatcher(EventDispatcherInterface $dispatcher): void |
23
|
|
|
{ |
24
|
93 |
|
$this->dispatcher = $dispatcher; |
25
|
93 |
|
} |
26
|
|
|
|
27
|
84 |
|
public function setCurrentSchemaName(?string $currentSchemaName): void |
28
|
|
|
{ |
29
|
84 |
|
$this->currentSchemaName = $currentSchemaName; |
30
|
84 |
|
} |
31
|
|
|
|
32
|
97 |
|
protected function onLoadSolution($solution): void |
33
|
|
|
{ |
34
|
97 |
|
if (null !== $this->dispatcher) { |
35
|
89 |
|
$this->dispatcher->dispatch(Events::TYPE_LOADED, new TypeLoadedEvent($solution, $this->currentSchemaName)); |
36
|
|
|
} |
37
|
97 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $alias |
41
|
|
|
* |
42
|
|
|
* @return Type |
43
|
|
|
*/ |
44
|
98 |
|
public function resolve($alias): ?Type |
45
|
|
|
{ |
46
|
98 |
|
if (null === $alias) { |
47
|
83 |
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
98 |
|
if (!isset($this->cache[$alias])) { |
51
|
98 |
|
$type = $this->string2Type($alias); |
52
|
94 |
|
$this->cache[$alias] = $type; |
53
|
|
|
} |
54
|
|
|
|
55
|
94 |
|
return $this->cache[$alias]; |
56
|
|
|
} |
57
|
|
|
|
58
|
98 |
|
private function string2Type($alias) |
59
|
|
|
{ |
60
|
98 |
|
if (false !== ($type = $this->wrapTypeIfNeeded($alias))) { |
61
|
6 |
|
return $type; |
62
|
|
|
} |
63
|
|
|
|
64
|
97 |
|
return $this->baseType($alias); |
65
|
|
|
} |
66
|
|
|
|
67
|
97 |
|
private function baseType($alias) |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
97 |
|
$type = $this->getSolution($alias); |
71
|
2 |
|
} catch (\Throwable $error) { |
72
|
2 |
|
throw self::createTypeLoadingException($alias, $error); |
73
|
|
|
} |
74
|
|
|
|
75
|
95 |
|
if (null !== $type) { |
76
|
94 |
|
return $type; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
throw new UnresolvableException( |
80
|
1 |
|
\sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
98 |
|
private function wrapTypeIfNeeded($alias) |
85
|
|
|
{ |
86
|
|
|
// Non-Null |
87
|
98 |
|
if ('!' === $alias[\strlen($alias) - 1]) { |
88
|
4 |
|
return Type::nonNull($this->string2Type(\substr($alias, 0, -1))); |
89
|
|
|
} |
90
|
|
|
// List |
91
|
98 |
|
if ($this->hasNeedListOfWrapper($alias)) { |
92
|
5 |
|
return Type::listOf($this->string2Type(\substr($alias, 1, -1))); |
93
|
|
|
} |
94
|
|
|
|
95
|
97 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
98 |
|
private function hasNeedListOfWrapper($alias) |
99
|
|
|
{ |
100
|
98 |
|
if ('[' === $alias[0]) { |
101
|
6 |
|
$got = $alias[\strlen($alias) - 1]; |
102
|
6 |
|
if (']' !== $got) { |
103
|
1 |
|
throw new UnresolvableException( |
104
|
1 |
|
\sprintf('Malformed ListOf wrapper type "%s" expected "]" but got "%s".', $alias, \json_encode($got)) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
97 |
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $alias |
116
|
|
|
* @param \Throwable $errorOrException |
117
|
|
|
* |
118
|
|
|
* @return \RuntimeException |
119
|
|
|
*/ |
120
|
2 |
|
private static function createTypeLoadingException(string $alias, \Throwable $errorOrException): \RuntimeException |
121
|
|
|
{ |
122
|
2 |
|
return new \RuntimeException( |
123
|
2 |
|
\sprintf( |
124
|
2 |
|
'Type class for alias %s could not be load. If you are using your own classLoader verify the path and the namespace please.', |
125
|
2 |
|
\json_encode($alias) |
126
|
|
|
), |
127
|
2 |
|
0, |
128
|
2 |
|
$errorOrException |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
99 |
|
protected function supportedSolutionClass(): ?string |
133
|
|
|
{ |
134
|
99 |
|
return Type::class; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|