From 1744dc91f043bc657a58ee7943433af15b0d58b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 26 Dec 2019 12:01:51 +0100 Subject: [PATCH 1/2] Adding failing test --- tests/Fixtures/Integration/Models/Contact.php | 6 +++--- tests/Integration/EndToEndTest.php | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/Fixtures/Integration/Models/Contact.php b/tests/Fixtures/Integration/Models/Contact.php index ed99fb9632..13d07b4b49 100644 --- a/tests/Fixtures/Integration/Models/Contact.php +++ b/tests/Fixtures/Integration/Models/Contact.php @@ -21,7 +21,7 @@ /** * @Type() - * @MagicField(name="magicNumber", phpType="int") + * @MagicField(name="magicContact", phpType="Contact") */ class Contact { @@ -54,7 +54,7 @@ public function __construct(string $name) { $this->name = $name; } - + public function getName() { return $this->name; @@ -210,6 +210,6 @@ public function injectServiceFromExternal(string $testService, string $testSkip public function __get(string $property) { - return 42; + return new Contact('foo'); } } diff --git a/tests/Integration/EndToEndTest.php b/tests/Integration/EndToEndTest.php index fee5a13e12..94654f7cc2 100644 --- a/tests/Integration/EndToEndTest.php +++ b/tests/Integration/EndToEndTest.php @@ -1297,7 +1297,9 @@ public function testEndToEndMagicFieldWithPhpType(): void $queryString = ' query { contacts { - magicNumber + magicContact { + name + } } } '; @@ -1310,10 +1312,14 @@ public function testEndToEndMagicFieldWithPhpType(): void $this->assertSame([ 'contacts' => [ [ - 'magicNumber' => 42, + 'magicContact' => [ + 'name' => 'foo' + ] ], [ - 'magicNumber' => 42, + 'magicContact' => [ + 'name' => 'foo' + ] ], ] ], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); From a50830d3cecf3c1001ba863d39b9ab8f0d1050f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 26 Dec 2019 12:06:07 +0100 Subject: [PATCH 2/2] Adding context before resolving type --- src/FieldsBuilder.php | 5 +++-- tests/Fixtures/Integration/Models/Contact.php | 3 --- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/FieldsBuilder.php b/src/FieldsBuilder.php index 433b9e9db3..06d01ed20f 100644 --- a/src/FieldsBuilder.php +++ b/src/FieldsBuilder.php @@ -472,10 +472,11 @@ private function resolvePhpType(string $phpTypeStr, ReflectionClass $refClass, R { $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); - $phpdocType = $typeResolver->resolve($phpTypeStr); + $context = $this->cachedDocBlockFactory->getContextFromClass($refClass); + $phpdocType = $typeResolver->resolve($phpTypeStr, $context); Assert::notNull($phpdocType); - $fakeDocBlock = new DocBlock('', null, [new DocBlock\Tags\Return_($phpdocType)], $this->cachedDocBlockFactory->getContextFromClass($refClass)); + $fakeDocBlock = new DocBlock('', null, [new DocBlock\Tags\Return_($phpdocType)], $context); return $this->typeMapper->mapReturnType($refMethod, $fakeDocBlock); // TODO: add a catch to CannotMapTypeExceptionInterface and a "addMagicFieldInfo" method to know where the issues are coming from. diff --git a/tests/Fixtures/Integration/Models/Contact.php b/tests/Fixtures/Integration/Models/Contact.php index 13d07b4b49..2ea264f6bb 100644 --- a/tests/Fixtures/Integration/Models/Contact.php +++ b/tests/Fixtures/Integration/Models/Contact.php @@ -14,9 +14,6 @@ use TheCodingMachine\GraphQLite\Annotations\Parameter; use TheCodingMachine\GraphQLite\Annotations\Right; use TheCodingMachine\GraphQLite\Annotations\Type; -use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper; -use TheCodingMachine\GraphQLite\Mappers\Parameters\ParameterHandlerInterface; -use TheCodingMachine\GraphQLite\TypeRegistry; use TheCodingMachine\GraphQLite\Annotations\Autowire; /**