1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Andi\GraphQL\Spiral\Common; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition as Webonyx; |
8
|
|
|
use GraphQL\Type\Schema; |
9
|
|
|
|
10
|
|
|
final class SchemaWarmupper |
11
|
|
|
{ |
12
|
|
|
public static function warmup(Schema $schema): void |
13
|
|
|
{ |
14
|
|
|
$allTypes = []; |
15
|
|
|
|
16
|
|
|
$types = $schema->getConfig()->getTypes(); |
17
|
|
|
if (is_callable($types)) { |
18
|
|
|
$types = $types(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
foreach ($types as $type) { |
22
|
|
|
self::warmupType(Schema::resolveType($type), $allTypes); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
foreach ($schema->getDirectives() as $directive) { |
26
|
|
|
foreach ($directive->args as $arg) { |
27
|
|
|
self::warmupType($arg->getType(), $allTypes); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if ($type = $schema->getQueryType()) { |
32
|
|
|
self::warmupType($type, $allTypes); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($type = $schema->getMutationType()) { |
36
|
|
|
self::warmupType($type, $allTypes); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($type = $schema->getSubscriptionType()) { |
40
|
|
|
self::warmupType($type, $allTypes); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$schema->getTypeMap(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private static function warmupType(Webonyx\Type $type, array &$allTypes): void |
47
|
|
|
{ |
48
|
|
|
if ($type instanceof Webonyx\WrappingType) { |
49
|
|
|
self::warmupType($type->getInnermostType(), $allTypes); |
50
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
assert($type instanceof Webonyx\NamedType); |
54
|
|
|
/** |
55
|
|
|
* @psalm-suppress NoInterfaceProperties |
56
|
|
|
* @psalm-suppress UndefinedPropertyFetch |
57
|
|
|
*/ |
58
|
|
|
$name = $type->name; |
59
|
|
|
if (isset($allTypes[$name])) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$allTypes[$name] = true; |
64
|
|
|
|
65
|
|
|
if ($type instanceof Webonyx\EnumType) { |
66
|
|
|
$enumValues = $type->getValues(); |
67
|
|
|
if (is_callable($type->config['values'])) { |
68
|
|
|
$values = []; |
69
|
|
|
foreach ($enumValues as $value) { |
70
|
|
|
$values[$value->name] = [ |
71
|
|
|
'value' => $value->value, |
72
|
|
|
'description' => $value->description, |
73
|
|
|
'deprecationReason' => $value->deprecationReason, |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
$type->config['values'] = $values; |
77
|
|
|
} |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($type instanceof Webonyx\UnionType) { |
82
|
|
|
$type->config['types'] = $type->getTypes(); |
83
|
|
|
foreach ($type->config['types'] as $member) { |
84
|
|
|
self::warmupType($member, $allTypes); |
85
|
|
|
} |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($type instanceof Webonyx\InputObjectType) { |
90
|
|
|
$type->config['fields'] = $type->getFields(); |
91
|
|
|
foreach ($type->config['fields'] as $field) { |
92
|
|
|
self::warmupType($field->getType(), $allTypes); |
93
|
|
|
} |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($type instanceof Webonyx\ImplementingType) { |
98
|
|
|
$interfaces = $type->getInterfaces(); |
99
|
|
|
foreach ($interfaces as $interface) { |
100
|
|
|
self::warmupType($interface, $allTypes); |
101
|
|
|
} |
102
|
|
|
assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType); |
103
|
|
|
$type->config['interfaces'] = $interfaces; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($type instanceof Webonyx\HasFieldsType) { |
107
|
|
|
$fields = $type->getFields(); |
108
|
|
|
foreach ($fields as $field) { |
109
|
|
|
foreach ($field->args as $arg) { |
110
|
|
|
self::warmupType($arg->config['type'] = $arg->getType(), $allTypes); |
111
|
|
|
} |
112
|
|
|
self::warmupType($field->getType(), $allTypes); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType); |
116
|
|
|
$type->config['fields'] = $fields; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|