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 ObjectField extends Webonyx\FieldDefinition |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param array $config |
16
|
|
|
* @param string $method |
17
|
|
|
* @param array<string,string> $argumentsMap |
18
|
|
|
* @param ScopeInterface $scope |
19
|
|
|
* @param InvokerInterface $invoker |
20
|
|
|
*/ |
21
|
3 |
|
public function __construct( |
22
|
|
|
array $config, |
23
|
|
|
private readonly string $method, |
24
|
|
|
private readonly array $argumentsMap, |
25
|
|
|
private readonly ScopeInterface $scope, |
26
|
|
|
private readonly InvokerInterface $invoker, |
27
|
|
|
) { |
28
|
3 |
|
$config['resolve'] = $this->resolve(...); |
29
|
|
|
|
30
|
3 |
|
parent::__construct($config); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $object |
35
|
|
|
* @param array<string,mixed> $args |
36
|
|
|
* @param mixed $context |
37
|
|
|
* @param Webonyx\ResolveInfo $info |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
3 |
|
private function resolve( |
42
|
|
|
mixed $object, |
43
|
|
|
array $args, |
44
|
|
|
mixed $context, |
45
|
|
|
Webonyx\ResolveInfo $info, |
46
|
|
|
): mixed { |
47
|
3 |
|
$parameters = []; |
48
|
3 |
|
foreach ($args as $name => $value) { |
49
|
3 |
|
$parameters[$this->argumentsMap[$name] ?? $name] = $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
return $this->scope->runScope( |
53
|
3 |
|
[ResolverArguments::class => new ResolverArguments($object, $args, $context, $info)], |
54
|
3 |
|
fn () => $this->invoker->invoke([$object, $this->method], $parameters), |
55
|
3 |
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|