Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge guessed argument properties and args described with GQL\Arg #1151

Merged
merged 10 commits into from
Mar 5, 2024
16 changes: 11 additions & 5 deletions src/Config/Parser/MetadataParser/MetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ private static function getTypeFieldConfigurationFromReflector(ReflectionClass $
}
}

if (empty($argAnnotations) && $reflector instanceof ReflectionMethod) {
$args = self::guessArgs($reflectionClass, $reflector);
if ($reflector instanceof ReflectionMethod) {
$args = self::guessArgs($reflectionClass, $reflector, $args);
}

if (!empty($args)) {
Expand Down Expand Up @@ -924,10 +924,16 @@ private static function guessType(ReflectionClass $reflectionClass, Reflector $r
/**
* Transform a method arguments from reflection to a list of GraphQL argument.
*/
private static function guessArgs(ReflectionClass $reflectionClass, ReflectionMethod $method): array
{
$arguments = [];
private static function guessArgs(
ReflectionClass $reflectionClass,
ReflectionMethod $method,
array $arguments,
): array {
foreach ($method->getParameters() as $index => $parameter) {
if (array_key_exists($parameter->getName(), $arguments)) {
continue;
}

try {
$gqlType = self::guessType($reflectionClass, $parameter, self::VALID_INPUT_TYPES);
} catch (TypeGuessingException $exception) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Config/Parser/MetadataParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,15 @@ public function testArgsAndReturnGuessing(): void
'type' => 'Int',
'args' => [
'areaId' => ['type' => 'Int!'],
'raceId' => ['type' => 'String!'],
'raceId' => ['type' => 'String!', 'description' => 'A race ID'],
'dayStart' => ['type' => 'Int', 'defaultValue' => null],
'dayEnd' => ['type' => 'Int', 'defaultValue' => null],
'nameStartingWith' => ['type' => 'String', 'defaultValue' => ''],
'planet' => ['type' => 'PlanetInput', 'defaultValue' => null],
'away' => ['type' => 'Boolean', 'defaultValue' => false],
'maxDistance' => ['type' => 'Float', 'defaultValue' => null],
],
'resolve' => '@=call(value.getCasualties, arguments({areaId: "Int!", raceId: "String!", dayStart: "Int", dayEnd: "Int", nameStartingWith: "String", planet: "PlanetInput", away: "Boolean", maxDistance: "Float"}, args))',
'resolve' => '@=call(value.getCasualties, arguments({raceId: "String!", areaId: "Int!", dayStart: "Int", dayEnd: "Int", nameStartingWith: "String", planet: "PlanetInput", away: "Boolean", maxDistance: "Float"}, args))',
'complexity' => '@=childrenComplexity * 5',
],
],
Expand Down
5 changes: 4 additions & 1 deletion tests/Config/Parser/fixtures/annotations/Type/Battle.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ final class Battle

/**
* @GQL\Field(name="casualties", complexity="childrenComplexity * 5")
*
* @GQL\Arg(name="raceId", type="String!", description="A race ID")
*/
#[GQL\Field(name: 'casualties', complexity: 'childrenComplexity * 5')]
#[GQL\Arg(name: 'raceId', type: 'String!', description: 'A race ID')]
public function getCasualties(
int $areaId,
string $raceId,
?string $raceId,
int $dayStart = null,
int $dayEnd = null,
string $nameStartingWith = '',
Expand Down
Loading