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 ed99fb9632..2ea264f6bb 100644 --- a/tests/Fixtures/Integration/Models/Contact.php +++ b/tests/Fixtures/Integration/Models/Contact.php @@ -14,14 +14,11 @@ 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; /** * @Type() - * @MagicField(name="magicNumber", phpType="int") + * @MagicField(name="magicContact", phpType="Contact") */ class Contact { @@ -54,7 +51,7 @@ public function __construct(string $name) { $this->name = $name; } - + public function getName() { return $this->name; @@ -210,6 +207,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']);