Skip to content

Commit d416c0d

Browse files
authored
Merge pull request #1151 from mathroc/patch-2
Merge guessed argument properties and args described with GQL\Arg
2 parents a6f5792 + b878a27 commit d416c0d

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/Config/Parser/MetadataParser/MetadataParser.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ private static function getTypeFieldConfigurationFromReflector(ReflectionClass $
570570
}
571571
}
572572

573-
if (empty($argAnnotations) && $reflector instanceof ReflectionMethod) {
574-
$args = self::guessArgs($reflectionClass, $reflector);
573+
if ($reflector instanceof ReflectionMethod) {
574+
$args = self::guessArgs($reflectionClass, $reflector, $args);
575575
}
576576

577577
if (!empty($args)) {
@@ -924,10 +924,16 @@ private static function guessType(ReflectionClass $reflectionClass, Reflector $r
924924
/**
925925
* Transform a method arguments from reflection to a list of GraphQL argument.
926926
*/
927-
private static function guessArgs(ReflectionClass $reflectionClass, ReflectionMethod $method): array
928-
{
929-
$arguments = [];
927+
private static function guessArgs(
928+
ReflectionClass $reflectionClass,
929+
ReflectionMethod $method,
930+
array $arguments,
931+
): array {
930932
foreach ($method->getParameters() as $index => $parameter) {
933+
if (array_key_exists($parameter->getName(), $arguments)) {
934+
continue;
935+
}
936+
931937
try {
932938
$gqlType = self::guessType($reflectionClass, $parameter, self::VALID_INPUT_TYPES);
933939
} catch (TypeGuessingException $exception) {

tests/Config/Parser/MetadataParserTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -452,15 +452,15 @@ public function testArgsAndReturnGuessing(): void
452452
'type' => 'Int',
453453
'args' => [
454454
'areaId' => ['type' => 'Int!'],
455-
'raceId' => ['type' => 'String!'],
455+
'raceId' => ['type' => 'String!', 'description' => 'A race ID'],
456456
'dayStart' => ['type' => 'Int', 'defaultValue' => null],
457457
'dayEnd' => ['type' => 'Int', 'defaultValue' => null],
458458
'nameStartingWith' => ['type' => 'String', 'defaultValue' => ''],
459459
'planet' => ['type' => 'PlanetInput', 'defaultValue' => null],
460460
'away' => ['type' => 'Boolean', 'defaultValue' => false],
461461
'maxDistance' => ['type' => 'Float', 'defaultValue' => null],
462462
],
463-
'resolve' => '@=call(value.getCasualties, arguments({areaId: "Int!", raceId: "String!", dayStart: "Int", dayEnd: "Int", nameStartingWith: "String", planet: "PlanetInput", away: "Boolean", maxDistance: "Float"}, args))',
463+
'resolve' => '@=call(value.getCasualties, arguments({raceId: "String!", areaId: "Int!", dayStart: "Int", dayEnd: "Int", nameStartingWith: "String", planet: "PlanetInput", away: "Boolean", maxDistance: "Float"}, args))',
464464
'complexity' => '@=childrenComplexity * 5',
465465
],
466466
],

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ final class Battle
2121

2222
/**
2323
* @GQL\Field(name="casualties", complexity="childrenComplexity * 5")
24+
*
25+
* @GQL\Arg(name="raceId", type="String!", description="A race ID")
2426
*/
2527
#[GQL\Field(name: 'casualties', complexity: 'childrenComplexity * 5')]
28+
#[GQL\Arg(name: 'raceId', type: 'String!', description: 'A race ID')]
2629
public function getCasualties(
2730
int $areaId,
28-
string $raceId,
31+
?string $raceId,
2932
int $dayStart = null,
3033
int $dayEnd = null,
3134
string $nameStartingWith = '',

0 commit comments

Comments
 (0)