-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathTypeRegistry.php
More file actions
100 lines (86 loc) · 3.67 KB
/
TypeRegistry.php
File metadata and controls
100 lines (86 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use TheCodingMachine\GraphQLite\Types\MutableInputInterface;
use TheCodingMachine\GraphQLite\Types\MutableInterface;
use TheCodingMachine\GraphQLite\Types\MutableInterfaceType;
use TheCodingMachine\GraphQLite\Types\MutableObjectType;
use TheCodingMachine\GraphQLite\Types\ResolvableMutableInputInterface;
use TheCodingMachine\GraphQLite\Types\UnionType;
/**
* A cache used to store already FULLY COMPUTED types.
*/
class TypeRegistry
{
/** @var array<string,NamedType&Type&(MutableObjectType|InterfaceType|UnionType|(InputObjectType&ResolvableMutableInputInterface))> */
private array $types = [];
/**
* Registers a type.
* IMPORTANT: the type MUST be fully computed (so ExtendType annotations must have ALREADY been applied to the tag)
* ONLY THE RecursiveTypeMapper IS ALLOWED TO CALL THIS METHOD.
*
* @param NamedType&Type&(MutableObjectType|InterfaceType|UnionType|(InputObjectType&ResolvableMutableInputInterface)) $type
*/
public function registerType(NamedType $type): void
{
if (isset($this->types[$type->name])) {
throw new GraphQLRuntimeException('Type "' . $type->name . '" is already registered');
}
$this->types[$type->name] = $type;
}
/**
* A failsafe variant of registerType:
* - Registers the type passed in parameter.
* - If the type is already present, does not fail. Instead, return the old type already available.
*
* @param NamedType&Type&(MutableObjectType|InterfaceType|UnionType|(InputObjectType&ResolvableMutableInputInterface)) $type
*
* @return NamedType&Type&(MutableObjectType|InterfaceType|UnionType|(InputObjectType&ResolvableMutableInputInterface))
*/
public function getOrRegisterType(NamedType $type): NamedType
{
if (isset($this->types[$type->name])) {
return $this->types[$type->name];
}
$this->types[$type->name] = $type;
return $type;
}
public function hasType(string $typeName): bool
{
return isset($this->types[$typeName]);
}
/** @return NamedType&Type&(ObjectType|InterfaceType|UnionType|(InputObjectType&ResolvableMutableInputInterface)) */
public function getType(string $typeName): NamedType
{
if (! isset($this->types[$typeName])) {
throw new GraphQLRuntimeException('Could not find type "' . $typeName . '" in registry');
}
return $this->types[$typeName];
}
public function getMutableObjectType(string $typeName): MutableObjectType
{
$type = $this->getType($typeName);
if (! $type instanceof MutableObjectType) {
throw new GraphQLRuntimeException('Expected GraphQL type "' . $typeName . '" to be an MutableObjectType. Got a ' . $type::class);
}
return $type;
}
/** @return MutableInterface&(MutableObjectType|MutableInterfaceType) */
public function getMutableInterface(string $typeName): MutableInterface
{
$type = $this->getType($typeName);
if (
! $type instanceof MutableInterface
&& ! $type instanceof MutableInputInterface
|| (! $type instanceof MutableInterfaceType && ! $type instanceof MutableObjectType)
) {
throw new GraphQLRuntimeException('Expected GraphQL type "' . $typeName . '" to be either a MutableObjectType or a MutableInterfaceType. Got a ' . $type::class);
}
return $type;
}
}