|
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 !== $type = $this->cache->fetch($alias)) { |
|
57
|
26 |
|
return $type; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
35 |
|
$type = $this->getType($alias); |
|
61
|
|
|
|
|
62
|
33 |
|
$this->cache->save($alias, $type); |
|
63
|
|
|
|
|
64
|
33 |
|
return $type; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
35 |
|
private function getType($alias) |
|
68
|
|
|
{ |
|
69
|
35 |
|
if (!is_string($alias)) { |
|
70
|
26 |
|
return $alias; |
|
71
|
|
|
} |
|
72
|
|
|
// Non-Null |
|
73
|
35 |
|
if ('!' === $alias[strlen($alias) - 1]) { |
|
74
|
13 |
|
return Type::nonNull($this->getType(substr($alias, 0, -1))); |
|
75
|
|
|
} |
|
76
|
|
|
// List |
|
77
|
35 |
|
if ('[' === $alias[0]) { |
|
78
|
8 |
|
if (']' !== $alias[strlen($alias) - 1]) { |
|
79
|
1 |
|
throw new UnresolvableException(sprintf('Invalid type "%s"', $alias)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
7 |
|
return Type::listOf($this->getType(substr($alias, 1, -1))); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
34 |
|
$type = $this->getSolution($alias); |
|
86
|
34 |
|
if (null !== $type) { |
|
87
|
33 |
|
return $type; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
//fallback load directly from container if exists |
|
91
|
1 |
|
if (null !== $this->container && isset($this->mapping[$alias])) { |
|
92
|
|
|
$options = $this->mapping[$alias]; |
|
93
|
|
|
return $this->container->get($options['id']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
throw new UnresolvableException( |
|
97
|
1 |
|
sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
|
98
|
1 |
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
36 |
|
protected function supportedSolutionClass() |
|
102
|
|
|
{ |
|
103
|
36 |
|
return 'GraphQL\\Type\\Definition\\Type'; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|