Skip to content

Commit 5a1dced

Browse files
committed
Refactor guessType to also check property type hint
1 parent e2661fb commit 5a1dced

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

src/Config/Parser/AnnotationParser.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Overblog\GraphQLBundle\Config\Parser;
66

77
use Doctrine\Common\Annotations\AnnotationException;
8+
use Doctrine\Common\Collections\Collection;
89
use Doctrine\ORM\Mapping\Column;
910
use Doctrine\ORM\Mapping\JoinColumn;
1011
use Doctrine\ORM\Mapping\ManyToMany;
@@ -585,7 +586,7 @@ private static function getTypeFieldConfigurationFromReflector(GraphClass $graph
585586
}
586587
} else {
587588
try {
588-
$fieldConfiguration['type'] = self::guessType($graphClass, $annotations);
589+
$fieldConfiguration['type'] = self::guessType($graphClass, $reflector, self::VALID_OUTPUT_TYPES);
589590
} catch (Exception $e) {
590591
throw new InvalidArgumentException(sprintf('The attribute "type" on "@%s" defined on "%s" is required and cannot be auto-guessed : %s.', $fieldAnnotationName, $reflector->getName(), $e->getMessage()));
591592
}
@@ -639,22 +640,11 @@ private static function getGraphQLInputFieldsFromAnnotations(GraphClass $graphCl
639640
if (isset($fieldAnnotation->type)) {
640641
$fieldType = $fieldAnnotation->type;
641642
} else {
642-
if ($reflector instanceof ReflectionProperty) {
643-
if ($reflector->hasType()) {
644-
try {
645-
// @phpstan-ignore-next-line
646-
$fieldType = self::resolveGraphQLTypeFromReflectionType($reflector->getType(), self::VALID_INPUT_TYPES);
647-
} catch (Exception $e) {
648-
throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on property "%s" and cannot be auto-guessed from type hint "%s"', GQL\Field::class, $reflector->getName(), (string) $reflector->getType()));
649-
}
650-
} else {
651-
try {
652-
$fieldType = self::guessType($graphClass, $annotations);
653-
} catch (Exception $e) {
654-
throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on property "%s" and cannot be auto-guessed as there is no type hint or Doctrine annotation.', GQL\Field::class, $reflector->getName()));
643+
try {
644+
$fieldType = self::guessType($graphClass, $reflector, self::VALID_INPUT_TYPES);
645+
} catch (Exception $e) {
646+
throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on property "%s" and cannot be auto-guessed as there is no type hint or Doctrine annotation.', GQL\Field::class, $reflector->getName()));
655647

656-
}
657-
}
658648
}
659649
}
660650
$fieldConfiguration = [];
@@ -864,8 +854,17 @@ private static function suffixName(string $name, string $suffix): string
864854
*
865855
* @throws RuntimeException
866856
*/
867-
private static function guessType(GraphClass $graphClass, array $annotations): string
857+
private static function guessType(GraphClass $graphClass, ReflectionProperty $reflector, array $filterGraphQLTypes = []): string
868858
{
859+
if ($reflector->hasType()) {
860+
try {
861+
// @phpstan-ignore-next-line
862+
return self::resolveGraphQLTypeFromReflectionType($reflector->getType(), $filterGraphQLTypes);
863+
} catch (Exception $e) {
864+
}
865+
}
866+
867+
$annotations = $graphClass->getAnnotations($reflector);
869868
$columnAnnotation = self::getFirstAnnotationMatching($annotations, Column::class);
870869
if (null !== $columnAnnotation) {
871870
$type = self::resolveTypeFromDoctrineType($columnAnnotation->type);

tests/Config/Parser/AnnotationParserTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public function testInput(): void
191191
'description' => ['type' => 'String!'],
192192
'diameter' => ['type' => 'Int'],
193193
'variable' => ['type' => 'Int!'],
194+
'tags' => ['type' => '[String]!'],
194195
],
195196
]);
196197
}

tests/Config/Parser/fixtures/annotations/Input/Planet.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ class Planet
4343

4444
// @phpstan-ignore-next-line
4545
protected $dummy;
46+
47+
/**
48+
* @GQL\Field
49+
* @ORM\Column(type="text[]")
50+
*/
51+
protected array $tags;
4652
}

tests/Config/Parser/fixtures/annotations/Type/Droid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class Droid extends Character
1414
{
1515
/**
16-
* @GQL\Field(type="Int!")
16+
* @GQL\Field
1717
*/
1818
protected int $memory;
1919
}

tests/Config/Parser/fixtures/annotations/Type/Lightsaber.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,44 @@ class Lightsaber
2323
* @ORM\Column(type="integer", nullable=true)
2424
* @GQL\Field
2525
*/
26-
protected int $size;
26+
// @phpstan-ignore-next-line
27+
protected $size;
2728

2829
/**
2930
* @ORM\OneToMany(targetEntity="Hero")
3031
* @GQL\Field
3132
*/
32-
protected Hero $holders;
33+
// @phpstan-ignore-next-line
34+
protected $holders;
3335

3436
/**
3537
* @ORM\ManyToOne(targetEntity="Hero")
3638
* @GQL\Field
3739
*/
38-
protected Hero $creator;
40+
// @phpstan-ignore-next-line
41+
protected $creator;
3942

4043
/**
4144
* @ORM\OneToOne(targetEntity="Crystal")
4245
* @GQL\Field
4346
*/
44-
protected Crystal $crystal;
47+
// @phpstan-ignore-next-line
48+
protected $crystal;
4549

4650
/**
4751
* @ORM\ManyToMany(targetEntity="Battle")
4852
* @GQL\Field
4953
*/
50-
protected Battle $battles;
54+
// @phpstan-ignore-next-line
55+
protected $battles;
5156

5257
/**
5358
* @GQL\Field
5459
* @ORM\OneToOne(targetEntity="Hero")
5560
* @ORM\JoinColumn(nullable=true)
5661
*/
57-
protected Hero $currentHolder;
62+
// @phpstan-ignore-next-line
63+
protected $currentHolder;
5864

5965
/**
6066
* @GQL\Field

0 commit comments

Comments
 (0)