1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Definition\Type; |
15
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
16
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
17
|
|
|
|
18
|
|
|
class TypeResolver extends AbstractResolver |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var CacheItemPoolInterface |
22
|
|
|
*/ |
23
|
|
|
private $cacheAdapter; |
24
|
|
|
|
25
|
26 |
|
public function __construct(CacheItemPoolInterface $cacheAdapter = null) |
26
|
|
|
{ |
27
|
26 |
|
$this->cacheAdapter = null !== $cacheAdapter ? $cacheAdapter : new ArrayAdapter(0, false); |
28
|
26 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $alias |
32
|
|
|
* |
33
|
|
|
* @return \GraphQL\Type\Definition\Type |
34
|
|
|
*/ |
35
|
32 |
|
public function resolve($alias) |
36
|
|
|
{ |
37
|
32 |
|
if (null === $alias) { |
38
|
15 |
|
return; |
39
|
|
|
} |
40
|
32 |
|
$item = $this->cacheAdapter->getItem(md5($alias)); |
41
|
|
|
|
42
|
32 |
|
if (!$item->isHit()) { |
43
|
27 |
|
$type = $this->string2Type($alias); |
44
|
25 |
|
$item->set($type); |
45
|
25 |
|
$this->cacheAdapter->save($item); |
46
|
25 |
|
} |
47
|
|
|
|
48
|
30 |
|
return $item->get(); |
49
|
|
|
} |
50
|
|
|
|
51
|
27 |
|
private function string2Type($alias) |
52
|
|
|
{ |
53
|
27 |
|
if (false !== ($type = $this->wrapTypeIfNeeded($alias))) { |
54
|
6 |
|
return $type; |
55
|
|
|
} |
56
|
|
|
|
57
|
26 |
|
return $this->baseType($alias); |
58
|
|
|
} |
59
|
|
|
|
60
|
26 |
|
private function baseType($alias) |
61
|
|
|
{ |
62
|
26 |
|
$type = $this->getSolution($alias); |
63
|
26 |
|
if (null !== $type) { |
64
|
25 |
|
return $type; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
throw new UnresolvableException( |
68
|
1 |
|
sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
69
|
1 |
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
27 |
|
private function wrapTypeIfNeeded($alias) |
73
|
|
|
{ |
74
|
|
|
// Non-Null |
75
|
27 |
|
if ('!' === $alias[strlen($alias) - 1]) { |
76
|
4 |
|
return Type::nonNull($this->string2Type(substr($alias, 0, -1))); |
77
|
|
|
} |
78
|
|
|
// List |
79
|
27 |
|
if ($this->hasNeedListOfWrapper($alias)) { |
80
|
5 |
|
return Type::listOf($this->string2Type(substr($alias, 1, -1))); |
81
|
|
|
} |
82
|
|
|
|
83
|
26 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
27 |
|
private function hasNeedListOfWrapper($alias) |
87
|
|
|
{ |
88
|
27 |
|
if ('[' === $alias[0]) { |
89
|
6 |
|
$got = $alias[strlen($alias) - 1]; |
90
|
6 |
|
if (']' !== $got) { |
91
|
1 |
|
throw new UnresolvableException( |
92
|
1 |
|
sprintf('Malformed ListOf wrapper type "%s" expected "]" but got .', $alias, json_encode($got)) |
93
|
1 |
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
26 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
23 |
|
protected function postLoadSolution($solution) |
103
|
|
|
{ |
104
|
|
|
// also add solution with real type name if needed for typeLoader when using autoMapping |
105
|
23 |
|
if ($solution && !isset($this->getSolutions()[$solution->name])) { |
106
|
1 |
|
$this->addSolution($solution->name, function () use ($solution) { |
107
|
1 |
|
return $solution; |
108
|
1 |
|
}); |
109
|
1 |
|
} |
110
|
23 |
|
} |
111
|
|
|
|
112
|
24 |
|
protected function supportedSolutionClass() |
113
|
|
|
{ |
114
|
24 |
|
return Type::class; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|