1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine\Definition; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use GraphQL\Doctrine\Utils; |
9
|
|
|
use GraphQL\Error\Error; |
10
|
|
|
use GraphQL\Language\AST\IntValueNode; |
11
|
|
|
use GraphQL\Language\AST\Node; |
12
|
|
|
use GraphQL\Language\AST\StringValueNode; |
13
|
|
|
use GraphQL\Type\Definition\ScalarType; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A specialized ID type that allows to fetch entity from DB. |
17
|
|
|
*/ |
18
|
|
|
final class EntityIDType extends ScalarType |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param class-string $className |
22
|
|
|
*/ |
23
|
18 |
|
public function __construct(private readonly EntityManager $entityManager, /** |
|
|
|
|
24
|
|
|
* The entity class name. |
25
|
|
|
*/ |
26
|
|
|
private readonly string $className, string $typeName) |
27
|
|
|
{ |
28
|
18 |
|
$this->name = $typeName; |
29
|
18 |
|
$this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed'; |
30
|
|
|
|
31
|
18 |
|
parent::__construct(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Serializes an internal value to include in a response. |
36
|
|
|
* |
37
|
|
|
* @param object $value |
38
|
|
|
*/ |
39
|
1 |
|
public function serialize($value): string |
40
|
|
|
{ |
41
|
1 |
|
$id = $this->entityManager->getClassMetadata($this->className)->getIdentifierValues($value); |
42
|
|
|
|
43
|
|
|
// @phpstan-ignore-next-line |
44
|
1 |
|
return (string) reset($id); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Parses an externally provided value (query variable) to use as an input. |
49
|
|
|
* |
50
|
|
|
* @param mixed $value |
51
|
|
|
*/ |
52
|
4 |
|
public function parseValue($value, ?array $variables = null): EntityID |
53
|
|
|
{ |
54
|
4 |
|
if (!is_string($value) && !is_int($value)) { |
55
|
1 |
|
throw new Error('EntityID cannot represent value: ' . \GraphQL\Utils\Utils::printSafe($value)); |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
return $this->createEntityID((string) $value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input. |
63
|
|
|
* |
64
|
|
|
* @param Node $valueNode |
65
|
|
|
*/ |
66
|
4 |
|
public function parseLiteral($valueNode, ?array $variables = null): EntityID |
67
|
|
|
{ |
68
|
4 |
|
if ($valueNode instanceof StringValueNode || $valueNode instanceof IntValueNode) { |
69
|
3 |
|
return $this->createEntityID((string) $valueNode->value); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Intentionally without message, as all information already in wrapped Exception |
73
|
1 |
|
throw new Error(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create EntityID to retrieve entity from DB later on. |
78
|
|
|
*/ |
79
|
6 |
|
private function createEntityID(string $id): EntityID |
80
|
|
|
{ |
81
|
6 |
|
return new EntityID($this->entityManager, $this->className, $id); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|