Driver::pagination()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\ORM\GraphQL;
6
7
use Closure;
8
use GraphQL\Error\Error;
9
use GraphQL\Type\Definition\InputObjectType;
10
use GraphQL\Type\Definition\ObjectType;
11
12
class Driver extends Container
13
{
14
    use Services;
15
16
    /**
17
     * Return a connection wrapper for a type.  This is a special type that
18
     * wraps the entity type
19
     *
20
     * @throws Error
21
     */
22
    public function connection(string $id, string|null $eventName = null): ObjectType
23
    {
24
        $objectType = $this->type($id, $eventName);
25
26
        return $this->get(Type\TypeContainer::class)
27
            ->build(Type\Connection::class, $objectType->name, $objectType);
28
    }
29
30
    /**
31
     * A shortcut into the EntityTypeContainer and TypeContainer
32
     *
33
     * @throws Error
34
     */
35
    public function type(string $id, string|null $eventName = null): mixed
36
    {
37
        $entityTypeContainer = $this->get(Type\Entity\EntityTypeContainer::class);
38
        if ($entityTypeContainer->has($id)) {
39
            return $entityTypeContainer->get($id, $eventName)->getObjectType();
40
        }
41
42
        $typeContainer = $this->get(Type\TypeContainer::class);
43
        if ($typeContainer->has($id)) {
44
            return $typeContainer->get($id);
45
        }
46
47
        throw new Error('Type "' . $id . '" is not registered');
48
    }
49
50
    /**
51
     * Return an InputObject type of filters for a connection
52
     * Requires the internal representation of the entity
53
     *
54
     * @throws Error
55
     */
56
    public function filter(string $id): object
57
    {
58
        return $this->get(Filter\FilterFactory::class)->get(
59
            $this->get(Type\Entity\EntityTypeContainer::class)->get($id),
60
        );
61
    }
62
63
    /**
64
     * Pagination for a connection
65
     *
66
     * @throws Error
67
     */
68
    public function pagination(): object
69
    {
70
        return $this->type('pagination');
71
    }
72
73
    /**
74
     * Resolve a connection
75
     *
76
     * @throws Error
77
     */
78
    public function resolve(string $id, string|null $eventName = null): Closure
79
    {
80
        return $this->get(Resolve\ResolveEntityFactory::class)->get(
81
            $this->get(Type\Entity\EntityTypeContainer::class)->get($id),
82
            $eventName,
83
        );
84
    }
85
86
    /**
87
     * @param string[] $requiredFields An optional list of just the required fields you want for the mutation.
88
     * @param string[] $optionalFields An optional list of optional fields you want for the mutation.
89
     */
90
    public function input(string $entityClass, array $requiredFields = [], array $optionalFields = []): InputObjectType
91
    {
92
        return $this->get(Input\InputFactory::class)->get($entityClass, $requiredFields, $optionalFields);
93
    }
94
95
    /**
96
     * Return an array defining a GraphQL endpoint.
97
     *
98
     * @return mixed[]
99
     */
100
    public function completeConnection(
101
        string $id,
102
        string|null $entityDefinitionEventName = null,
103
        string|null $resolveEventName = null,
104
    ): array {
105
        return [
106
            'type' => $this->connection($id, $entityDefinitionEventName),
107
            'args' => [
108
                'filter' => $this->filter($id),
109
                'pagination' => $this->pagination(),
110
            ],
111
            'resolve' => $this->resolve($id, $resolveEventName),
112
        ];
113
    }
114
}
115