|
14 | 14 | use GraphQL\Type\Definition\Type;
|
15 | 15 | use Overblog\GraphQLBundle\Resolver\Cache\ArrayCache;
|
16 | 16 | use Overblog\GraphQLBundle\Resolver\Cache\CacheInterface;
|
| 17 | +use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
17 | 19 |
|
18 |
| -class TypeResolver extends AbstractResolver |
| 20 | +class TypeResolver extends AbstractResolver implements ContainerAwareInterface |
19 | 21 | {
|
| 22 | + use ContainerAwareTrait; |
| 23 | + |
20 | 24 | /**
|
21 | 25 | * @var CacheInterface
|
22 | 26 | */
|
23 |
| - protected $cache; |
| 27 | + private $cache; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + private $mapping; |
24 | 33 |
|
25 | 34 | public function __construct(CacheInterface $cache = null)
|
26 | 35 | {
|
27 | 36 | $this->cache = null !== $cache ? $cache : new ArrayCache();
|
28 | 37 | }
|
29 | 38 |
|
| 39 | + /** |
| 40 | + * @param array $mapping |
| 41 | + * @return TypeResolver |
| 42 | + */ |
| 43 | + public function setMapping($mapping) |
| 44 | + { |
| 45 | + $this->mapping = $mapping; |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
30 | 49 | /**
|
31 | 50 | * @param string $alias
|
32 | 51 | *
|
@@ -64,13 +83,19 @@ private function getType($alias)
|
64 | 83 | }
|
65 | 84 |
|
66 | 85 | $type = $this->getSolution($alias);
|
67 |
| - if (null === $type) { |
68 |
| - throw new UnresolvableException( |
69 |
| - sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
70 |
| - ); |
| 86 | + if (null !== $type) { |
| 87 | + return $type; |
71 | 88 | }
|
72 | 89 |
|
73 |
| - return $type; |
| 90 | + //fallback load directly from container if exists |
| 91 | + if (null !== $this->container && isset($this->mapping[$alias])) { |
| 92 | + $options = $this->mapping[$alias]; |
| 93 | + return $this->container->get($options['id']); |
| 94 | + } |
| 95 | + |
| 96 | + throw new UnresolvableException( |
| 97 | + sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
| 98 | + ); |
74 | 99 | }
|
75 | 100 |
|
76 | 101 | protected function supportedSolutionClass()
|
|
0 commit comments