1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
9
|
|
|
use GraphQL\Doctrine\Definition\EntityIDType; |
10
|
|
|
use GraphQL\Doctrine\Definition\JoinTypeType; |
11
|
|
|
use GraphQL\Doctrine\Definition\LogicalOperatorType; |
12
|
|
|
use GraphQL\Doctrine\Definition\Operator\AbstractOperator; |
13
|
|
|
use GraphQL\Doctrine\Definition\SortingOrderType; |
14
|
|
|
use GraphQL\Doctrine\Factory\FilteredQueryBuilderFactory; |
15
|
|
|
use GraphQL\Doctrine\Factory\Type\AbstractTypeFactory; |
16
|
|
|
use GraphQL\Doctrine\Factory\Type\EntityIDTypeFactory; |
17
|
|
|
use GraphQL\Doctrine\Factory\Type\FilterGroupConditionTypeFactory; |
18
|
|
|
use GraphQL\Doctrine\Factory\Type\FilterGroupJoinTypeFactory; |
19
|
|
|
use GraphQL\Doctrine\Factory\Type\FilterTypeFactory; |
20
|
|
|
use GraphQL\Doctrine\Factory\Type\InputTypeFactory; |
21
|
|
|
use GraphQL\Doctrine\Factory\Type\JoinOnTypeFactory; |
22
|
|
|
use GraphQL\Doctrine\Factory\Type\ObjectTypeFactory; |
23
|
|
|
use GraphQL\Doctrine\Factory\Type\PartialInputTypeFactory; |
24
|
|
|
use GraphQL\Doctrine\Factory\Type\SortingTypeFactory; |
25
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
26
|
|
|
use GraphQL\Type\Definition\LeafType; |
27
|
|
|
use GraphQL\Type\Definition\ListOfType; |
28
|
|
|
use GraphQL\Type\Definition\NamedType; |
29
|
|
|
use GraphQL\Type\Definition\ObjectType; |
30
|
|
|
use GraphQL\Type\Definition\Type; |
31
|
|
|
use Psr\Container\ContainerInterface; |
32
|
|
|
use UnexpectedValueException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Registry of types to manage all GraphQL types. |
36
|
|
|
* |
37
|
|
|
* This is the entry point for the library. |
38
|
|
|
*/ |
39
|
|
|
final class Types implements TypesInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var array<string, NamedType&Type> mapping of type name to type instances |
43
|
|
|
*/ |
44
|
|
|
private array $types = []; |
45
|
|
|
|
46
|
|
|
private readonly ObjectTypeFactory $objectTypeFactory; |
47
|
|
|
|
48
|
|
|
private readonly InputTypeFactory $inputTypeFactory; |
49
|
|
|
|
50
|
|
|
private readonly PartialInputTypeFactory $partialInputTypeFactory; |
51
|
|
|
|
52
|
|
|
private readonly FilterTypeFactory $filterTypeFactory; |
53
|
|
|
|
54
|
|
|
private readonly FilteredQueryBuilderFactory $filteredQueryBuilderFactory; |
55
|
|
|
|
56
|
|
|
private readonly SortingTypeFactory $sortingTypeFactory; |
57
|
|
|
|
58
|
|
|
private readonly EntityIDTypeFactory $entityIDTypeFactory; |
59
|
|
|
|
60
|
|
|
private readonly JoinOnTypeFactory $joinOnTypeFactory; |
61
|
|
|
|
62
|
|
|
private readonly FilterGroupJoinTypeFactory $filterGroupJoinTypeFactory; |
63
|
|
|
|
64
|
|
|
private readonly FilterGroupConditionTypeFactory $filterGroupConditionTypeFactory; |
65
|
|
|
|
66
|
121 |
|
public function __construct(private readonly EntityManager $entityManager, private readonly ?ContainerInterface $customTypes = null) |
67
|
|
|
{ |
68
|
121 |
|
$this->objectTypeFactory = new ObjectTypeFactory($this, $entityManager); |
|
|
|
|
69
|
121 |
|
$this->inputTypeFactory = new InputTypeFactory($this, $entityManager); |
|
|
|
|
70
|
121 |
|
$this->partialInputTypeFactory = new PartialInputTypeFactory($this, $entityManager); |
|
|
|
|
71
|
121 |
|
$this->sortingTypeFactory = new SortingTypeFactory($this, $entityManager); |
|
|
|
|
72
|
121 |
|
$this->entityIDTypeFactory = new EntityIDTypeFactory($this, $entityManager); |
|
|
|
|
73
|
121 |
|
$this->filterGroupJoinTypeFactory = new FilterGroupJoinTypeFactory($this, $entityManager); |
|
|
|
|
74
|
121 |
|
$this->filterGroupConditionTypeFactory = new FilterGroupConditionTypeFactory($this, $entityManager); |
|
|
|
|
75
|
121 |
|
$this->filteredQueryBuilderFactory = new FilteredQueryBuilderFactory($this, $entityManager, $this->sortingTypeFactory); |
|
|
|
|
76
|
121 |
|
$this->filterTypeFactory = new FilterTypeFactory($this, $entityManager, $this->filterGroupJoinTypeFactory, $this->filterGroupConditionTypeFactory); |
|
|
|
|
77
|
121 |
|
$this->joinOnTypeFactory = new JoinOnTypeFactory($this, $entityManager, $this->filterGroupJoinTypeFactory, $this->filterGroupConditionTypeFactory); |
|
|
|
|
78
|
|
|
|
79
|
121 |
|
$this->initializeInternalTypes(); |
80
|
|
|
} |
81
|
|
|
|
82
|
44 |
|
public function has(string $key): bool |
83
|
|
|
{ |
84
|
44 |
|
return $this->customTypes && $this->customTypes->has($key) || array_key_exists($key, $this->types); |
85
|
|
|
} |
86
|
|
|
|
87
|
54 |
|
public function get(string $key): Type&NamedType |
88
|
|
|
{ |
89
|
54 |
|
if ($this->customTypes && $this->customTypes->has($key)) { |
90
|
|
|
/** @var NamedType&Type $t */ |
91
|
36 |
|
$t = $this->customTypes->get($key); |
92
|
36 |
|
$this->registerInstance($t); |
93
|
|
|
|
94
|
36 |
|
return $t; |
95
|
|
|
} |
96
|
|
|
|
97
|
43 |
|
if (array_key_exists($key, $this->types)) { |
98
|
42 |
|
return $this->types[$key]; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
throw new Exception('No type registered with key `' . $key . '`. Either correct the usage, or register it in your custom types container when instantiating `' . self::class . '`.'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get a type from internal registry, and create it via the factory if needed. |
106
|
|
|
* |
107
|
|
|
* @param class-string $className |
108
|
|
|
*/ |
109
|
65 |
|
private function getViaFactory(string $className, string $typeName, AbstractTypeFactory $factory): Type&NamedType |
110
|
|
|
{ |
111
|
65 |
|
$this->throwIfNotEntity($className); |
112
|
|
|
|
113
|
64 |
|
if (!isset($this->types[$typeName])) { |
114
|
64 |
|
$instance = $factory->create($className, $typeName); |
115
|
64 |
|
$this->registerInstance($instance); |
116
|
|
|
} |
117
|
|
|
|
118
|
64 |
|
return $this->types[$typeName]; |
119
|
|
|
} |
120
|
|
|
|
121
|
19 |
|
public function getOutput(string $className): ObjectType |
122
|
|
|
{ |
123
|
|
|
/** @var ObjectType $type */ |
124
|
19 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className), $this->objectTypeFactory); |
125
|
|
|
|
126
|
18 |
|
return $type; |
127
|
|
|
} |
128
|
|
|
|
129
|
10 |
|
public function getInput(string $className): InputObjectType |
130
|
|
|
{ |
131
|
|
|
/** @var InputObjectType $type */ |
132
|
10 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className) . 'Input', $this->inputTypeFactory); |
133
|
|
|
|
134
|
10 |
|
return $type; |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
public function getPartialInput(string $className): InputObjectType |
138
|
|
|
{ |
139
|
|
|
/** @var InputObjectType $type */ |
140
|
2 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className) . 'PartialInput', $this->partialInputTypeFactory); |
141
|
|
|
|
142
|
2 |
|
return $type; |
143
|
|
|
} |
144
|
|
|
|
145
|
28 |
|
public function getFilter(string $className): InputObjectType |
146
|
|
|
{ |
147
|
|
|
/** @var InputObjectType $type */ |
148
|
28 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className) . 'Filter', $this->filterTypeFactory); |
149
|
|
|
|
150
|
28 |
|
return $type; |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
public function getSorting(string $className): ListOfType |
154
|
|
|
{ |
155
|
|
|
/** @var InputObjectType $type */ |
156
|
4 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className) . 'Sorting', $this->sortingTypeFactory); |
157
|
|
|
|
158
|
4 |
|
return Type::listOf(Type::nonNull($type)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns a joinOn input type for the given entity. |
163
|
|
|
* |
164
|
|
|
* This is for internal use only. |
165
|
|
|
* |
166
|
|
|
* @param class-string $className the class name of an entity (`Post::class`) |
167
|
|
|
*/ |
168
|
2 |
|
public function getJoinOn(string $className): InputObjectType |
169
|
|
|
{ |
170
|
|
|
/** @var InputObjectType $type */ |
171
|
2 |
|
$type = $this->getViaFactory($className, 'JoinOn' . Utils::getTypeName($className), $this->joinOnTypeFactory); |
172
|
|
|
|
173
|
2 |
|
return $type; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns a joins input type for the given entity. |
178
|
|
|
* |
179
|
|
|
* This is for internal use only. |
180
|
|
|
* |
181
|
|
|
* @param class-string $className the class name of an entity (`Post::class`) |
182
|
|
|
*/ |
183
|
24 |
|
public function getFilterGroupJoin(string $className): InputObjectType |
184
|
|
|
{ |
185
|
|
|
/** @var InputObjectType $type */ |
186
|
24 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className) . 'FilterGroupJoin', $this->filterGroupJoinTypeFactory); |
187
|
|
|
|
188
|
24 |
|
return $type; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns a condition input type for the given entity. |
193
|
|
|
* |
194
|
|
|
* This is for internal use only. |
195
|
|
|
* |
196
|
|
|
* @param class-string $className the class name of an entity (`Post::class`) |
197
|
|
|
*/ |
198
|
28 |
|
public function getFilterGroupCondition(string $className): InputObjectType |
199
|
|
|
{ |
200
|
|
|
/** @var InputObjectType $type */ |
201
|
28 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className) . 'FilterGroupCondition', $this->filterGroupConditionTypeFactory); |
202
|
|
|
|
203
|
28 |
|
return $type; |
204
|
|
|
} |
205
|
|
|
|
206
|
8 |
|
public function getId(string $className): EntityIDType |
207
|
|
|
{ |
208
|
|
|
/** @var EntityIDType $type */ |
209
|
8 |
|
$type = $this->getViaFactory($className, Utils::getTypeName($className) . 'ID', $this->entityIDTypeFactory); |
210
|
|
|
|
211
|
8 |
|
return $type; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns an operator input type. |
216
|
|
|
* |
217
|
|
|
* This is for internal use only. |
218
|
|
|
* |
219
|
|
|
* @param class-string<AbstractOperator> $className the class name of an operator (`EqualOperatorType::class`) |
220
|
|
|
*/ |
221
|
20 |
|
public function getOperator(string $className, LeafType $type): AbstractOperator |
222
|
|
|
{ |
223
|
20 |
|
if (!is_a($className, AbstractOperator::class, true)) { |
224
|
1 |
|
throw new Exception('Expects a FQCN implementing `' . AbstractOperator::class . '`, but instead got: ' . $className); |
225
|
|
|
} |
226
|
|
|
|
227
|
19 |
|
$key = Utils::getOperatorTypeName($className, $type); |
228
|
|
|
|
229
|
19 |
|
if (!isset($this->types[$key])) { |
230
|
19 |
|
$instance = new $className($this, $type); |
231
|
19 |
|
$this->registerInstance($instance); |
232
|
|
|
} |
233
|
|
|
|
234
|
19 |
|
return $this->types[$key]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Register the given type in our internal registry with its name. |
239
|
|
|
* |
240
|
|
|
* This is for internal use only. You should declare custom types via the constructor, not this method. |
241
|
|
|
*/ |
242
|
121 |
|
public function registerInstance(Type&NamedType $instance): void |
243
|
|
|
{ |
244
|
121 |
|
$this->types[$instance->name()] = $instance; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Checks if a className is a valid doctrine entity. |
249
|
|
|
*/ |
250
|
67 |
|
public function isEntity(string $className): bool |
251
|
|
|
{ |
252
|
67 |
|
return class_exists($className) && !$this->entityManager->getMetadataFactory()->isTransient($className); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Initialize internal types for common needs. |
257
|
|
|
*/ |
258
|
121 |
|
private function initializeInternalTypes(): void |
259
|
|
|
{ |
260
|
121 |
|
$phpToGraphQLMapping = [ |
261
|
|
|
// PHP types |
262
|
121 |
|
'id' => Type::id(), |
263
|
121 |
|
'bool' => Type::boolean(), |
264
|
121 |
|
'int' => Type::int(), |
265
|
121 |
|
'float' => Type::float(), |
266
|
121 |
|
'string' => Type::string(), |
267
|
|
|
|
268
|
|
|
// Doctrine types |
269
|
121 |
|
'boolean' => Type::boolean(), |
270
|
121 |
|
'integer' => Type::int(), |
271
|
121 |
|
'smallint' => Type::int(), |
272
|
121 |
|
'bigint' => Type::int(), |
273
|
121 |
|
'decimal' => Type::string(), |
274
|
121 |
|
'text' => Type::string(), |
275
|
121 |
|
]; |
276
|
|
|
|
277
|
121 |
|
$this->types = $phpToGraphQLMapping; |
278
|
121 |
|
$this->registerInstance(new LogicalOperatorType()); |
279
|
121 |
|
$this->registerInstance(new JoinTypeType()); |
280
|
121 |
|
$this->registerInstance(new SortingOrderType()); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Throw an exception if the class name is not Doctrine entity. |
285
|
|
|
*/ |
286
|
65 |
|
private function throwIfNotEntity(string $className): void |
287
|
|
|
{ |
288
|
65 |
|
if (!$this->isEntity($className)) { |
289
|
1 |
|
throw new UnexpectedValueException('Given class name `' . $className . '` is not a Doctrine entity. Either register a custom GraphQL type for `' . $className . '` when instantiating `' . self::class . '`, or change the usage of that class to something else.'); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
22 |
|
public function createFilteredQueryBuilder(string $className, array $filter, array $sorting): QueryBuilder |
294
|
|
|
{ |
295
|
22 |
|
return $this->filteredQueryBuilderFactory->create($className, $filter, $sorting); |
296
|
|
|
} |
297
|
|
|
|
298
|
12 |
|
public function loadType(string $typeName, string $namespace): ?Type |
299
|
|
|
{ |
300
|
12 |
|
if ($this->has($typeName)) { |
301
|
1 |
|
return $this->get($typeName); |
302
|
|
|
} |
303
|
|
|
|
304
|
11 |
|
if (preg_match('~^(?<shortName>.*)(?<kind>PartialInput)$~', $typeName, $m) |
305
|
11 |
|
|| preg_match('~^(?<shortName>.*)(?<kind>Input|PartialInput|Filter|Sorting|FilterGroupJoin|FilterGroupCondition|ID)$~', $typeName, $m) |
306
|
11 |
|
|| preg_match('~^(?<kind>JoinOn)(?<shortName>.*)$~', $typeName, $m) |
307
|
11 |
|
|| preg_match('~^(?<shortName>.*)$~', $typeName, $m)) { |
308
|
11 |
|
$shortName = $m['shortName']; |
309
|
11 |
|
$kind = $m['kind'] ?? ''; |
310
|
|
|
|
311
|
|
|
/** @var class-string $className */ |
312
|
11 |
|
$className = $namespace . '\\' . $shortName; |
313
|
|
|
|
314
|
11 |
|
if ($this->isEntity($className)) { |
315
|
9 |
|
return match ($kind) { |
316
|
1 |
|
'Input' => $this->getViaFactory($className, $typeName, $this->inputTypeFactory), |
317
|
1 |
|
'PartialInput' => $this->getViaFactory($className, $typeName, $this->partialInputTypeFactory), |
318
|
1 |
|
'Filter' => $this->getViaFactory($className, $typeName, $this->filterTypeFactory), |
319
|
1 |
|
'Sorting' => $this->getViaFactory($className, $typeName, $this->sortingTypeFactory), |
320
|
1 |
|
'JoinOn' => $this->getViaFactory($className, $typeName, $this->joinOnTypeFactory), |
321
|
1 |
|
'FilterGroupJoin' => $this->getViaFactory($className, $typeName, $this->filterGroupJoinTypeFactory), |
322
|
1 |
|
'FilterGroupCondition' => $this->getViaFactory($className, $typeName, $this->filterGroupConditionTypeFactory), |
323
|
1 |
|
'ID' => $this->getViaFactory($className, $typeName, $this->entityIDTypeFactory), |
324
|
1 |
|
'' => $this->getViaFactory($className, $typeName, $this->objectTypeFactory), |
325
|
9 |
|
default => throw new Exception("Unsupported kind of type `$kind` when trying to load type `$typeName`"), |
326
|
9 |
|
}; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
2 |
|
return null; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|