Skip to content

Commit 00dd070

Browse files
committed
Support for self in type hinting
1 parent 5151fc7 commit 00dd070

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed

src/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace GraphQL\Doctrine;
66

77
/**
8-
* Exception specific to this GraphQL Doctrine
8+
* Exception specific to GraphQL Doctrine
99
*/
1010
class Exception extends \Exception
1111
{

src/Factory/AbstractFieldsConfigurationFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,12 @@ protected function getTypeFromReturnTypeHint(ReflectionMethod $method, string $f
241241
*/
242242
protected function reflectionTypeToType(ReflectionType $reflectionType, bool $isEntityId = false): Type
243243
{
244-
$type = $this->getTypeFromRegistry((string) $reflectionType, $isEntityId);
244+
$name = $reflectionType->getName();
245+
if ($name === 'self') {
246+
$name = $this->metadata->name;
247+
}
248+
249+
$type = $this->getTypeFromRegistry($name, $isEntityId);
245250
if (!$reflectionType->allowsNull()) {
246251
$type = Type::nonNull($type);
247252
}

src/Factory/OutputFieldsConfigurationFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private function getArgumentsFromTypeHint(ReflectionMethod $method, array $argsF
138138
* @param ReflectionMethod $method
139139
* @param ReflectionParameter $param
140140
* @param Argument $arg
141+
* @param DocBlockReader $docBlock
141142
*/
142143
private function completeArgumentFromTypeHint(ReflectionMethod $method, ReflectionParameter $param, Argument $arg, DocBlockReader $docBlock): void
143144
{

tests/Blog/Model/User.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace GraphQLTests\Doctrine\Blog\Model;
66

7+
use Doctrine\Common\Collections\ArrayCollection;
78
use Doctrine\Common\Collections\Collection;
89
use Doctrine\ORM\Mapping as ORM;
910
use GraphQL\Doctrine\Annotation as API;
@@ -50,6 +51,13 @@ class User extends AbstractModel
5051
*/
5152
private $posts;
5253

54+
/**
55+
* @var User
56+
*
57+
* @ORM\ManyToOne(targetEntity="GraphQLTests\Doctrine\Blog\Model\User")
58+
*/
59+
private $manager;
60+
5361
/**
5462
* Constructor
5563
*
@@ -60,7 +68,7 @@ public function __construct(?int $id = null)
6068
// This is a bad idea in real world, but we are just testing stuff here
6169
$this->id = $id;
6270

63-
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
71+
$this->posts = new ArrayCollection();
6472
}
6573

6674
/**
@@ -179,4 +187,14 @@ public function getPostsWithIds(array $ids): Collection
179187
return in_array($post->getId(), $ids, true);
180188
});
181189
}
190+
191+
public function setManager(?self $manager): void
192+
{
193+
$this->manager = $manager;
194+
}
195+
196+
public function getManager(): ?self
197+
{
198+
return $this->manager;
199+
}
182200
}

tests/data/UserInput.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
'description' => 'Encrypt and change the user password',
2525
'defaultValue' => null,
2626
],
27+
[
28+
'name' => 'manager',
29+
'type' => 'UserID',
30+
'description' => null,
31+
'defaultValue' => null,
32+
],
2733
[
2834
'name' => 'creationDate',
2935
'type' => 'DateTime!',

tests/data/UserOutput.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
],
5151
],
5252
],
53+
[
54+
'name' => 'manager',
55+
'type' => 'User',
56+
'description' => null,
57+
'args' => [],
58+
],
5359
[
5460
'name' => 'id',
5561
'type' => 'ID!',

0 commit comments

Comments
 (0)