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