Test Failed
Pull Request — master (#11)
by Andrey
14:35
created

SchemaWarmupper   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
dl 0
loc 104
rs 10
c 1
b 0
f 0
wmc 25

2 Methods

Rating   Name   Duplication   Size   Complexity  
B warmup() 0 32 8
C warmupType() 0 68 17
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
        $name = $type->name;
56
        if (isset($allTypes[$name])) {
57
            return;
58
        }
59
60
        $allTypes[$name] = true;
61
62
        if ($type instanceof Webonyx\EnumType) {
63
            $enumValues = $type->getValues();
64
            if (is_callable($type->config['values'])) {
65
                $values = [];
66
                foreach ($enumValues as $value) {
67
                    $values[$value->name] = [
68
                        'value' => $value->value,
69
                        'description' => $value->description,
70
                        'deprecationReason' => $value->deprecationReason,
71
                    ];
72
                }
73
                $type->config['values'] = $values;
74
            }
75
            return;
76
        }
77
78
        if ($type instanceof Webonyx\UnionType) {
79
            $type->config['types'] = $type->getTypes();
80
            foreach ($type->config['types'] as $member) {
81
                self::warmupType($member, $allTypes);
82
            }
83
            return;
84
        }
85
86
        if ($type instanceof Webonyx\InputObjectType) {
87
            $type->config['fields'] = $type->getFields();
88
            foreach ($type->config['fields'] as $field) {
89
                self::warmupType($field->getType(), $allTypes);
90
            }
91
            return;
92
        }
93
94
        if ($type instanceof Webonyx\ImplementingType) {
95
            $interfaces = $type->getInterfaces();
96
            foreach ($interfaces as $interface) {
97
                self::warmupType($interface, $allTypes);
98
            }
99
            assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType);
100
            $type->config['interfaces'] = $interfaces;
101
        }
102
103
        if ($type instanceof Webonyx\HasFieldsType) {
104
            $fields = $type->getFields();
105
            foreach ($fields as $field) {
106
                foreach ($field->args as $arg) {
107
                    self::warmupType($arg->config['type'] = $arg->getType(), $allTypes);
108
                }
109
                self::warmupType($field->getType(), $allTypes);
110
            }
111
112
            assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType);
113
            $type->config['fields'] = $fields;
114
        }
115
    }
116
}
117