|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Andi\GraphQL\Common; |
|
6
|
|
|
|
|
7
|
|
|
use Andi\GraphQL\Definition\Field\TypeAwareInterface; |
|
8
|
|
|
use Andi\GraphQL\Exception\CantResolveGraphQLTypeException; |
|
9
|
|
|
use Andi\GraphQL\TypeRegistryInterface; |
|
10
|
|
|
use GraphQL\Language\AST\ListTypeNode; |
|
11
|
|
|
use GraphQL\Language\AST\NamedTypeNode; |
|
12
|
|
|
use GraphQL\Language\AST\NameNode; |
|
13
|
|
|
use GraphQL\Language\AST\Node; |
|
14
|
|
|
use GraphQL\Language\AST\NonNullTypeNode; |
|
15
|
|
|
use GraphQL\Language\AST\TypeNode; |
|
16
|
|
|
use GraphQL\Language\Parser; |
|
17
|
|
|
use GraphQL\Type\Definition as Webonyx; |
|
18
|
|
|
|
|
19
|
|
|
final class LazyParserType |
|
20
|
|
|
{ |
|
21
|
11 |
|
public function __construct( |
|
22
|
|
|
private readonly string $type, |
|
23
|
|
|
private readonly int $mode, |
|
24
|
|
|
private readonly TypeRegistryInterface $typeRegistry, |
|
25
|
|
|
) { |
|
26
|
11 |
|
} |
|
27
|
|
|
|
|
28
|
10 |
|
public function __invoke(): Webonyx\Type |
|
29
|
|
|
{ |
|
30
|
10 |
|
if (! $this->typeRegistry->has($this->type)) { |
|
31
|
3 |
|
return $this->getType(Parser::parseType($this->type)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
7 |
|
$type = $this->typeRegistry->get($this->type); |
|
35
|
|
|
|
|
36
|
7 |
|
if (TypeAwareInterface::ITEM_IS_REQUIRED === (TypeAwareInterface::ITEM_IS_REQUIRED & $this->mode)) { |
|
37
|
2 |
|
\assert($type instanceof Webonyx\NullableType); |
|
38
|
2 |
|
$type = Webonyx\Type::nonNull($type); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
7 |
|
if (TypeAwareInterface::IS_LIST === (TypeAwareInterface::IS_LIST & $this->mode)) { |
|
42
|
3 |
|
$type = Webonyx\Type::listOf($type); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
7 |
|
if (TypeAwareInterface::IS_REQUIRED === (TypeAwareInterface::IS_REQUIRED & $this->mode)) { |
|
46
|
3 |
|
$type = Webonyx\Type::nonNull($type); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
7 |
|
return $type; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
private function getType(Node&TypeNode $node): Webonyx\Type |
|
53
|
|
|
{ |
|
54
|
3 |
|
if ($node instanceof NameNode) { |
|
55
|
3 |
|
return $this->typeRegistry->get($node->value); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
if ($node instanceof NamedTypeNode) { |
|
59
|
3 |
|
return $this->getType($node->name); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
if ($node instanceof NonNullTypeNode) { |
|
63
|
1 |
|
$type = $this->getType($node->type); |
|
64
|
1 |
|
return $type instanceof Webonyx\NullableType |
|
65
|
1 |
|
? Webonyx\Type::nonNull($type) |
|
66
|
1 |
|
: $type; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
if ($node instanceof ListTypeNode) { |
|
70
|
1 |
|
return Webonyx\Type::listOf($this->getType($node->type)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
throw new CantResolveGraphQLTypeException(\sprintf('Can\'t resolve GraphQL type for "%s"', $this->type)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|