Skip to content

Commit bf56296

Browse files
author
Jeremiah VALERIE
committed
Add fallback in TypeResolver to load type from not required container
1 parent 087fbec commit bf56296

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

DependencyInjection/Configurator/ResolverConfigurator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ private function configure(AbstractResolver $resolver, array $mapping)
3030
{
3131
foreach ($mapping as $name => $options) {
3232
$cleanOptions = $options;
33-
unset($cleanOptions['alias'], $cleanOptions['id']);
34-
3533
$solution = $this->container->get($options['id']);
3634

3735
if ($solution instanceof ContainerAwareInterface) {

Resolver/TypeResolver.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,38 @@
1414
use GraphQL\Type\Definition\Type;
1515
use Overblog\GraphQLBundle\Resolver\Cache\ArrayCache;
1616
use Overblog\GraphQLBundle\Resolver\Cache\CacheInterface;
17+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1719

18-
class TypeResolver extends AbstractResolver
20+
class TypeResolver extends AbstractResolver implements ContainerAwareInterface
1921
{
22+
use ContainerAwareTrait;
23+
2024
/**
2125
* @var CacheInterface
2226
*/
23-
protected $cache;
27+
private $cache;
28+
29+
/**
30+
* @var array
31+
*/
32+
private $mapping;
2433

2534
public function __construct(CacheInterface $cache = null)
2635
{
2736
$this->cache = null !== $cache ? $cache : new ArrayCache();
2837
}
2938

39+
/**
40+
* @param array $mapping
41+
* @return TypeResolver
42+
*/
43+
public function setMapping($mapping)
44+
{
45+
$this->mapping = $mapping;
46+
return $this;
47+
}
48+
3049
/**
3150
* @param string $alias
3251
*
@@ -64,13 +83,19 @@ private function getType($alias)
6483
}
6584

6685
$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;
7188
}
7289

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+
);
7499
}
75100

76101
protected function supportedSolutionClass()

Resources/config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ services:
7070
overblog_graphql.type_resolver:
7171
class: Overblog\GraphQLBundle\Resolver\TypeResolver
7272
configurator: ["@overblog_graphql.resolver_configurator", "configureType"]
73+
arguments:
74+
-
75+
calls:
76+
- ["setContainer", ["@service_container"]]
77+
- ["setMapping", [%overblog_graphql.types_mapping%]]
7378

7479
overblog_graphql.field_resolver:
7580
class: Overblog\GraphQLBundle\Resolver\FieldResolver

0 commit comments

Comments
 (0)