|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
|
8
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
|
9
|
|
|
use Overblog\GraphQLBundle\Event\TypeLoadedEvent; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
11
|
|
|
use function sprintf; |
|
12
|
|
|
|
|
13
|
|
|
class TypeResolver extends AbstractResolver |
|
14
|
|
|
{ |
|
15
|
|
|
private array $cache = []; |
|
16
|
|
|
private ?string $currentSchemaName = null; |
|
17
|
|
|
private EventDispatcherInterface $dispatcher; |
|
18
|
|
|
|
|
19
|
125 |
|
public function setDispatcher(EventDispatcherInterface $dispatcher): void |
|
20
|
|
|
{ |
|
21
|
125 |
|
$this->dispatcher = $dispatcher; |
|
22
|
125 |
|
} |
|
23
|
|
|
|
|
24
|
107 |
|
public function setCurrentSchemaName(?string $currentSchemaName): void |
|
25
|
|
|
{ |
|
26
|
107 |
|
$this->currentSchemaName = $currentSchemaName; |
|
27
|
107 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param mixed $solution |
|
31
|
|
|
*/ |
|
32
|
114 |
|
protected function onLoadSolution($solution): void |
|
33
|
|
|
{ |
|
34
|
114 |
|
if (isset($this->dispatcher)) { |
|
35
|
|
|
// @phpstan-ignore-next-line (only for Symfony 4.4) |
|
36
|
112 |
|
$this->dispatcher->dispatch(new TypeLoadedEvent($solution, $this->currentSchemaName), Events::TYPE_LOADED); |
|
37
|
|
|
} |
|
38
|
114 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $alias |
|
42
|
|
|
* |
|
43
|
|
|
* @return Type |
|
44
|
|
|
*/ |
|
45
|
114 |
|
public function resolve($alias): ?Type |
|
46
|
|
|
{ |
|
47
|
114 |
|
if (null === $alias) { |
|
|
|
|
|
|
48
|
106 |
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
114 |
|
if (!isset($this->cache[$alias])) { |
|
52
|
114 |
|
$type = $this->baseType($alias); |
|
53
|
111 |
|
$this->cache[$alias] = $type; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
111 |
|
return $this->cache[$alias]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
114 |
|
private function baseType(string $alias): Type |
|
60
|
|
|
{ |
|
61
|
114 |
|
$type = $this->getSolution($alias); |
|
62
|
113 |
|
if (null === $type) { |
|
63
|
5 |
|
throw new UnresolvableException( |
|
64
|
5 |
|
sprintf('Could not find type with alias "%s". Did you forget to define it?', $alias) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
111 |
|
return $type; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
115 |
|
protected function supportedSolutionClass(): ?string |
|
72
|
|
|
{ |
|
73
|
115 |
|
return Type::class; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|