1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Andi\GraphQL\ObjectFieldResolver\Middleware; |
||
6 | |||
7 | use Andi\GraphQL\ArgumentResolver\ArgumentResolverInterface; |
||
8 | use Andi\GraphQL\Common\LazyType; |
||
9 | use Andi\GraphQL\Definition\Field\ArgumentsAwareInterface; |
||
10 | use Andi\GraphQL\Definition\Field\ComplexityAwareInterface; |
||
11 | use Andi\GraphQL\Definition\Field\ObjectFieldInterface; |
||
12 | use Andi\GraphQL\Definition\Field\ResolveAwareInterface; |
||
13 | use Andi\GraphQL\ObjectFieldResolver\ObjectFieldResolverInterface; |
||
14 | use Andi\GraphQL\TypeRegistryInterface; |
||
15 | use GraphQL\Type\Definition as Webonyx; |
||
16 | |||
17 | /** |
||
18 | * @see Webonyx\FieldDefinition |
||
19 | * |
||
20 | * @phpstan-import-type FieldDefinitionConfig from Webonyx\FieldDefinition |
||
21 | */ |
||
22 | final class ObjectFieldMiddleware implements MiddlewareInterface |
||
23 | { |
||
24 | public const PRIORITY = 2048; |
||
25 | |||
26 | 4 | public function __construct( |
|
27 | private readonly TypeRegistryInterface $typeRegistry, |
||
28 | private readonly ArgumentResolverInterface $argumentResolver, |
||
29 | ) { |
||
30 | 4 | } |
|
31 | |||
32 | 3 | public function process(mixed $field, ObjectFieldResolverInterface $fieldResolver): Webonyx\FieldDefinition |
|
33 | { |
||
34 | 3 | if (! $field instanceof ObjectFieldInterface) { |
|
35 | 1 | return $fieldResolver->resolve($field); |
|
36 | } |
||
37 | |||
38 | /** @var FieldDefinitionConfig $config */ |
||
39 | 2 | $config = [ |
|
40 | 2 | 'name' => $field->getName(), |
|
41 | 2 | 'description' => $field->getDescription(), |
|
42 | 2 | 'deprecationReason' => $field->getDeprecationReason(), |
|
43 | 2 | 'type' => new LazyType($field, $this->typeRegistry), |
|
44 | 2 | ]; |
|
45 | |||
46 | 2 | if ($field instanceof ArgumentsAwareInterface) { |
|
47 | 1 | $config['args'] = $this->extractArguments($field->getArguments()); |
|
48 | } |
||
49 | |||
50 | 2 | if ($field instanceof ResolveAwareInterface) { |
|
51 | /** @psalm-suppress UndefinedMethod */ |
||
52 | 1 | $config['resolve'] = $field->resolve(...); |
|
53 | } |
||
54 | |||
55 | 2 | if ($field instanceof ComplexityAwareInterface) { |
|
56 | /** @psalm-suppress UndefinedMethod */ |
||
57 | 1 | $config['complexity'] = $field->complexity(...); |
|
58 | } |
||
59 | |||
60 | 2 | return new Webonyx\FieldDefinition($config); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
61 | } |
||
62 | |||
63 | 1 | private function extractArguments(iterable $arguments): iterable |
|
64 | { |
||
65 | 1 | foreach ($arguments as $argument) { |
|
66 | 1 | yield $this->argumentResolver->resolve($argument); |
|
67 | } |
||
68 | } |
||
69 | } |
||
70 |