GraphQLTypeMiddleware::process()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 15
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 31
ccs 16
cts 16
cp 1
crap 9
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\TypeResolver\Middleware;
6
7
use Andi\GraphQL\Common\LazyInputObjectFields;
8
use Andi\GraphQL\Common\LazyObjectFields;
9
use Andi\GraphQL\Common\LazyTypeIterator;
10
use Andi\GraphQL\Common\LazyTypeResolver;
11
use Andi\GraphQL\Definition\Type\EnumTypeInterface;
12
use Andi\GraphQL\Definition\Type\InputObjectTypeInterface;
13
use Andi\GraphQL\Definition\Type\InterfacesAwareInterface;
14
use Andi\GraphQL\Definition\Type\InterfaceTypeInterface;
15
use Andi\GraphQL\Definition\Type\IsTypeOfAwareInterface;
16
use Andi\GraphQL\Definition\Type\ObjectTypeInterface;
17
use Andi\GraphQL\Definition\Type\ParseValueAwareInterface;
18
use Andi\GraphQL\Definition\Type\ResolveFieldAwareInterface;
19
use Andi\GraphQL\Definition\Type\ResolveTypeAwareInterface;
20
use Andi\GraphQL\Definition\Type\ScalarTypeInterface;
21
use Andi\GraphQL\Definition\Type\TypeInterface;
22
use Andi\GraphQL\Definition\Type\UnionTypeInterface;
23
use Andi\GraphQL\InputObjectFieldResolver\InputObjectFieldResolverInterface;
24
use Andi\GraphQL\ObjectFieldResolver\ObjectFieldResolverInterface;
25
use Andi\GraphQL\Type\DynamicObjectTypeInterface;
26
use Andi\GraphQL\TypeRegistryInterface;
27
use Andi\GraphQL\TypeResolver\TypeResolverInterface;
28
use Andi\GraphQL\WebonyxType\DynamicObjectType;
29
use GraphQL\Type\Definition as Webonyx;
30
use Psr\Container\ContainerInterface;
31
32
final class GraphQLTypeMiddleware implements MiddlewareInterface
33
{
34
    public const PRIORITY = 2048;
35
36 10
    public function __construct(
37
        private readonly ContainerInterface $container,
38
    ) {
39 10
    }
40
41 9
    public function process(mixed $type, TypeResolverInterface $typeResolver): Webonyx\Type
42
    {
43 9
        if (! \is_string($type) || ! \is_subclass_of($type, TypeInterface::class)) {
44 1
            return $typeResolver->resolve($type);
45
        }
46
47 8
        if (\is_subclass_of($type, ObjectTypeInterface::class)) {
48 2
            return $this->buildObjectType($this->container->get($type));
49
        }
50
51 6
        if (\is_subclass_of($type, InputObjectTypeInterface::class)) {
52 1
            return $this->buildInputObjectType($this->container->get($type));
53
        }
54
55 5
        if (\is_subclass_of($type, InterfaceTypeInterface::class)) {
56 1
            return $this->buildInterfaceType($this->container->get($type));
57
        }
58
59 4
        if (\is_subclass_of($type, UnionTypeInterface::class)) {
60 1
            return $this->buildUnionType($this->container->get($type));
61
        }
62
63 3
        if (\is_subclass_of($type, EnumTypeInterface::class)) {
64 1
            return $this->buildEnumType($this->container->get($type));
65
        }
66
67 2
        if (\is_subclass_of($type, ScalarTypeInterface::class)) {
68 1
            return $this->buildScalarType($this->container->get($type));
69
        }
70
71 1
        return $typeResolver->resolve($type);
72
    }
73
74 2
    private function buildObjectType(ObjectTypeInterface $type): Webonyx\ObjectType
75
    {
76 2
        $objectFieldResolver = $this->container->get(ObjectFieldResolverInterface::class);
77
78 2
        $config = [
79 2
            'name' => $type->getName(),
80 2
            'description' => $type->getDescription(),
81 2
            'fields' => new LazyObjectFields($type, $objectFieldResolver),
82 2
        ];
83
84 2
        if ($type instanceof InterfacesAwareInterface) {
85
            /** @psalm-suppress UndefinedMethod */
86 2
            $config['interfaces'] = new LazyTypeIterator(
87 2
                $type->getInterfaces(...),
88 2
                $this->container->get(TypeRegistryInterface::class),
89 2
            );
90
        }
91
92 2
        if ($type instanceof IsTypeOfAwareInterface) {
93
            /** @psalm-suppress UndefinedMethod */
94 2
            $config['isTypeOf'] = $type->isTypeOf(...);
95
        }
96
97 2
        if ($type instanceof ResolveFieldAwareInterface) {
98
            /** @psalm-suppress UndefinedMethod */
99 2
            $config['resolveField'] = $type->resolveField(...);
100
        }
101
102 2
        if ($type instanceof DynamicObjectTypeInterface) {
103 1
            return new DynamicObjectType($type, $config);
104
        }
105
106 1
        return new Webonyx\ObjectType($config);
107
    }
108
109 1
    private function buildInputObjectType(InputObjectTypeInterface $type): Webonyx\InputObjectType
110
    {
111 1
        $fieldResolver = $this->container->get(InputObjectFieldResolverInterface::class);
112
113 1
        $config = [
114 1
            'name' => $type->getName(),
115 1
            'description' => $type->getDescription(),
116 1
            'fields' => new LazyInputObjectFields($type, $fieldResolver),
117 1
        ];
118
119 1
        if ($type instanceof ParseValueAwareInterface) {
120
            /** @psalm-suppress UndefinedMethod */
121 1
            $config['parseValue'] = $type::parseValue(...);
122
        }
123
124 1
        return new Webonyx\InputObjectType($config);
125
    }
126
127 1
    private function buildInterfaceType(InterfaceTypeInterface $type): Webonyx\InterfaceType
128
    {
129 1
        $objectFieldResolver = $this->container->get(ObjectFieldResolverInterface::class);
130
131 1
        $config = [
132 1
            'name' => $type->getName(),
133 1
            'description' => $type->getDescription(),
134 1
            'fields' => new LazyObjectFields($type, $objectFieldResolver),
135 1
        ];
136
137 1
        if ($type instanceof ResolveTypeAwareInterface) {
138 1
            $typeRegistry = $this->container->get(TypeRegistryInterface::class);
139
            /** @psalm-suppress UndefinedMethod */
140 1
            $config['resolveType'] = new LazyTypeResolver($type::resolveType(...), $typeRegistry);
141
        }
142
143 1
        return new Webonyx\InterfaceType($config);
144
    }
145
146 1
    private function buildUnionType(UnionTypeInterface $type): Webonyx\UnionType
147
    {
148 1
        $typeRegistry = $this->container->get(TypeRegistryInterface::class);
149
150 1
        $config = [
151 1
            'name' => $type->getName(),
152 1
            'description' => $type->getDescription(),
153 1
            'types' => new LazyTypeIterator($type->getTypes(...), $typeRegistry),
154 1
        ];
155
156 1
        if ($type instanceof ResolveTypeAwareInterface) {
157
            /** @psalm-suppress UndefinedMethod */
158 1
            $config['resolveType'] = new LazyTypeResolver($type::resolveType(...), $typeRegistry);
159
        }
160
161 1
        return new Webonyx\UnionType($config);
162
    }
163
164 1
    private function buildEnumType(EnumTypeInterface $type): Webonyx\EnumType
165
    {
166 1
        return new Webonyx\EnumType([
167 1
            'name' => $type->getName(),
168 1
            'description' => $type->getDescription(),
169 1
            'values' => static function () use ($type): iterable {
170 1
                foreach ($type->getValues() as $value) {
171 1
                    yield $value->getName() => [
172 1
                        'name' => $value->getName(),
173 1
                        'description' => $value->getDescription(),
174 1
                        'value' => $value->getValue(),
175 1
                        'deprecationReason' => $value->getDeprecationReason(),
176 1
                    ];
177
                }
178 1
            },
179 1
        ]);
180
    }
181
182 1
    private function buildScalarType(ScalarTypeInterface $type): Webonyx\CustomScalarType
183
    {
184 1
        return new Webonyx\CustomScalarType([
185 1
            'name' => $type->getName(),
186 1
            'description' => $type->getDescription(),
187 1
            'serialize' => $type->serialize(...),
188 1
            'parseValue' => $type->parseValue(...),
189 1
            'parseLiteral' => $type->parseLiteral(...),
190 1
        ]);
191
    }
192
}
193