Skip to content

Commit 2c4b602

Browse files
authored
Merge 7ab5c11 into acf933d
2 parents acf933d + 7ab5c11 commit 2c4b602

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/ControllerQueryProvider.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,21 @@ private function getFieldsByAnnotations(string $annotationName, bool $injectSour
165165

166166
$args = $this->mapParameters($refMethod, $docBlockObj);
167167

168-
$phpdocType = $typeResolver->resolve((string) $refMethod->getReturnType());
169-
170168
if ($queryAnnotation->getReturnType()) {
171169
$type = $this->registry->get($queryAnnotation->getReturnType());
172170
} else {
171+
$phpdocType = null;
172+
$returnType = $refMethod->getReturnType();
173+
if ($returnType !== null) {
174+
$phpdocType = $typeResolver->resolve((string) $refMethod->getReturnType());
175+
} else {
176+
$phpdocType = new Mixed_();
177+
}
178+
173179
$docBlockReturnType = $this->getDocBlocReturnType($docBlockObj, $refMethod);
174180

175181
try {
176-
$type = $this->mapType($phpdocType, $docBlockReturnType, $refMethod->getReturnType()->allowsNull(), false);
182+
$type = $this->mapType($phpdocType, $docBlockReturnType, $returnType ? $returnType->allowsNull() : true, false);
177183
} catch (TypeMappingException $e) {
178184
throw TypeMappingException::wrapWithReturnInfo($e, $refMethod);
179185
}
@@ -417,7 +423,7 @@ private function mapType(Type $type, ?Type $docBlockType, bool $isNullable, bool
417423
}
418424
} catch (TypeMappingException | CannotMapTypeException $e) {
419425
// Is the type iterable? If yes, let's analyze the docblock
420-
// TODO: it would be bettrr not to go through an exception for this.
426+
// TODO: it would be better not to go through an exception for this.
421427
if ($type instanceof Object_) {
422428
$fqcn = (string) $type->getFqsen();
423429
$refClass = new ReflectionClass($fqcn);

tests/ControllerQueryProviderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use GraphQL\Type\Definition\StringType;
1414
use GraphQL\Type\Definition\ObjectType;
1515
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestController;
16+
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestControllerNoReturnType;
1617
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
1718
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType;
1819
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeId;
@@ -290,4 +291,11 @@ public function testQueryProviderWithIterable()
290291
$this->assertInstanceOf(ObjectType::class, $iterableQuery->getType()->getWrappedType()->getWrappedType()->getWrappedType());
291292
$this->assertSame('TestObject', $iterableQuery->getType()->getWrappedType()->getWrappedType()->getWrappedType()->name);
292293
}
294+
295+
public function testNoReturnTypeError()
296+
{
297+
$queryProvider = new ControllerQueryProvider(new TestControllerNoReturnType(), $this->getRegistry());
298+
$this->expectException(TypeMappingException::class);
299+
$queryProvider->getQueries();
300+
}
293301
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQL\Controllers\Fixtures;
5+
6+
use ArrayObject;
7+
use TheCodingMachine\GraphQL\Controllers\Annotations\Logged;
8+
use TheCodingMachine\GraphQL\Controllers\Annotations\Mutation;
9+
use TheCodingMachine\GraphQL\Controllers\Annotations\Query;
10+
use TheCodingMachine\GraphQL\Controllers\Annotations\Right;
11+
12+
class TestControllerNoReturnType
13+
{
14+
/**
15+
* @Query()
16+
*/
17+
public function test()
18+
{
19+
return 'foo';
20+
}
21+
}

0 commit comments

Comments
 (0)