Skip to content

Commit a8c5661

Browse files
authored
Merge pull request #1191 from sparklink-pro/master
ResolveInfo injection in Query
2 parents 9f65551 + 03c746b commit a8c5661

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

src/Config/Parser/MetadataParser/MetadataParser.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Overblog\GraphQLBundle\Config\Parser\MetadataParser;
66

77
use Doctrine\Common\Annotations\AnnotationException;
8+
use GraphQL\Type\Definition\ResolveInfo;
89
use Overblog\GraphQLBundle\Annotation\Annotation as Meta;
910
use Overblog\GraphQLBundle\Annotation as Metadata;
1011
use Overblog\GraphQLBundle\Annotation\InputField;
@@ -15,10 +16,12 @@
1516
use Overblog\GraphQLBundle\Config\Parser\PreParserInterface;
1617
use Overblog\GraphQLBundle\Relay\Connection\ConnectionInterface;
1718
use Overblog\GraphQLBundle\Relay\Connection\EdgeInterface;
19+
use Overblog\GraphQLBundle\Transformer\ArgumentsTransformer;
1820
use ReflectionClass;
1921
use ReflectionClassConstant;
2022
use ReflectionException;
2123
use ReflectionMethod;
24+
use ReflectionNamedType;
2225
use ReflectionParameter;
2326
use ReflectionProperty;
2427
use Reflector;
@@ -617,8 +620,9 @@ private static function getTypeFieldConfigurationFromReflector(ReflectionClass $
617620
$args = self::guessArgs($reflectionClass, $reflector, $args);
618621
}
619622

620-
if (!empty($args)) {
621-
$fieldConfiguration['args'] = $args;
623+
$gqlArgs = array_filter($args, fn ($arg) => !isset($arg['internal']));
624+
if (!empty($gqlArgs)) {
625+
$fieldConfiguration['args'] = $gqlArgs;
622626
}
623627

624628
$fieldName = $fieldMetadata->name ?? $fieldName;
@@ -985,6 +989,14 @@ private static function guessArgs(
985989
continue;
986990
}
987991

992+
if ($parameter->getType() instanceof ReflectionNamedType) {
993+
$className = $parameter->getType()->getName();
994+
if (ResolveInfo::class === $className || is_subclass_of($className, ResolveInfo::class)) {
995+
$arguments[$parameter->getName()] = ['type' => ArgumentsTransformer::RESOLVE_INFO_TOKEN, 'internal' => true];
996+
continue;
997+
}
998+
}
999+
9881000
try {
9891001
$gqlType = self::guessType($reflectionClass, $parameter, self::VALID_INPUT_TYPES);
9901002
} catch (TypeGuessingException $exception) {

src/Transformer/ArgumentsTransformer.php

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
final class ArgumentsTransformer
3030
{
31+
public const RESOLVE_INFO_TOKEN = '#ResolveInfo';
32+
3133
private PropertyAccessor $accessor;
3234
private ?ValidatorInterface $validator;
3335
private array $classesMap;
@@ -190,6 +192,10 @@ public function getArguments(array $mapping, $data, ResolveInfo $info)
190192

191193
foreach ($mapping as $name => $type) {
192194
try {
195+
if (self::RESOLVE_INFO_TOKEN === $type) {
196+
$args[] = $info;
197+
continue;
198+
}
193199
$value = $this->getInstanceAndValidate($type, $data[$name], $info, $name);
194200
$args[] = $value;
195201
} catch (InvalidArgumentError $exception) {

tests/Config/Parser/MetadataParserTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ public function testProviders(): void
372372
'access' => '@=default_access',
373373
'public' => '@=default_public',
374374
],
375+
'planet_isHabitablePlanet' => [
376+
'type' => 'Boolean!',
377+
'args' => [
378+
'planetId' => ['type' => 'Int!'],
379+
],
380+
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').isHabitablePlanet, arguments({planetId: \"Int!\", info: \"#ResolveInfo\"}, args))",
381+
'access' => '@=default_access',
382+
'public' => '@=default_public',
383+
],
375384
],
376385
]);
377386

tests/Config/Parser/fixtures/annotations/Repository/PlanetRepository.php

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

7+
use GraphQL\Type\Definition\ResolveInfo;
78
use Overblog\GraphQLBundle\Annotation as GQL;
89
use Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Type\Planet;
910

@@ -138,4 +139,13 @@ public function getNextPlanet(int $planetId, int $minDistance, int $maxDistance)
138139
'maxDistance' => $maxDistance,
139140
];
140141
}
142+
143+
/**
144+
* @GQL\Query
145+
*/
146+
#[GQL\Query]
147+
public function isHabitablePlanet(int $planetId, ResolveInfo $info): bool
148+
{
149+
return true;
150+
}
141151
}

tests/Transformer/ArgumentsTransformerTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setUp(): void
3737
}
3838
}
3939

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

456456
$this->assertEquals($data, $typeValue);
457457
}
458+
459+
public function testResolveInfoInjection(): void
460+
{
461+
$transformer = $this->getTransformer();
462+
$resolveInfo = $this->getResolveInfo([]);
463+
464+
$result = $transformer->getArguments(['resolveInfo' => ArgumentsTransformer::RESOLVE_INFO_TOKEN], [], $resolveInfo);
465+
466+
$this->assertTrue($result[0] === $resolveInfo);
467+
}
458468
}

0 commit comments

Comments
 (0)