1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Andi\GraphQL\Field; |
6
|
|
|
|
7
|
|
|
use Andi\GraphQL\Common\ResolverArguments; |
8
|
|
|
use GraphQL\Type\Definition as Webonyx; |
9
|
|
|
use Spiral\Core\InvokerInterface; |
10
|
|
|
use Spiral\Core\ScopeInterface; |
11
|
|
|
|
12
|
|
|
final class OuterObjectField extends Webonyx\FieldDefinition |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param array $config |
16
|
|
|
* @param class-string $class |
|
|
|
|
17
|
|
|
* @param string $method |
18
|
|
|
* @param array<string,string> $argumentsMap |
19
|
|
|
* @param ScopeInterface $scope |
20
|
|
|
* @param InvokerInterface $invoker |
21
|
|
|
*/ |
22
|
3 |
|
public function __construct( |
23
|
|
|
array $config, |
24
|
|
|
private readonly string $class, |
25
|
|
|
private readonly string $method, |
26
|
|
|
private readonly array $argumentsMap, |
27
|
|
|
private readonly ScopeInterface $scope, |
28
|
|
|
private readonly InvokerInterface $invoker, |
29
|
|
|
) { |
30
|
3 |
|
$config['resolve'] = $this->resolve(...); |
31
|
|
|
|
32
|
3 |
|
parent::__construct($config); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $object |
37
|
|
|
* @param array<string,mixed> $args |
38
|
|
|
* @param mixed $context |
39
|
|
|
* @param Webonyx\ResolveInfo $info |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
3 |
|
private function resolve( |
44
|
|
|
mixed $object, |
45
|
|
|
array $args, |
46
|
|
|
mixed $context, |
47
|
|
|
Webonyx\ResolveInfo $info, |
48
|
|
|
): mixed { |
49
|
3 |
|
$parameters = []; |
50
|
3 |
|
foreach ($args as $name => $value) { |
51
|
3 |
|
$parameters[$this->argumentsMap[$name] ?? $name] = $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
return $this->scope->runScope( |
55
|
3 |
|
[ResolverArguments::class => new ResolverArguments($object, $args, $context, $info)], |
56
|
3 |
|
fn () => $this->invoker->invoke([$this->class, $this->method], $parameters), |
57
|
3 |
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|