1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine\Definition; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
use GraphQL\Doctrine\Utils; |
10
|
|
|
use GraphQL\Type\Definition\IDType; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A specialized ID type that allows to fetch entity from DB |
14
|
|
|
*/ |
15
|
|
|
class EntityIDType extends IDType |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EntityManager |
19
|
|
|
*/ |
20
|
|
|
private $entityManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The entity class name |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $className; |
28
|
|
|
|
29
|
7 |
|
public function __construct(EntityManager $entityManager, string $className) |
|
|
|
|
30
|
|
|
{ |
31
|
7 |
|
$this->entityManager = $entityManager; |
32
|
7 |
|
$this->className = $className; |
33
|
7 |
|
$this->name = Utils::getIDTypeName($className); |
34
|
7 |
|
$this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed'; |
35
|
|
|
|
36
|
7 |
|
parent::__construct(); |
37
|
7 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Serializes an internal value to include in a response. |
41
|
|
|
* |
42
|
|
|
* @param mixed $value |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
1 |
|
public function serialize($value) |
47
|
|
|
{ |
48
|
1 |
|
$id = $this->entityManager->getClassMetadata($this->className)->getIdentifierValues($value); |
49
|
|
|
|
50
|
1 |
|
return (string) reset($id); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Parses an externally provided value (query variable) to use as an input |
55
|
|
|
* |
56
|
|
|
* @param mixed $value |
57
|
|
|
* |
58
|
|
|
* @return mixed A Doctrine entity |
59
|
|
|
*/ |
60
|
1 |
|
public function parseValue($value) |
61
|
|
|
{ |
62
|
1 |
|
$value = parent::parseValue($value); |
63
|
|
|
|
64
|
1 |
|
return $this->getRepository()->find($value); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input |
69
|
|
|
* |
70
|
|
|
* @param \GraphQL\Language\AST\Node $valueNode |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
1 |
|
public function parseLiteral($valueNode) |
75
|
|
|
{ |
76
|
1 |
|
$value = parent::parseLiteral($valueNode); |
77
|
|
|
|
78
|
1 |
|
return $this->getRepository()->find($value); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the repository for our entity |
83
|
|
|
* |
84
|
|
|
* @return EntityRepository |
85
|
|
|
*/ |
86
|
2 |
|
private function getRepository(): EntityRepository |
87
|
|
|
{ |
88
|
2 |
|
return $this->entityManager->getRepository($this->className); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.