1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\Type; |
6
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
7
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
8
|
|
|
|
9
|
|
|
class TypeResolver extends AbstractResolver |
10
|
|
|
{ |
11
|
|
|
/** @var CacheItemPoolInterface */ |
12
|
|
|
private $cacheAdapter; |
13
|
|
|
|
14
|
82 |
|
public function __construct(CacheItemPoolInterface $cacheAdapter = null) |
15
|
|
|
{ |
16
|
82 |
|
$this->cacheAdapter = null !== $cacheAdapter ? $cacheAdapter : new ArrayAdapter(0, false); |
17
|
82 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $alias |
21
|
|
|
* |
22
|
|
|
* @return Type |
23
|
|
|
*/ |
24
|
75 |
|
public function resolve($alias) |
25
|
|
|
{ |
26
|
75 |
|
if (null === $alias) { |
27
|
63 |
|
return; |
28
|
|
|
} |
29
|
75 |
|
$item = $this->cacheAdapter->getItem(md5($alias)); |
30
|
|
|
|
31
|
75 |
|
if (!$item->isHit()) { |
32
|
75 |
|
$type = $this->string2Type($alias); |
33
|
72 |
|
$item->set($type); |
34
|
72 |
|
$this->cacheAdapter->save($item); |
35
|
|
|
} |
36
|
|
|
|
37
|
72 |
|
return $item->get(); |
38
|
|
|
} |
39
|
|
|
|
40
|
75 |
|
private function string2Type($alias) |
41
|
|
|
{ |
42
|
75 |
|
if (false !== ($type = $this->wrapTypeIfNeeded($alias))) { |
43
|
6 |
|
return $type; |
44
|
|
|
} |
45
|
|
|
|
46
|
74 |
|
return $this->baseType($alias); |
47
|
|
|
} |
48
|
|
|
|
49
|
74 |
|
private function baseType($alias) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
74 |
|
$type = $this->getSolution($alias); |
53
|
1 |
|
} catch (\Error $error) { |
54
|
1 |
|
throw self::createTypeLoadingException($alias, $error); |
|
|
|
|
55
|
|
|
} catch (\Exception $exception) { |
56
|
|
|
throw self::createTypeLoadingException($alias, $exception); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
73 |
|
if (null !== $type) { |
60
|
72 |
|
return $type; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
throw new UnresolvableException( |
64
|
1 |
|
sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
75 |
|
private function wrapTypeIfNeeded($alias) |
69
|
|
|
{ |
70
|
|
|
// Non-Null |
71
|
75 |
|
if ('!' === $alias[strlen($alias) - 1]) { |
72
|
4 |
|
return Type::nonNull($this->string2Type(substr($alias, 0, -1))); |
73
|
|
|
} |
74
|
|
|
// List |
75
|
75 |
|
if ($this->hasNeedListOfWrapper($alias)) { |
76
|
5 |
|
return Type::listOf($this->string2Type(substr($alias, 1, -1))); |
77
|
|
|
} |
78
|
|
|
|
79
|
74 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
75 |
|
private function hasNeedListOfWrapper($alias) |
83
|
|
|
{ |
84
|
75 |
|
if ('[' === $alias[0]) { |
85
|
6 |
|
$got = $alias[strlen($alias) - 1]; |
86
|
6 |
|
if (']' !== $got) { |
87
|
1 |
|
throw new UnresolvableException( |
88
|
1 |
|
sprintf('Malformed ListOf wrapper type "%s" expected "]" but got "%s".', $alias, json_encode($got)) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
74 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $alias |
100
|
|
|
* @param \Throwable $errorOrException |
101
|
|
|
* |
102
|
|
|
* @return \RuntimeException |
103
|
|
|
*/ |
104
|
1 |
|
private static function createTypeLoadingException($alias, $errorOrException) |
105
|
|
|
{ |
106
|
1 |
|
return new \RuntimeException( |
107
|
1 |
|
sprintf( |
108
|
1 |
|
'Type class for alias %s could not be load. If you are using your own classLoader verify the path and the namespace please.', |
109
|
1 |
|
json_encode($alias) |
110
|
|
|
), |
111
|
1 |
|
0, |
112
|
1 |
|
$errorOrException |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
75 |
|
protected function postLoadSolution($solution) |
117
|
|
|
{ |
118
|
|
|
// also add solution with real type name if needed for typeLoader when using autoMapping |
119
|
75 |
|
if ($solution && !$this->hasSolution($solution->name)) { |
120
|
2 |
|
$this->addSolution($solution->name, function () use ($solution) { |
121
|
2 |
|
return $solution; |
122
|
2 |
|
}); |
123
|
|
|
} |
124
|
75 |
|
} |
125
|
|
|
|
126
|
76 |
|
protected function supportedSolutionClass() |
127
|
|
|
{ |
128
|
76 |
|
return Type::class; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: