1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Andi\GraphQL\Common; |
6
|
|
|
|
7
|
|
|
use Andi\GraphQL\Exception\CantResolveGraphQLTypeException; |
8
|
|
|
use Andi\GraphQL\TypeRegistryInterface; |
9
|
|
|
use GraphQL\Type\Definition as Webonyx; |
10
|
|
|
use ReflectionNamedType; |
11
|
|
|
use ReflectionType; |
12
|
|
|
use ReflectionUnionType; |
13
|
|
|
use UnhandledMatchError; |
14
|
|
|
|
15
|
|
|
class LazyTypeByReflectionType |
16
|
|
|
{ |
17
|
18 |
|
public function __construct( |
18
|
|
|
private readonly ReflectionType $type, |
19
|
|
|
private readonly TypeRegistryInterface $typeRegistry, |
20
|
|
|
private readonly string $selfClassName, |
21
|
|
|
) { |
22
|
18 |
|
} |
23
|
|
|
|
24
|
17 |
|
public function __invoke(): Webonyx\Type |
25
|
|
|
{ |
26
|
17 |
|
if ($this->type instanceof ReflectionNamedType) { |
27
|
9 |
|
return $this->getTypeFromReflectionNamedType(); |
28
|
|
|
} |
29
|
|
|
|
30
|
8 |
|
if ($this->type instanceof ReflectionUnionType) { |
31
|
7 |
|
return $this->getTypeFromReflectionUnionType(); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
throw new CantResolveGraphQLTypeException('Can\'t resolve GraphQL type from ReflectionType'); |
35
|
|
|
} |
36
|
|
|
|
37
|
9 |
|
private function getTypeFromReflectionNamedType(): Webonyx\Type |
38
|
|
|
{ |
39
|
9 |
|
assert($this->type instanceof ReflectionNamedType); |
40
|
9 |
|
if ($this->type->isBuiltin()) { |
41
|
|
|
try { |
42
|
5 |
|
$type = match ($this->type->getName()) { |
|
|
|
|
43
|
5 |
|
'int' => $this->typeRegistry->get(Webonyx\IntType::class), |
44
|
5 |
|
'string' => $this->typeRegistry->get(Webonyx\StringType::class), |
45
|
5 |
|
'bool', 'true', 'false' => $this->typeRegistry->get(Webonyx\BooleanType::class), |
46
|
5 |
|
'float' => $this->typeRegistry->get(Webonyx\FloatType::class), |
47
|
5 |
|
}; |
48
|
1 |
|
} catch (UnhandledMatchError $exception) { |
49
|
1 |
|
$message = 'Can\'t resolve GraphQL type from ReflectionType'; |
50
|
5 |
|
throw new CantResolveGraphQLTypeException($message, 0, $exception); |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
4 |
|
$name = $this->type->getName(); |
54
|
|
|
|
55
|
|
|
/** @psalm-suppress TypeDoesNotContainType */ |
56
|
4 |
|
$type = 'self' === $name || 'static' === $name |
57
|
3 |
|
? $this->typeRegistry->get($this->selfClassName) |
58
|
1 |
|
: $this->typeRegistry->get($name); |
59
|
|
|
} |
60
|
|
|
|
61
|
8 |
|
return !$this->type->allowsNull() && $type instanceof Webonyx\NullableType |
62
|
4 |
|
? Webonyx\Type::nonNull($type) |
63
|
8 |
|
: $type; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Webonyx\Type |
68
|
|
|
*/ |
69
|
7 |
|
private function getTypeFromReflectionUnionType(): Webonyx\Type |
70
|
|
|
{ |
71
|
7 |
|
$names = []; |
72
|
7 |
|
$types = []; |
73
|
7 |
|
$allowsNull = false; |
74
|
7 |
|
assert($this->type instanceof ReflectionUnionType); |
75
|
7 |
|
foreach ($this->type->getTypes() as $type) { |
|
|
|
|
76
|
7 |
|
assert($type instanceof ReflectionNamedType); |
77
|
7 |
|
if ($type->isBuiltin()) { |
78
|
2 |
|
$allowsNull = 'null' === $type->getName() |
79
|
2 |
|
|| throw new CantResolveGraphQLTypeException('UnionType must contains only ObjectTypes'); |
80
|
|
|
|
81
|
2 |
|
continue; |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
$name = $type->getName(); |
85
|
7 |
|
if ($this->typeRegistry->has($name)) { |
86
|
6 |
|
$gqlType = $this->typeRegistry->get($name); |
87
|
|
|
|
88
|
6 |
|
if (! $gqlType instanceof Webonyx\ObjectType) { |
89
|
1 |
|
throw new CantResolveGraphQLTypeException('UnionType must contains only ObjectTypes'); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
$names[] = (string) $gqlType; |
93
|
5 |
|
$types[] = $name; |
94
|
|
|
} else { |
95
|
1 |
|
throw new CantResolveGraphQLTypeException(sprintf('Undefined ObjectType "%s" for UnionType', $name)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
sort($names); |
100
|
5 |
|
$name = implode('', $names) . 'UnionType'; |
101
|
|
|
|
102
|
5 |
|
if ($this->typeRegistry->has($name)) { |
103
|
2 |
|
$existsType = $this->typeRegistry->get($name); |
104
|
|
|
|
105
|
2 |
|
return !$this->type->allowsNull() && !$allowsNull && $existsType instanceof Webonyx\NullableType |
106
|
1 |
|
? Webonyx\Type::nonNull($existsType) |
107
|
2 |
|
: $existsType; |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
$unionType = new Webonyx\UnionType([ |
111
|
3 |
|
'name' => $name, |
112
|
3 |
|
'types' => new LazyTypeIterator(fn() => $types, $this->typeRegistry), |
113
|
3 |
|
'resolveType' => new ResolveType($this->typeRegistry), |
114
|
3 |
|
]); |
115
|
|
|
|
116
|
3 |
|
$this->typeRegistry->register($unionType); |
117
|
|
|
|
118
|
3 |
|
return $this->type->allowsNull() || $allowsNull |
119
|
1 |
|
? $unionType |
120
|
3 |
|
: Webonyx\Type::nonNull($unionType); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|