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\Schema; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use Overblog\GraphQLBundle\Resolver\Cache\ArrayCache; |
17
|
|
|
use Overblog\GraphQLBundle\Resolver\Cache\CacheInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
20
|
|
|
|
21
|
|
|
class TypeResolver extends AbstractResolver implements ContainerAwareInterface, TypeResolverInterface |
22
|
|
|
{ |
23
|
|
|
use ContainerAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CacheInterface |
27
|
|
|
*/ |
28
|
|
|
private $cache; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $mapping; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Schema |
37
|
|
|
*/ |
38
|
|
|
private $schema; |
39
|
|
|
|
40
|
47 |
|
public function __construct(CacheInterface $cache = null) |
41
|
|
|
{ |
42
|
47 |
|
$this->cache = null !== $cache ? $cache : new ArrayCache(); |
43
|
47 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $mapping |
47
|
|
|
* |
48
|
|
|
* @return TypeResolver |
49
|
|
|
*/ |
50
|
36 |
|
public function setMapping(array $mapping) |
51
|
|
|
{ |
52
|
36 |
|
$this->mapping = $mapping; |
53
|
|
|
|
54
|
36 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Schema $schema |
59
|
|
|
* |
60
|
|
|
* @return TypeResolver |
61
|
|
|
*/ |
62
|
37 |
|
public function setSchema(Schema $schema) |
63
|
|
|
{ |
64
|
37 |
|
$this->schema = $schema; |
65
|
37 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $alias |
69
|
|
|
* |
70
|
|
|
* @return \GraphQL\Type\Definition\Type |
71
|
|
|
*/ |
72
|
46 |
|
public function resolve($alias) |
73
|
|
|
{ |
74
|
46 |
|
if (null === $alias) { |
75
|
36 |
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
46 |
|
if (null !== $type = $this->cache->fetch($alias)) { |
79
|
36 |
|
return $type; |
80
|
|
|
} |
81
|
|
|
|
82
|
46 |
|
$type = $this->string2Type($alias); |
83
|
|
|
|
84
|
44 |
|
$this->cache->save($alias, $type); |
85
|
|
|
|
86
|
44 |
|
return $type; |
87
|
|
|
} |
88
|
|
|
|
89
|
46 |
|
private function string2Type($alias) |
90
|
|
|
{ |
91
|
46 |
|
if (false !== ($type = $this->wrapTypeIfNeeded($alias))) { |
92
|
24 |
|
return $type; |
93
|
|
|
} |
94
|
|
|
|
95
|
45 |
|
return $this->baseType($alias); |
96
|
|
|
} |
97
|
|
|
|
98
|
45 |
|
private function baseType($alias) |
99
|
|
|
{ |
100
|
|
|
$baseTypeResolvers = [ |
101
|
45 |
|
'baseTypeUsingSchema', // first search in schema if type exists |
102
|
45 |
|
'getSolution', // then in register solutions |
103
|
45 |
|
'baseTypeUsingContainer', // fallback using mapped services |
104
|
45 |
|
]; |
105
|
|
|
|
106
|
45 |
|
foreach ($baseTypeResolvers as $method) { |
107
|
45 |
|
$type = $this->$method($alias); |
108
|
45 |
|
if (null !== $type) { |
109
|
44 |
|
return $type; |
110
|
|
|
} |
111
|
44 |
|
} |
112
|
|
|
|
113
|
1 |
|
throw new UnresolvableException( |
114
|
1 |
|
sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
115
|
1 |
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
45 |
|
private function baseTypeUsingSchema($alias) |
119
|
|
|
{ |
120
|
45 |
|
if (null !== $this->schema) { |
121
|
9 |
|
return $this->schema->getType($alias); |
122
|
|
|
} |
123
|
44 |
|
} |
124
|
|
|
|
125
|
10 |
|
private function baseTypeUsingContainer($alias) |
126
|
|
|
{ |
127
|
10 |
|
if (null !== $this->container && isset($this->mapping[$alias])) { |
128
|
9 |
|
$options = $this->mapping[$alias]; |
129
|
|
|
|
130
|
9 |
|
return $this->container->get($options['id']); |
131
|
|
|
} |
132
|
1 |
|
} |
133
|
|
|
|
134
|
46 |
|
private function wrapTypeIfNeeded($alias) |
135
|
|
|
{ |
136
|
|
|
// Non-Null |
137
|
46 |
|
if ('!' === $alias[strlen($alias) - 1]) { |
138
|
13 |
|
return Type::nonNull($this->string2Type(substr($alias, 0, -1))); |
139
|
|
|
} |
140
|
|
|
// List |
141
|
46 |
|
if ($this->hasNeedListOfWrapper($alias)) { |
142
|
14 |
|
return Type::listOf($this->string2Type(substr($alias, 1, -1))); |
143
|
|
|
} |
144
|
|
|
|
145
|
45 |
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
46 |
|
private function hasNeedListOfWrapper($alias) |
149
|
|
|
{ |
150
|
46 |
|
if ('[' === $alias[0]) { |
151
|
15 |
|
$got = $alias[strlen($alias) - 1]; |
152
|
15 |
|
if (']' !== $got) { |
153
|
1 |
|
throw new UnresolvableException( |
154
|
1 |
|
sprintf('Malformed ListOf wrapper type "%s" expected "]" but got .', $alias, json_encode($got)) |
155
|
1 |
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
14 |
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
45 |
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
47 |
|
protected function supportedSolutionClass() |
165
|
|
|
{ |
166
|
47 |
|
return 'GraphQL\\Type\\Definition\\Type'; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|