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\Type\Definition\IDType; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A specialized ID type that allows to fetch entity from DB |
13
|
|
|
*/ |
14
|
|
|
class EntityIDType extends IDType |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EntityManager |
18
|
|
|
*/ |
19
|
|
|
private $entityManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The entity class name |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $className; |
27
|
|
|
|
28
|
9 |
|
public function __construct(EntityManager $entityManager, string $className) |
|
|
|
|
29
|
|
|
{ |
30
|
9 |
|
$this->entityManager = $entityManager; |
31
|
9 |
|
$this->className = $className; |
32
|
9 |
|
$this->name = Utils::getIDTypeName($className); |
33
|
9 |
|
$this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed'; |
34
|
|
|
|
35
|
9 |
|
parent::__construct(); |
36
|
9 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Serializes an internal value to include in a response. |
40
|
|
|
* |
41
|
|
|
* @param mixed $value |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
public function serialize($value) |
46
|
|
|
{ |
47
|
1 |
|
$id = $this->entityManager->getClassMetadata($this->className)->getIdentifierValues($value); |
48
|
|
|
|
49
|
1 |
|
return (string) reset($id); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Parses an externally provided value (query variable) to use as an input |
54
|
|
|
* |
55
|
|
|
* @param mixed $value |
56
|
|
|
* |
57
|
|
|
* @return mixed A Doctrine entity |
58
|
|
|
*/ |
59
|
2 |
|
public function parseValue($value) |
60
|
|
|
{ |
61
|
2 |
|
$value = parent::parseValue($value); |
62
|
|
|
|
63
|
2 |
|
return $this->createEntityID($value); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input |
68
|
|
|
* |
69
|
|
|
* @param \GraphQL\Language\AST\Node $valueNode |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
2 |
|
public function parseLiteral($valueNode) |
74
|
|
|
{ |
75
|
2 |
|
$value = parent::parseLiteral($valueNode); |
76
|
|
|
|
77
|
2 |
|
return $this->createEntityID($value); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create EntityID to retrieve entity from DB later on |
82
|
|
|
* |
83
|
|
|
* @return mixed entity |
84
|
|
|
*/ |
85
|
4 |
|
private function createEntityID(string $id) |
86
|
|
|
{ |
87
|
4 |
|
return new EntityID($this->entityManager, $this->className, $id); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.