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 Overblog\GraphQLBundle\Resolver\Cache\ArrayCache; |
16
|
|
|
use Overblog\GraphQLBundle\Resolver\Cache\CacheInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
19
|
|
|
|
20
|
|
|
class TypeResolver extends AbstractResolver implements ContainerAwareInterface |
21
|
|
|
{ |
22
|
|
|
use ContainerAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var CacheInterface |
26
|
|
|
*/ |
27
|
|
|
private $cache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $mapping; |
33
|
|
|
|
34
|
36 |
|
public function __construct(CacheInterface $cache = null) |
35
|
|
|
{ |
36
|
36 |
|
$this->cache = null !== $cache ? $cache : new ArrayCache(); |
37
|
36 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $mapping |
41
|
|
|
* @return TypeResolver |
42
|
|
|
*/ |
43
|
26 |
|
public function setMapping($mapping) |
44
|
|
|
{ |
45
|
26 |
|
$this->mapping = $mapping; |
46
|
26 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $alias |
51
|
|
|
* |
52
|
|
|
* @return \GraphQL\Type\Definition\Type |
53
|
|
|
*/ |
54
|
35 |
|
public function resolve($alias) |
55
|
|
|
{ |
56
|
35 |
|
if (null === $alias) { |
57
|
26 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
35 |
|
if (null !== $type = $this->cache->fetch($alias)) { |
61
|
26 |
|
return $type; |
62
|
|
|
} |
63
|
|
|
|
64
|
35 |
|
$type = $this->string2Type($alias); |
65
|
|
|
|
66
|
33 |
|
$this->cache->save($alias, $type); |
67
|
|
|
|
68
|
33 |
|
return $type; |
69
|
|
|
} |
70
|
|
|
|
71
|
35 |
|
private function string2Type($alias) |
72
|
|
|
{ |
73
|
35 |
|
if (false !== ($type = $this->wrapTypeIfNeeded($alias))) { |
74
|
17 |
|
return $type; |
75
|
|
|
} |
76
|
|
|
|
77
|
34 |
|
return $this->baseType($alias); |
78
|
|
|
} |
79
|
|
|
|
80
|
34 |
|
private function baseType($alias) |
81
|
|
|
{ |
82
|
34 |
|
$type = $this->getSolution($alias); |
83
|
34 |
|
if (null !== $type) { |
84
|
33 |
|
return $type; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//fallback load directly from container if exists |
88
|
1 |
|
if (null !== $this->container && isset($this->mapping[$alias])) { |
89
|
|
|
$options = $this->mapping[$alias]; |
90
|
|
|
return $this->container->get($options['id']); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
throw new UnresolvableException( |
94
|
1 |
|
sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
95
|
1 |
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
35 |
|
private function wrapTypeIfNeeded($alias) |
99
|
|
|
{ |
100
|
|
|
// Non-Null |
101
|
35 |
|
if ('!' === $alias[strlen($alias) - 1]) { |
102
|
13 |
|
return Type::nonNull($this->string2Type(substr($alias, 0, -1))); |
103
|
|
|
} |
104
|
|
|
// List |
105
|
35 |
|
if ($this->hasNeedListOfWrapper($alias)) { |
106
|
7 |
|
return Type::listOf($this->string2Type(substr($alias, 1, -1))); |
107
|
|
|
} |
108
|
|
|
|
109
|
34 |
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
35 |
|
private function hasNeedListOfWrapper($alias) |
113
|
|
|
{ |
114
|
35 |
|
if ('[' === $alias[0]) { |
115
|
8 |
|
$got = $alias[strlen($alias) - 1]; |
116
|
8 |
|
if (']' !== $got) { |
117
|
1 |
|
throw new UnresolvableException( |
118
|
1 |
|
sprintf('Malformed ListOf wrapper type "%s" expected "]" but got .', $alias, json_encode($got)) |
119
|
1 |
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
7 |
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
34 |
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
36 |
|
protected function supportedSolutionClass() |
129
|
|
|
{ |
130
|
36 |
|
return 'GraphQL\\Type\\Definition\\Type'; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|