Skip to content

Commit

Permalink
ResolveInfo injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincz committed Aug 8, 2024
1 parent 9f65551 commit 03c746b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
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);
}
}

0 comments on commit 03c746b

Please sign in to comment.