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