Skip to content

Commit e66676e

Browse files
committed
Prefer property type hint over Doctrine annotation
1 parent 25813fb commit e66676e

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/Config/Parser/AnnotationParser.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,24 @@ private static function getGraphQLInputFieldsFromAnnotations(GraphClass $graphCl
636636
}
637637

638638
$fieldName = $reflector->getName();
639-
$fieldType = $fieldAnnotation->type ?? null;
640-
if (!$fieldType) {
641-
if ($reflector instanceof ReflectionProperty && $reflector->hasType()) {
642-
$fieldType = self::resolveGraphQLTypeFromReflectionType($reflector->getType(), self::VALID_INPUT_TYPES);
643-
} else {
644-
$fieldType = self::guessType($graphClass, $annotations);
639+
if (isset($fieldAnnotation->type)) {
640+
$fieldType = $fieldAnnotation->type;
641+
} else {
642+
if ($reflector instanceof ReflectionProperty) {
643+
if ($reflector->hasType()) {
644+
try {
645+
$fieldType = self::resolveGraphQLTypeFromReflectionType($reflector->getType(), self::VALID_INPUT_TYPES);
646+
} catch (Exception $e) {
647+
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()));
648+
}
649+
} else {
650+
try {
651+
$fieldType = self::guessType($graphClass, $annotations);
652+
} catch (Exception $e) {
653+
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()));
654+
655+
}
656+
}
645657
}
646658
}
647659
$fieldConfiguration = [];

tests/Config/Parser/AnnotationParserTest.php

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ class Planet
3434
*/
3535
protected $diameter;
3636

37+
/**
38+
* @GQL\Field
39+
* @ORM\Column(type="boolean")
40+
*/
41+
protected int $variable;
42+
3743
protected $dummy;
3844
}

0 commit comments

Comments
 (0)