1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Schema\Resolver; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Execution\ResolveInfo; |
6
|
|
|
|
7
|
|
|
class ResolverRegistry implements ResolverRegistryInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var ResolverInterface[] |
11
|
|
|
*/ |
12
|
|
|
protected $resolverMap; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var ResolverMiddlewareInterface[]|null |
16
|
|
|
*/ |
17
|
|
|
protected $middleware; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ResolverRegistry constructor. |
21
|
|
|
* @param ResolverInterface[] $resolvers |
22
|
|
|
* @param ResolverMiddlewareInterface[]|null $middleware |
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $resolvers = [], ?array $middleware = null) |
25
|
|
|
{ |
26
|
|
|
$this->middleware = $middleware; |
27
|
|
|
|
28
|
|
|
$this->registerResolvers($resolvers); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function register(string $typeName, ResolverInterface $resolver): void |
35
|
|
|
{ |
36
|
|
|
$this->resolverMap[$typeName] = $resolver; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function getFieldResolver(string $typeName, string $fieldName): ?callable |
43
|
|
|
{ |
44
|
|
|
$resolver = $this->getResolver($typeName); |
45
|
|
|
|
46
|
|
|
$resolver = $resolver instanceof ResolverCollection |
47
|
|
|
? $resolver->getResolveCallback()($fieldName) |
48
|
|
|
: $resolver; |
49
|
|
|
|
50
|
|
|
$resolveCallback = $resolver instanceof ResolverInterface |
51
|
|
|
? $resolver->getResolveCallback() |
52
|
|
|
: $resolver; |
53
|
|
|
|
54
|
|
|
if (null === $resolveCallback) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (null !== $this->middleware) { |
59
|
|
|
return $this->applyMiddleware($resolveCallback, \array_reverse($this->middleware)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $resolveCallback; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function getTypeResolver(string $typeName): ?callable |
69
|
|
|
{ |
70
|
|
|
$resolver = $this->getResolver($typeName); |
71
|
|
|
|
72
|
|
|
if ($resolver instanceof ResolverInterface) { |
73
|
|
|
return $resolver->getTypeResolver(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
|
|
public function getResolver(string $typeName): ?ResolverInterface |
83
|
|
|
{ |
84
|
|
|
return $this->resolverMap[$typeName] ?? null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $resolvers |
89
|
|
|
*/ |
90
|
|
|
protected function registerResolvers(array $resolvers): void |
91
|
|
|
{ |
92
|
|
|
foreach ($resolvers as $typeName => $resolver) { |
93
|
|
|
if (\is_array($resolver)) { |
94
|
|
|
$resolver = new ResolverCollection($resolver); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->register($typeName, $resolver); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param callable $resolveCallback |
103
|
|
|
* @param array $middleware |
104
|
|
|
* @return callable |
105
|
|
|
*/ |
106
|
|
|
protected function applyMiddleware(callable $resolveCallback, array $middleware): callable |
107
|
|
|
{ |
108
|
|
|
return \array_reduce( |
109
|
|
|
$middleware, |
110
|
|
|
function (callable $resolveCallback, ResolverMiddlewareInterface $middleware) { |
111
|
|
|
return function ($rootValue, array $arguments, $context = null, ?ResolveInfo $info = null) use ( |
112
|
|
|
$resolveCallback, |
113
|
|
|
$middleware |
114
|
|
|
) { |
115
|
|
|
return $middleware->resolve($resolveCallback, $rootValue, $arguments, $context, $info); |
116
|
|
|
}; |
117
|
|
|
}, |
118
|
|
|
$resolveCallback |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|