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

ResolveInfo injection in Query #1191

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Config/Parser/MetadataParser/MetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Overblog\GraphQLBundle\Config\Parser\MetadataParser;

use Doctrine\Common\Annotations\AnnotationException;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Annotation\Annotation as Meta;
use Overblog\GraphQLBundle\Annotation as Metadata;
use Overblog\GraphQLBundle\Annotation\InputField;
Expand All @@ -15,10 +16,12 @@
use Overblog\GraphQLBundle\Config\Parser\PreParserInterface;
use Overblog\GraphQLBundle\Relay\Connection\ConnectionInterface;
use Overblog\GraphQLBundle\Relay\Connection\EdgeInterface;
use Overblog\GraphQLBundle\Transformer\ArgumentsTransformer;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionException;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionProperty;
use Reflector;
Expand Down Expand Up @@ -617,8 +620,9 @@ private static function getTypeFieldConfigurationFromReflector(ReflectionClass $
$args = self::guessArgs($reflectionClass, $reflector, $args);
}

if (!empty($args)) {
$fieldConfiguration['args'] = $args;
$gqlArgs = array_filter($args, fn ($arg) => !isset($arg['internal']));
if (!empty($gqlArgs)) {
$fieldConfiguration['args'] = $gqlArgs;
}

$fieldName = $fieldMetadata->name ?? $fieldName;
Expand Down Expand Up @@ -985,6 +989,14 @@ private static function guessArgs(
continue;
}

if ($parameter->getType() instanceof ReflectionNamedType) {
$className = $parameter->getType()->getName();
if (ResolveInfo::class === $className || is_subclass_of($className, ResolveInfo::class)) {
$arguments[$parameter->getName()] = ['type' => ArgumentsTransformer::RESOLVE_INFO_TOKEN, 'internal' => true];
continue;
}
}

try {
$gqlType = self::guessType($reflectionClass, $parameter, self::VALID_INPUT_TYPES);
} catch (TypeGuessingException $exception) {
Expand Down
6 changes: 6 additions & 0 deletions src/Transformer/ArgumentsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

final class ArgumentsTransformer
{
public const RESOLVE_INFO_TOKEN = '#ResolveInfo';

private PropertyAccessor $accessor;
private ?ValidatorInterface $validator;
private array $classesMap;
Expand Down Expand Up @@ -190,6 +192,10 @@ public function getArguments(array $mapping, $data, ResolveInfo $info)

foreach ($mapping as $name => $type) {
try {
if (self::RESOLVE_INFO_TOKEN === $type) {
$args[] = $info;
continue;
}
$value = $this->getInstanceAndValidate($type, $data[$name], $info, $name);
$args[] = $value;
} catch (InvalidArgumentError $exception) {
Expand Down
9 changes: 9 additions & 0 deletions tests/Config/Parser/MetadataParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,15 @@ public function testProviders(): void
'access' => '@=default_access',
'public' => '@=default_public',
],
'planet_isHabitablePlanet' => [
'type' => 'Boolean!',
'args' => [
'planetId' => ['type' => 'Int!'],
],
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').isHabitablePlanet, arguments({planetId: \"Int!\", info: \"#ResolveInfo\"}, args))",
'access' => '@=default_access',
'public' => '@=default_public',
],
],
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Repository;

use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Annotation as GQL;
use Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Type\Planet;

Expand Down Expand Up @@ -138,4 +139,13 @@ public function getNextPlanet(int $planetId, int $minDistance, int $maxDistance)
'maxDistance' => $maxDistance,
];
}

/**
* @GQL\Query
*/
#[GQL\Query]
public function isHabitablePlanet(int $planetId, ResolveInfo $info): bool
{
return true;
}
}
12 changes: 11 additions & 1 deletion tests/Transformer/ArgumentsTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function setUp(): void
}
}

private function getTransformer(array $classesMap = null, ConstraintViolationList $validateReturn = null): ArgumentsTransformer
private function getTransformer(array $classesMap = [], ConstraintViolationList $validateReturn = null): ArgumentsTransformer
{
$validator = $this->createMock(RecursiveValidator::class);
$validator->method('validate')->willReturn($validateReturn ?? new ConstraintViolationList());
Expand Down Expand Up @@ -455,4 +455,14 @@ public function testIgnoreNonInputObjectValidation(): void

$this->assertEquals($data, $typeValue);
}

public function testResolveInfoInjection(): void
{
$transformer = $this->getTransformer();
$resolveInfo = $this->getResolveInfo([]);

$result = $transformer->getArguments(['resolveInfo' => ArgumentsTransformer::RESOLVE_INFO_TOKEN], [], $resolveInfo);

$this->assertTrue($result[0] === $resolveInfo);
}
}
Loading