From bd1425487a03510e0bb9a11efc81c9dbd8345c3a Mon Sep 17 00:00:00 2001 From: spawnia Date: Wed, 17 Jul 2019 21:33:52 +0200 Subject: [PATCH 1/4] Add FieldDefinition to ResolveInfo --- src/Executor/ReferenceExecutor.php | 2 +- src/Type/Definition/ResolveInfo.php | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 057a69dbf..854281ba0 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -532,7 +532,7 @@ private function resolveField(ObjectType $parentType, $rootValue, $fieldNodes, $ $info = new ResolveInfo( $fieldName, $fieldNodes, - $returnType, + $fieldDef, $parentType, $path, $exeContext->schema, diff --git a/src/Type/Definition/ResolveInfo.php b/src/Type/Definition/ResolveInfo.php index 0f453e03b..95bab5e75 100644 --- a/src/Type/Definition/ResolveInfo.php +++ b/src/Type/Definition/ResolveInfo.php @@ -36,6 +36,14 @@ class ResolveInfo */ public $fieldNodes = []; + /** + * The definition of the field being resolved. + * + * @api + * @var FieldDefinition + */ + public $fieldDefinition; + /** * Expected return type of the field being resolved. * @@ -105,7 +113,6 @@ class ResolveInfo /** * @param FieldNode[] $fieldNodes - * @param ScalarType|ObjectType|InterfaceType|UnionType|EnumType|ListOfType|NonNull $returnType * @param string[][] $path * @param FragmentDefinitionNode[] $fragments * @param mixed|null $rootValue @@ -114,7 +121,7 @@ class ResolveInfo public function __construct( string $fieldName, iterable $fieldNodes, - $returnType, + FieldDefinition $fieldDefinition, ObjectType $parentType, array $path, Schema $schema, @@ -125,7 +132,8 @@ public function __construct( ) { $this->fieldName = $fieldName; $this->fieldNodes = $fieldNodes; - $this->returnType = $returnType; + $this->fieldDefinition = $fieldDefinition; + $this->returnType = $fieldDefinition->getType(); $this->parentType = $parentType; $this->path = $path; $this->schema = $schema; From 7f97ee43e83c08e1bead0b7cdbafb3ede4bf0569 Mon Sep 17 00:00:00 2001 From: spawnia Date: Wed, 24 Jul 2019 20:35:26 +0200 Subject: [PATCH 2/4] Simplify and reorder constructor --- src/Executor/ReferenceExecutor.php | 3 +- .../Executor/CoroutineExecutor.php | 3 +- src/Type/Definition/ResolveInfo.php | 57 +++++++++---------- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 854281ba0..6d828459e 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -530,9 +530,8 @@ private function resolveField(ObjectType $parentType, $rootValue, $fieldNodes, $ // The resolve function's optional 4th argument is a collection of // information about the current execution state. $info = new ResolveInfo( - $fieldName, - $fieldNodes, $fieldDef, + $fieldNodes, $parentType, $path, $exeContext->schema, diff --git a/src/Experimental/Executor/CoroutineExecutor.php b/src/Experimental/Executor/CoroutineExecutor.php index f823aeae1..b9d2c6f3f 100644 --- a/src/Experimental/Executor/CoroutineExecutor.php +++ b/src/Experimental/Executor/CoroutineExecutor.php @@ -396,9 +396,8 @@ private function spawn(CoroutineContext $ctx) $returnType = $fieldDefinition->getType(); $ctx->resolveInfo = new ResolveInfo( - $ctx->shared->fieldName, + $fieldDefinition, $ctx->shared->fieldNodes, - $returnType, $ctx->type, $ctx->path, $this->schema, diff --git a/src/Type/Definition/ResolveInfo.php b/src/Type/Definition/ResolveInfo.php index 95bab5e75..6e6287164 100644 --- a/src/Type/Definition/ResolveInfo.php +++ b/src/Type/Definition/ResolveInfo.php @@ -21,36 +21,36 @@ class ResolveInfo { /** - * The name of the field being resolved. + * The definition of the field being resolved. * * @api - * @var string + * @var FieldDefinition */ - public $fieldName; + public $fieldDefinition; /** - * AST of all nodes referencing this field in the query. + * The name of the field being resolved. * * @api - * @var FieldNode[] + * @var string */ - public $fieldNodes = []; + public $fieldName; /** - * The definition of the field being resolved. + * Expected return type of the field being resolved. * * @api - * @var FieldDefinition + * @var ScalarType|ObjectType|InterfaceType|UnionType|EnumType|ListOfType|NonNull */ - public $fieldDefinition; + public $returnType; /** - * Expected return type of the field being resolved. + * AST of all nodes referencing this field in the query. * * @api - * @var ScalarType|ObjectType|InterfaceType|UnionType|EnumType|ListOfType|NonNull + * @var FieldNode[] */ - public $returnType; + public $fieldNodes = []; /** * Parent type of the field being resolved. @@ -112,16 +112,15 @@ class ResolveInfo private $queryPlan; /** - * @param FieldNode[] $fieldNodes - * @param string[][] $path - * @param FragmentDefinitionNode[] $fragments - * @param mixed|null $rootValue - * @param mixed[] $variableValues + * @param FieldNode[] $fieldNodes + * @param string[][] $path + * @param FragmentDefinitionNode[] $fragments + * @param mixed|null $rootValue + * @param mixed[] $variableValues */ public function __construct( - string $fieldName, - iterable $fieldNodes, FieldDefinition $fieldDefinition, + iterable $fieldNodes, ObjectType $parentType, array $path, Schema $schema, @@ -130,17 +129,17 @@ public function __construct( ?OperationDefinitionNode $operation, array $variableValues ) { - $this->fieldName = $fieldName; - $this->fieldNodes = $fieldNodes; $this->fieldDefinition = $fieldDefinition; - $this->returnType = $fieldDefinition->getType(); - $this->parentType = $parentType; - $this->path = $path; - $this->schema = $schema; - $this->fragments = $fragments; - $this->rootValue = $rootValue; - $this->operation = $operation; - $this->variableValues = $variableValues; + $this->fieldName = $fieldDefinition->name; + $this->returnType = $fieldDefinition->getType(); + $this->fieldNodes = $fieldNodes; + $this->parentType = $parentType; + $this->path = $path; + $this->schema = $schema; + $this->fragments = $fragments; + $this->rootValue = $rootValue; + $this->operation = $operation; + $this->variableValues = $variableValues; } /** From 785961ee1d4b80c5b3e71172f84ad7cdeed88700 Mon Sep 17 00:00:00 2001 From: spawnia Date: Wed, 11 Sep 2019 19:20:31 +0200 Subject: [PATCH 3/4] Ensure fieldDefinition is set --- tests/Type/ResolveInfoTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/Type/ResolveInfoTest.php b/tests/Type/ResolveInfoTest.php index d05a44c35..c7b802261 100644 --- a/tests/Type/ResolveInfoTest.php +++ b/tests/Type/ResolveInfoTest.php @@ -5,6 +5,7 @@ namespace GraphQL\Tests\Type; use GraphQL\GraphQL; +use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; @@ -378,4 +379,32 @@ public function testMergedFragmentsFieldSelection() : void self::assertEquals(['data' => ['article' => null]], $result); self::assertEquals($expectedDeepSelection, $actualDeepSelection); } + + public function testFieldDefinition() : void + { + $query = ' + query Ping { + ping + } + '; + + $pingPongQuery = new ObjectType([ + 'name' => 'Query', + 'fields' => [ + 'ping' => [ + 'type' => Type::string(), + 'resolve' => static function ($value, $args, $context, ResolveInfo $info) : string { + self::assertInstanceOf(FieldDefinition::class, $info->fieldDefinition); + + return 'pong'; + }, + ], + ], + ]); + + $schema = new Schema(['query' => $pingPongQuery]); + $result = GraphQL::executeQuery($schema, $query)->toArray(); + + self::assertEquals(['data' => ['ping' => 'pong']], $result); + } } From 2f0c8f4f68cf2b1d6b55bff0a2d3ea5180b93305 Mon Sep 17 00:00:00 2001 From: spawnia Date: Wed, 11 Sep 2019 19:22:55 +0200 Subject: [PATCH 4/4] Move test --- tests/Executor/ExecutorTest.php | 2 ++ tests/Type/ResolveInfoTest.php | 28 ---------------------------- 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/tests/Executor/ExecutorTest.php b/tests/Executor/ExecutorTest.php index eb8e4a12e..77ef6e938 100644 --- a/tests/Executor/ExecutorTest.php +++ b/tests/Executor/ExecutorTest.php @@ -12,6 +12,7 @@ use GraphQL\Tests\Executor\TestClasses\NotSpecial; use GraphQL\Tests\Executor\TestClasses\Special; use GraphQL\Type\Definition\EnumType; +use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; @@ -295,6 +296,7 @@ public function testProvidesInfoAboutCurrentExecutionState() : void self::assertSame($rootValue, $info->rootValue); self::assertEquals($ast->definitions[0], $info->operation); self::assertEquals(['var' => '123'], $info->variableValues); + self::assertInstanceOf(FieldDefinition::class, $info->fieldDefinition); } /** diff --git a/tests/Type/ResolveInfoTest.php b/tests/Type/ResolveInfoTest.php index c7b802261..8b7e93065 100644 --- a/tests/Type/ResolveInfoTest.php +++ b/tests/Type/ResolveInfoTest.php @@ -379,32 +379,4 @@ public function testMergedFragmentsFieldSelection() : void self::assertEquals(['data' => ['article' => null]], $result); self::assertEquals($expectedDeepSelection, $actualDeepSelection); } - - public function testFieldDefinition() : void - { - $query = ' - query Ping { - ping - } - '; - - $pingPongQuery = new ObjectType([ - 'name' => 'Query', - 'fields' => [ - 'ping' => [ - 'type' => Type::string(), - 'resolve' => static function ($value, $args, $context, ResolveInfo $info) : string { - self::assertInstanceOf(FieldDefinition::class, $info->fieldDefinition); - - return 'pong'; - }, - ], - ], - ]); - - $schema = new Schema(['query' => $pingPongQuery]); - $result = GraphQL::executeQuery($schema, $query)->toArray(); - - self::assertEquals(['data' => ['ping' => 'pong']], $result); - } }