|
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): ObjectType |
|
23
|
|
|
{ |
|
24
|
|
|
$objectType = $this->type($id); |
|
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): mixed |
|
36
|
|
|
{ |
|
37
|
|
|
$entityTypeContainer = $this->get(Type\Entity\EntityTypeContainer::class); |
|
38
|
|
|
if ($entityTypeContainer->has($id)) { |
|
39
|
|
|
return $entityTypeContainer->get($id)->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(string $id, string|null $eventName = null): array |
|
101
|
|
|
{ |
|
102
|
|
|
return [ |
|
103
|
|
|
'type' => $this->connection($id), |
|
104
|
|
|
'args' => [ |
|
105
|
|
|
'filter' => $this->filter($id), |
|
106
|
|
|
'pagination' => $this->pagination(), |
|
107
|
|
|
], |
|
108
|
|
|
'resolve' => $this->resolve($id, $eventName), |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|