From 03c33c9dc22cc95e605298876eeab931ae3aeb4b Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 17:30:57 +0200 Subject: [PATCH 1/8] Consistently name the $rootValue argument --- docs/data-fetching.md | 18 ++++++++--------- docs/reference.md | 2 +- src/Executor/ExecutionContext.php | 4 ++-- src/Executor/Executor.php | 20 +++++++++---------- src/Executor/ReferenceExecutor.php | 31 +++++++++++++++--------------- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/docs/data-fetching.md b/docs/data-fetching.md index 4bc0cfb..346cd9a 100644 --- a/docs/data-fetching.md +++ b/docs/data-fetching.md @@ -103,22 +103,22 @@ for a field you simply override this default resolver. **graphql-php** provides following default field resolver: ```php fieldName; - $property = null; + $property = null; - if (is_array($source) || $source instanceof \ArrayAccess) { - if (isset($source[$fieldName])) { - $property = $source[$fieldName]; + if (is_array($rootValue) || $rootValue instanceof ArrayAccess) { + if (isset($rootValue[$fieldName])) { + $property = $rootValue[$fieldName]; } - } else if (is_object($source)) { - if (isset($source->{$fieldName})) { - $property = $source->{$fieldName}; + } elseif (is_object($rootValue)) { + if (isset($rootValue->{$fieldName})) { + $property = $rootValue->{$fieldName}; } } - return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; + return $property instanceof Closure ? $property($rootValue, $args, $context, $info) : $property; } ``` diff --git a/docs/reference.md b/docs/reference.md index 7ea7709..c9f4a11 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -33,7 +33,7 @@ See [related documentation](executing-queries.md). * fieldResolver: * A resolver function to use when one is not provided by the schema. * If not provided, the default field resolver is used (which looks for a - * value on the source value with the field's name). + * value on the root value with the field's name). * validationRules: * A set of rules for query validation step. Default value is all available rules. * Empty array would allow to skip query validation (may be convenient for persisted diff --git a/src/Executor/ExecutionContext.php b/src/Executor/ExecutionContext.php index 3de1a2e..6bd7318 100644 --- a/src/Executor/ExecutionContext.php +++ b/src/Executor/ExecutionContext.php @@ -50,7 +50,7 @@ class ExecutionContext public function __construct( $schema, $fragments, - $root, + $rootValue, $contextValue, $operation, $variables, @@ -60,7 +60,7 @@ class ExecutionContext ) { $this->schema = $schema; $this->fragments = $fragments; - $this->rootValue = $root; + $this->rootValue = $rootValue; $this->contextValue = $contextValue; $this->operation = $operation; $this->variableValues = $variables; diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 30ccad4..cc76e4c 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -157,31 +157,31 @@ class Executor /** * If a resolve function is not given, then a default resolve behavior is used - * which takes the property of the source object of the same name as the field + * which takes the property of the root value of the same name as the field * and returns it as the result, or if it's a function, returns the result * of calling that function while passing along args and context. * - * @param mixed $source + * @param mixed $rootValue * @param mixed[] $args * @param mixed|null $context * * @return mixed|null */ - public static function defaultFieldResolver($source, $args, $context, ResolveInfo $info) + public static function defaultFieldResolver($rootValue, $args, $context, ResolveInfo $info) { $fieldName = $info->fieldName; $property = null; - if (is_array($source) || $source instanceof ArrayAccess) { - if (isset($source[$fieldName])) { - $property = $source[$fieldName]; + if (is_array($rootValue) || $rootValue instanceof ArrayAccess) { + if (isset($rootValue[$fieldName])) { + $property = $rootValue[$fieldName]; } - } elseif (is_object($source)) { - if (isset($source->{$fieldName})) { - $property = $source->{$fieldName}; + } elseif (is_object($rootValue)) { + if (isset($rootValue->{$fieldName})) { + $property = $rootValue->{$fieldName}; } } - return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; + return $property instanceof Closure ? $property($rootValue, $args, $context, $info) : $property; } } diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 6e35f7c..6c4a00c 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -237,7 +237,7 @@ class ReferenceExecutor implements ExecutorImplementation /** * Implements the "Evaluating operations" section of the spec. * - * @param mixed[] $rootValue + * @param mixed $rootValue * * @return Promise|stdClass|mixed[] */ @@ -463,21 +463,21 @@ class ReferenceExecutor implements ExecutorImplementation * Implements the "Evaluating selection sets" section of the spec * for "write" mode. * - * @param mixed[] $sourceValue + * @param mixed $rootValue * @param mixed[] $path * @param ArrayObject $fields * * @return Promise|stdClass|mixed[] */ - private function executeFieldsSerially(ObjectType $parentType, $sourceValue, $path, $fields) + private function executeFieldsSerially(ObjectType $parentType, $rootValue, $path, $fields) { $result = $this->promiseReduce( array_keys($fields->getArrayCopy()), - function ($results, $responseName) use ($path, $parentType, $sourceValue, $fields) { + function ($results, $responseName) use ($path, $parentType, $rootValue, $fields) { $fieldNodes = $fields[$responseName]; $fieldPath = $path; $fieldPath[] = $responseName; - $result = $this->resolveField($parentType, $sourceValue, $fieldNodes, $fieldPath); + $result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath); if ($result === self::$UNDEFINED) { return $results; } @@ -505,18 +505,19 @@ class ReferenceExecutor implements ExecutorImplementation } /** - * Resolves the field on the given source object. In particular, this - * figures out the value that the field returns by calling its resolve function, - * then calls completeValue to complete promises, serialize scalars, or execute - * the sub-selection-set for objects. + * Resolves the field on the given root value. * - * @param object|null $source + * In particular, this figures out the value that the field returns + * by calling its resolve function, then calls completeValue to complete promises, + * serialize scalars, or execute the sub-selection-set for objects. + * + * @param mixed $rootValue * @param FieldNode[] $fieldNodes * @param mixed[] $path * * @return mixed[]|Exception|mixed|null */ - private function resolveField(ObjectType $parentType, $source, $fieldNodes, $path) + private function resolveField(ObjectType $parentType, $rootValue, $fieldNodes, $path) { $exeContext = $this->exeContext; $fieldNode = $fieldNodes[0]; @@ -557,7 +558,7 @@ class ReferenceExecutor implements ExecutorImplementation $fieldDef, $fieldNode, $resolveFn, - $source, + $rootValue, $context, $info ); @@ -614,13 +615,13 @@ class ReferenceExecutor implements ExecutorImplementation * @param FieldDefinition $fieldDef * @param FieldNode $fieldNode * @param callable $resolveFn - * @param mixed $source + * @param mixed $rootValue * @param mixed $context * @param ResolveInfo $info * * @return Throwable|Promise|mixed */ - private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $source, $context, $info) + private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $rootValue, $context, $info) { try { // Build hash of arguments from the field.arguments AST, using the @@ -631,7 +632,7 @@ class ReferenceExecutor implements ExecutorImplementation $this->exeContext->variableValues ); - return $resolveFn($source, $args, $context, $info); + return $resolveFn($rootValue, $args, $context, $info); } catch (Exception $error) { return $error; } catch (Throwable $error) { From 65a3a8d13efa75a9cb732abfc2d970a22f4a50ef Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 18:36:07 +0200 Subject: [PATCH 2/8] Add missing renaming in ReferenceExecutor --- src/Executor/ReferenceExecutor.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 6c4a00c..82824c4 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -1235,20 +1235,20 @@ class ReferenceExecutor implements ExecutorImplementation * Implements the "Evaluating selection sets" section of the spec * for "read" mode. * - * @param mixed|null $source + * @param mixed $rootValue * @param mixed[] $path * @param ArrayObject $fields * * @return Promise|stdClass|mixed[] */ - private function executeFields(ObjectType $parentType, $source, $path, $fields) + private function executeFields(ObjectType $parentType, $rootValue, $path, $fields) { $containsPromise = false; $finalResults = []; foreach ($fields as $responseName => $fieldNodes) { $fieldPath = $path; $fieldPath[] = $responseName; - $result = $this->resolveField($parentType, $source, $fieldNodes, $fieldPath); + $result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath); if ($result === self::$UNDEFINED) { continue; } From 8da3043702ddcfaa097d16b782da465a012be150 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 21:26:38 +0200 Subject: [PATCH 3/8] Rename some test variable names --- docs/type-system/enum-types.md | 2 +- tests/Executor/DeferredFieldsTest.php | 4 ++-- tests/Executor/ExecutorSchemaTest.php | 2 +- tests/Executor/ExecutorTest.php | 2 +- tests/Executor/TestClasses/Adder.php | 2 +- tests/GraphQLTest.php | 2 +- tests/Type/EnumTypeTest.php | 12 ++++++------ tests/Type/IntrospectionTest.php | 2 +- tests/Type/QueryPlanTest.php | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/type-system/enum-types.md b/docs/type-system/enum-types.md index 4cebdec..6170c0c 100644 --- a/docs/type-system/enum-types.md +++ b/docs/type-system/enum-types.md @@ -158,7 +158,7 @@ $heroType = new ObjectType([ 'args' => [ 'episode' => Type::nonNull($enumType) ], - 'resolve' => function($_value, $args) { + 'resolve' => function($root, $args) { return $args['episode'] === 5 ? true : false; } ] diff --git a/tests/Executor/DeferredFieldsTest.php b/tests/Executor/DeferredFieldsTest.php index e68388c..a4021d2 100644 --- a/tests/Executor/DeferredFieldsTest.php +++ b/tests/Executor/DeferredFieldsTest.php @@ -109,10 +109,10 @@ class DeferredFieldsTest extends TestCase 'fields' => [ 'title' => [ 'type' => Type::string(), - 'resolve' => function ($entry, $args, $context, ResolveInfo $info) { + 'resolve' => function ($story, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; - return $entry['title']; + return $story['title']; }, ], 'author' => [ diff --git a/tests/Executor/ExecutorSchemaTest.php b/tests/Executor/ExecutorSchemaTest.php index 4946c59..2a0fd99 100644 --- a/tests/Executor/ExecutorSchemaTest.php +++ b/tests/Executor/ExecutorSchemaTest.php @@ -75,7 +75,7 @@ class ExecutorSchemaTest extends TestCase 'article' => [ 'type' => $BlogArticle, 'args' => ['id' => ['type' => Type::id()]], - 'resolve' => function ($_, $args) { + 'resolve' => function ($root, $args) { return $this->article($args['id']); }, ], diff --git a/tests/Executor/ExecutorTest.php b/tests/Executor/ExecutorTest.php index 9f5480f..7e7082b 100644 --- a/tests/Executor/ExecutorTest.php +++ b/tests/Executor/ExecutorTest.php @@ -273,7 +273,7 @@ class ExecutorTest extends TestCase 'fields' => [ 'test' => [ 'type' => Type::string(), - 'resolve' => static function ($val, $args, $ctx, $_info) use (&$info) { + 'resolve' => static function ($root, $args, $ctx, $_info) use (&$info) { $info = $_info; }, ], diff --git a/tests/Executor/TestClasses/Adder.php b/tests/Executor/TestClasses/Adder.php index b086b51..804617b 100644 --- a/tests/Executor/TestClasses/Adder.php +++ b/tests/Executor/TestClasses/Adder.php @@ -16,7 +16,7 @@ class Adder { $this->num = $num; - $this->test = function ($source, $args, $context) { + $this->test = function ($root, $args, $context) { return $this->num + $args['addend1'] + $context['addend2']; }; } diff --git a/tests/GraphQLTest.php b/tests/GraphQLTest.php index 13193b5..15da589 100644 --- a/tests/GraphQLTest.php +++ b/tests/GraphQLTest.php @@ -30,7 +30,7 @@ class GraphQLTest extends TestCase 'type' => Type::nonNull(Type::string()), ], ], - 'resolve' => static function ($value, $args) use ($promiseAdapter) { + 'resolve' => static function ($root, $args) use ($promiseAdapter) { return $promiseAdapter->createFulfilled(sprintf('Hi %s!', $args['name'])); }, ], diff --git a/tests/Type/EnumTypeTest.php b/tests/Type/EnumTypeTest.php index 9b2e8e6..8e579de 100644 --- a/tests/Type/EnumTypeTest.php +++ b/tests/Type/EnumTypeTest.php @@ -74,7 +74,7 @@ class EnumTypeTest extends TestCase 'fromInt' => ['type' => Type::int()], 'fromString' => ['type' => Type::string()], ], - 'resolve' => static function ($value, $args) { + 'resolve' => static function ($root, $args) { if (isset($args['fromInt'])) { return $args['fromInt']; } @@ -92,7 +92,7 @@ class EnumTypeTest extends TestCase 'fromName' => ['type' => Type::string()], 'fromValue' => ['type' => Type::string()], ], - 'resolve' => static function ($value, $args) { + 'resolve' => static function ($root, $args) { if (isset($args['fromName'])) { return $args['fromName']; } @@ -107,7 +107,7 @@ class EnumTypeTest extends TestCase 'fromEnum' => ['type' => $ColorType], 'fromInt' => ['type' => Type::int()], ], - 'resolve' => static function ($value, $args) { + 'resolve' => static function ($root, $args) { if (isset($args['fromInt'])) { return $args['fromInt']; } @@ -132,7 +132,7 @@ class EnumTypeTest extends TestCase 'type' => Type::boolean(), ], ], - 'resolve' => static function ($value, $args) use ($Complex2) { + 'resolve' => static function ($root, $args) use ($Complex2) { if (! empty($args['provideGoodValue'])) { // Note: this is one of the references of the internal values which // ComplexEnum allows. @@ -156,7 +156,7 @@ class EnumTypeTest extends TestCase 'favoriteEnum' => [ 'type' => $ColorType, 'args' => ['color' => ['type' => $ColorType]], - 'resolve' => static function ($value, $args) { + 'resolve' => static function ($root, $args) { return $args['color'] ?? null; }, ], @@ -169,7 +169,7 @@ class EnumTypeTest extends TestCase 'subscribeToEnum' => [ 'type' => $ColorType, 'args' => ['color' => ['type' => $ColorType]], - 'resolve' => static function ($value, $args) { + 'resolve' => static function ($root, $args) { return $args['color'] ?? null; }, ], diff --git a/tests/Type/IntrospectionTest.php b/tests/Type/IntrospectionTest.php index 51feb40..d92bba0 100644 --- a/tests/Type/IntrospectionTest.php +++ b/tests/Type/IntrospectionTest.php @@ -1049,7 +1049,7 @@ class IntrospectionTest extends TestCase 'field' => [ 'type' => Type::string(), 'args' => ['complex' => ['type' => $TestInputObject]], - 'resolve' => static function ($_, $args) { + 'resolve' => static function ($root, $args) { return json_encode($args['complex']); }, ], diff --git a/tests/Type/QueryPlanTest.php b/tests/Type/QueryPlanTest.php index 73f8ae7..a0d05fd 100644 --- a/tests/Type/QueryPlanTest.php +++ b/tests/Type/QueryPlanTest.php @@ -393,7 +393,7 @@ final class QueryPlanTest extends TestCase } }, ]); - $result = GraphQL::executeQuery($schema, $query)->toArray(); + GraphQL::executeQuery($schema, $query)->toArray(); self::assertTrue($hasCalled); self::assertEquals($expectedQueryPlan, $queryPlan->queryPlan()); From 24f236403a7ffa653a560e47edfd10f3469dcba6 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 30 Jun 2019 20:54:56 +0200 Subject: [PATCH 4/8] More consistent naming --- docs/getting-started.md | 4 ++-- docs/reference.md | 2 +- docs/type-system/enum-types.md | 2 +- examples/00-hello-world/graphql.php | 6 +++--- examples/02-shorthand/rootvalue.php | 16 ++++++++-------- examples/03-server/graphql.php | 6 +++--- src/Server/Helper.php | 8 ++++---- tests/Executor/ExecutorSchemaTest.php | 2 +- tests/Executor/ExecutorTest.php | 2 +- tests/Executor/TestClasses/Adder.php | 2 +- tests/GraphQLTest.php | 2 +- tests/Regression/Issue396Test.php | 4 ++-- tests/Server/ServerTestCase.php | 12 ++++++------ tests/StarWarsSchema.php | 6 +++--- tests/Type/EnumTypeTest.php | 12 ++++++------ tests/Type/IntrospectionTest.php | 2 +- tests/Utils/BuildSchemaTest.php | 22 +++++++++++----------- 17 files changed, 55 insertions(+), 55 deletions(-) diff --git a/docs/getting-started.md b/docs/getting-started.md index 4477cda..69abf8e 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -54,8 +54,8 @@ $queryType = new ObjectType([ 'args' => [ 'message' => Type::nonNull(Type::string()), ], - 'resolve' => function ($root, $args) { - return $root['prefix'] . $args['message']; + 'resolve' => function ($rootValue, $args) { + return $rootValue['prefix'] . $args['message']; } ], ], diff --git a/docs/reference.md b/docs/reference.md index c9f4a11..b579179 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -998,7 +998,7 @@ visitor API: * * @api */ -static function visit($root, $visitor, $keyMap = null) +static function visit($rootValue, $visitor, $keyMap = null) ``` ```php diff --git a/docs/type-system/enum-types.md b/docs/type-system/enum-types.md index 6170c0c..ea62c2b 100644 --- a/docs/type-system/enum-types.md +++ b/docs/type-system/enum-types.md @@ -158,7 +158,7 @@ $heroType = new ObjectType([ 'args' => [ 'episode' => Type::nonNull($enumType) ], - 'resolve' => function($root, $args) { + 'resolve' => function($rootValue, $args) { return $args['episode'] === 5 ? true : false; } ] diff --git a/examples/00-hello-world/graphql.php b/examples/00-hello-world/graphql.php index 30b1f9c..167b44d 100644 --- a/examples/00-hello-world/graphql.php +++ b/examples/00-hello-world/graphql.php @@ -19,8 +19,8 @@ try { 'args' => [ 'message' => ['type' => Type::string()], ], - 'resolve' => function ($root, $args) { - return $root['prefix'] . $args['message']; + 'resolve' => function ($rootValue, $args) { + return $rootValue['prefix'] . $args['message']; } ], ], @@ -35,7 +35,7 @@ try { 'x' => ['type' => Type::int()], 'y' => ['type' => Type::int()], ], - 'resolve' => function ($root, $args) { + 'resolve' => function ($rootValue, $args) { return $args['x'] + $args['y']; }, ], diff --git a/examples/02-shorthand/rootvalue.php b/examples/02-shorthand/rootvalue.php index 97e0a82..c16d62b 100644 --- a/examples/02-shorthand/rootvalue.php +++ b/examples/02-shorthand/rootvalue.php @@ -1,12 +1,12 @@ function($root, $args, $context) { + 'sum' => function($rootValue, $args, $context) { $sum = new Addition(); - return $sum->resolve($root, $args, $context); + return $sum->resolve($rootValue, $args, $context); }, - 'echo' => function($root, $args, $context) { + 'echo' => function($rootValue, $args, $context) { $echo = new Echoer(); - return $echo->resolve($root, $args, $context); + return $echo->resolve($rootValue, $args, $context); }, 'prefix' => 'You said: ', ]; diff --git a/examples/03-server/graphql.php b/examples/03-server/graphql.php index 0c01195..8b73f44 100644 --- a/examples/03-server/graphql.php +++ b/examples/03-server/graphql.php @@ -19,8 +19,8 @@ try { 'args' => [ 'message' => ['type' => Type::string()], ], - 'resolve' => function ($root, $args) { - return $root['prefix'] . $args['message']; + 'resolve' => function ($rootValue, $args) { + return $rootValue['prefix'] . $args['message']; } ], ], @@ -35,7 +35,7 @@ try { 'x' => ['type' => Type::int()], 'y' => ['type' => Type::int()], ], - 'resolve' => function ($root, $args) { + 'resolve' => function ($rootValue, $args) { return $args['x'] + $args['y']; }, ], diff --git a/src/Server/Helper.php b/src/Server/Helper.php index ff62b2b..9c522c0 100644 --- a/src/Server/Helper.php +++ b/src/Server/Helper.php @@ -385,13 +385,13 @@ class Helper */ private function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $doc, $operationType) { - $root = $config->getRootValue(); + $rootValue = $config->getRootValue(); - if (is_callable($root)) { - $root = $root($params, $doc, $operationType); + if (is_callable($rootValue)) { + $rootValue = $rootValue($params, $doc, $operationType); } - return $root; + return $rootValue; } /** diff --git a/tests/Executor/ExecutorSchemaTest.php b/tests/Executor/ExecutorSchemaTest.php index 2a0fd99..ae5a3c1 100644 --- a/tests/Executor/ExecutorSchemaTest.php +++ b/tests/Executor/ExecutorSchemaTest.php @@ -75,7 +75,7 @@ class ExecutorSchemaTest extends TestCase 'article' => [ 'type' => $BlogArticle, 'args' => ['id' => ['type' => Type::id()]], - 'resolve' => function ($root, $args) { + 'resolve' => function ($rootValue, $args) { return $this->article($args['id']); }, ], diff --git a/tests/Executor/ExecutorTest.php b/tests/Executor/ExecutorTest.php index 7e7082b..76b4beb 100644 --- a/tests/Executor/ExecutorTest.php +++ b/tests/Executor/ExecutorTest.php @@ -273,7 +273,7 @@ class ExecutorTest extends TestCase 'fields' => [ 'test' => [ 'type' => Type::string(), - 'resolve' => static function ($root, $args, $ctx, $_info) use (&$info) { + 'resolve' => static function ($rootValue, $args, $ctx, $_info) use (&$info) { $info = $_info; }, ], diff --git a/tests/Executor/TestClasses/Adder.php b/tests/Executor/TestClasses/Adder.php index 804617b..416cd5e 100644 --- a/tests/Executor/TestClasses/Adder.php +++ b/tests/Executor/TestClasses/Adder.php @@ -16,7 +16,7 @@ class Adder { $this->num = $num; - $this->test = function ($root, $args, $context) { + $this->test = function ($rootValue, $args, $context) { return $this->num + $args['addend1'] + $context['addend2']; }; } diff --git a/tests/GraphQLTest.php b/tests/GraphQLTest.php index 15da589..6d28e09 100644 --- a/tests/GraphQLTest.php +++ b/tests/GraphQLTest.php @@ -30,7 +30,7 @@ class GraphQLTest extends TestCase 'type' => Type::nonNull(Type::string()), ], ], - 'resolve' => static function ($root, $args) use ($promiseAdapter) { + 'resolve' => static function ($rootValue, $args) use ($promiseAdapter) { return $promiseAdapter->createFulfilled(sprintf('Hi %s!', $args['name'])); }, ], diff --git a/tests/Regression/Issue396Test.php b/tests/Regression/Issue396Test.php index 41d0b79..f7c26a9 100644 --- a/tests/Regression/Issue396Test.php +++ b/tests/Regression/Issue396Test.php @@ -30,7 +30,7 @@ class Issue396Test extends TestCase $unionResult = new UnionType([ 'name' => 'UnionResult', 'types' => [$a, $b, $c], - 'resolveType' => static function ($result, $root, ResolveInfo $info) use ($a, $b, $c, &$log) : Type { + 'resolveType' => static function ($result, $rootValue, ResolveInfo $info) use ($a, $b, $c, &$log) : Type { $log[] = [$result, $info->path]; if (stristr($result['name'], 'A')) { return $a; @@ -97,7 +97,7 @@ class Issue396Test extends TestCase 'fields' => [ 'name' => Type::string(), ], - 'resolveType' => static function ($result, $root, ResolveInfo $info) use (&$a, &$b, &$c, &$log) : Type { + 'resolveType' => static function ($result, $rootValue, ResolveInfo $info) use (&$a, &$b, &$c, &$log) : Type { $log[] = [$result, $info->path]; if (stristr($result['name'], 'A')) { return $a; diff --git a/tests/Server/ServerTestCase.php b/tests/Server/ServerTestCase.php index 072acba..d94ac34 100644 --- a/tests/Server/ServerTestCase.php +++ b/tests/Server/ServerTestCase.php @@ -25,13 +25,13 @@ abstract class ServerTestCase extends TestCase 'fields' => [ 'f1' => [ 'type' => Type::string(), - 'resolve' => static function ($root, $args, $context, $info) { + 'resolve' => static function ($rootValue, $args, $context, $info) { return $info->fieldName; }, ], 'fieldWithPhpError' => [ 'type' => Type::string(), - 'resolve' => static function ($root, $args, $context, $info) { + 'resolve' => static function ($rootValue, $args, $context, $info) { trigger_error('deprecated', E_USER_DEPRECATED); trigger_error('notice', E_USER_NOTICE); trigger_error('warning', E_USER_WARNING); @@ -55,8 +55,8 @@ abstract class ServerTestCase extends TestCase ], 'testContextAndRootValue' => [ 'type' => Type::string(), - 'resolve' => static function ($root, $args, $context, $info) { - $context->testedRootValue = $root; + 'resolve' => static function ($rootValue, $args, $context, $info) { + $context->testedRootValue = $rootValue; return $info->fieldName; }, @@ -68,7 +68,7 @@ abstract class ServerTestCase extends TestCase 'type' => Type::nonNull(Type::string()), ], ], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { return $args['arg']; }, ], @@ -79,7 +79,7 @@ abstract class ServerTestCase extends TestCase 'type' => Type::nonNull(Type::int()), ], ], - 'resolve' => static function ($root, $args, $context) { + 'resolve' => static function ($rootValue, $args, $context) { $context['buffer']($args['num']); return new Deferred(static function () use ($args, $context) { diff --git a/tests/StarWarsSchema.php b/tests/StarWarsSchema.php index b9240af..86221e6 100644 --- a/tests/StarWarsSchema.php +++ b/tests/StarWarsSchema.php @@ -273,7 +273,7 @@ class StarWarsSchema 'type' => $episodeEnum, ], ], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { return StarWarsData::getHero($args['episode'] ?? null); }, ], @@ -286,7 +286,7 @@ class StarWarsSchema 'type' => Type::nonNull(Type::string()), ], ], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { $humans = StarWarsData::humans(); return $humans[$args['id']] ?? null; @@ -301,7 +301,7 @@ class StarWarsSchema 'type' => Type::nonNull(Type::string()), ], ], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { $droids = StarWarsData::droids(); return $droids[$args['id']] ?? null; diff --git a/tests/Type/EnumTypeTest.php b/tests/Type/EnumTypeTest.php index 8e579de..b1649be 100644 --- a/tests/Type/EnumTypeTest.php +++ b/tests/Type/EnumTypeTest.php @@ -74,7 +74,7 @@ class EnumTypeTest extends TestCase 'fromInt' => ['type' => Type::int()], 'fromString' => ['type' => Type::string()], ], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { if (isset($args['fromInt'])) { return $args['fromInt']; } @@ -92,7 +92,7 @@ class EnumTypeTest extends TestCase 'fromName' => ['type' => Type::string()], 'fromValue' => ['type' => Type::string()], ], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { if (isset($args['fromName'])) { return $args['fromName']; } @@ -107,7 +107,7 @@ class EnumTypeTest extends TestCase 'fromEnum' => ['type' => $ColorType], 'fromInt' => ['type' => Type::int()], ], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { if (isset($args['fromInt'])) { return $args['fromInt']; } @@ -132,7 +132,7 @@ class EnumTypeTest extends TestCase 'type' => Type::boolean(), ], ], - 'resolve' => static function ($root, $args) use ($Complex2) { + 'resolve' => static function ($rootValue, $args) use ($Complex2) { if (! empty($args['provideGoodValue'])) { // Note: this is one of the references of the internal values which // ComplexEnum allows. @@ -156,7 +156,7 @@ class EnumTypeTest extends TestCase 'favoriteEnum' => [ 'type' => $ColorType, 'args' => ['color' => ['type' => $ColorType]], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { return $args['color'] ?? null; }, ], @@ -169,7 +169,7 @@ class EnumTypeTest extends TestCase 'subscribeToEnum' => [ 'type' => $ColorType, 'args' => ['color' => ['type' => $ColorType]], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { return $args['color'] ?? null; }, ], diff --git a/tests/Type/IntrospectionTest.php b/tests/Type/IntrospectionTest.php index d92bba0..afea3d9 100644 --- a/tests/Type/IntrospectionTest.php +++ b/tests/Type/IntrospectionTest.php @@ -1049,7 +1049,7 @@ class IntrospectionTest extends TestCase 'field' => [ 'type' => Type::string(), 'args' => ['complex' => ['type' => $TestInputObject]], - 'resolve' => static function ($root, $args) { + 'resolve' => static function ($rootValue, $args) { return json_encode($args['complex']); }, ], diff --git a/tests/Utils/BuildSchemaTest.php b/tests/Utils/BuildSchemaTest.php index e8e3280..0ca56c9 100644 --- a/tests/Utils/BuildSchemaTest.php +++ b/tests/Utils/BuildSchemaTest.php @@ -51,7 +51,7 @@ class BuildSchemaTest extends TestCase '); $root = [ - 'add' => static function ($root, $args) { + 'add' => static function ($rootValue, $args) { return $args['x'] + $args['y']; }, ]; @@ -437,7 +437,7 @@ type WorldTwo { */ public function testSpecifyingUnionTypeUsingTypename() : void { - $schema = BuildSchema::buildAST(Parser::parse(' + $schema = BuildSchema::buildAST(Parser::parse(' type Query { fruits: [Fruit] } @@ -452,7 +452,7 @@ type WorldTwo { length: Int } ')); - $query = ' + $query = ' { fruits { ... on Apple { @@ -464,7 +464,7 @@ type WorldTwo { } } '; - $root = [ + $rootValue = [ 'fruits' => [ [ 'color' => 'green', @@ -476,7 +476,7 @@ type WorldTwo { ], ], ]; - $expected = [ + $expected = [ 'data' => [ 'fruits' => [ ['color' => 'green'], @@ -485,7 +485,7 @@ type WorldTwo { ], ]; - $result = GraphQL::executeQuery($schema, $query, $root); + $result = GraphQL::executeQuery($schema, $query, $rootValue); self::assertEquals($expected, $result->toArray(true)); } @@ -494,7 +494,7 @@ type WorldTwo { */ public function testSpecifyingInterfaceUsingTypename() : void { - $schema = BuildSchema::buildAST(Parser::parse(' + $schema = BuildSchema::buildAST(Parser::parse(' type Query { characters: [Character] } @@ -513,7 +513,7 @@ type WorldTwo { primaryFunction: String } ')); - $query = ' + $query = ' { characters { name @@ -526,7 +526,7 @@ type WorldTwo { } } '; - $root = [ + $rootValue = [ 'characters' => [ [ 'name' => 'Han Solo', @@ -540,7 +540,7 @@ type WorldTwo { ], ], ]; - $expected = [ + $expected = [ 'data' => [ 'characters' => [ ['name' => 'Han Solo', 'totalCredits' => 10], @@ -549,7 +549,7 @@ type WorldTwo { ], ]; - $result = GraphQL::executeQuery($schema, $query, $root); + $result = GraphQL::executeQuery($schema, $query, $rootValue); self::assertEquals($expected, $result->toArray(true)); } From 19a37609f4ba53d6e7ce13ee0644b65b821fb154 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 30 Jun 2019 23:08:24 +0200 Subject: [PATCH 5/8] Add a few more --- benchmarks/Utils/SchemaGenerator.php | 2 +- docs/type-system/object-types.md | 4 ++-- examples/01-blog/Blog/Type/CommentType.php | 6 +++--- examples/01-blog/Blog/Type/StoryType.php | 6 +++--- examples/01-blog/Blog/Type/UserType.php | 6 +++--- src/Type/Definition/ObjectType.php | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/benchmarks/Utils/SchemaGenerator.php b/benchmarks/Utils/SchemaGenerator.php index 8ee39e7..495ad8f 100644 --- a/benchmarks/Utils/SchemaGenerator.php +++ b/benchmarks/Utils/SchemaGenerator.php @@ -152,7 +152,7 @@ class SchemaGenerator ]; } - public function resolveField($value, $args, $context, $resolveInfo) + public function resolveField($rootValue, $args, $context, $resolveInfo) { return $resolveInfo->fieldName . '-value'; } diff --git a/docs/type-system/object-types.md b/docs/type-system/object-types.md index 9c1f2fb..c12595a 100644 --- a/docs/type-system/object-types.md +++ b/docs/type-system/object-types.md @@ -69,7 +69,7 @@ name | `string` | **Required.** Unique name of this object type within S fields | `array` or `callable` | **Required**. An array describing object fields or callable returning such an array. See [Fields](#field-definitions) section below for expected structure of each array entry. See also the section on [Circular types](#recurring-and-circular-types) for an explanation of when to use callable for this option. description | `string` | Plain-text description of this type for clients (e.g. used by [GraphiQL](https://github.com/graphql/graphiql) for auto-generated documentation) interfaces | `array` or `callable` | List of interfaces implemented by this type or callable returning such a list. See [Interface Types](interfaces.md) for details. See also the section on [Circular types](#recurring-and-circular-types) for an explanation of when to use callable for this option. -isTypeOf | `callable` | **function($value, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Expected to return **true** if **$value** qualifies for this type (see section about [Abstract Type Resolution](interfaces.md#interface-role-in-data-fetching) for explanation). +isTypeOf | `callable` | **function($rootValue, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Expected to return **true** if **$value** qualifies for this type (see section about [Abstract Type Resolution](interfaces.md#interface-role-in-data-fetching) for explanation). resolveField | `callable` | **function($value, $args, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Given the **$value** of this type, it is expected to return value for a field defined in **$info->fieldName**. A good place to define a type-specific strategy for field resolution. See section on [Data Fetching](../data-fetching.md) for details. # Field configuration options @@ -80,7 +80,7 @@ Option | Type | Notes name | `string` | **Required.** Name of the field. When not set - inferred from **fields** array key (read about [shorthand field definition](#shorthand-field-definitions) below) type | `Type` | **Required.** An instance of internal or custom type. Note: type must be represented by a single instance within one schema (see also [Type Registry](index.md#type-registry)) args | `array` | An array of possible type arguments. Each entry is expected to be an array with keys: **name**, **type**, **description**, **defaultValue**. See [Field Arguments](#field-arguments) section below. -resolve | `callable` | **function($value, $args, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Given the **$value** of this type, it is expected to return actual value of the current field. See section on [Data Fetching](../data-fetching.md) for details +resolve | `callable` | **function($rootValue, $args, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Given the **$value** of this type, it is expected to return actual value of the current field. See section on [Data Fetching](../data-fetching.md) for details complexity | `callable` | **function($childrenComplexity, $args)**
Used to restrict query complexity. The feature is disabled by default, read about [Security](../security.md#query-complexity-analysis) to use it. description | `string` | Plain-text description of this field for clients (e.g. used by [GraphiQL](https://github.com/graphql/graphiql) for auto-generated documentation) deprecationReason | `string` | Text describing why this field is deprecated. When not empty - field will not be returned by introspection queries (unless forced) diff --git a/examples/01-blog/Blog/Type/CommentType.php b/examples/01-blog/Blog/Type/CommentType.php index cbcad4d..c61e338 100644 --- a/examples/01-blog/Blog/Type/CommentType.php +++ b/examples/01-blog/Blog/Type/CommentType.php @@ -35,12 +35,12 @@ class CommentType extends ObjectType Types::htmlField('body') ]; }, - 'resolveField' => function($value, $args, $context, ResolveInfo $info) { + 'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { $method = 'resolve' . ucfirst($info->fieldName); if (method_exists($this, $method)) { - return $this->{$method}($value, $args, $context, $info); + return $this->{$method}($rootValue, $args, $context, $info); } else { - return $value->{$info->fieldName}; + return $rootValue->{$info->fieldName}; } } ]; diff --git a/examples/01-blog/Blog/Type/StoryType.php b/examples/01-blog/Blog/Type/StoryType.php index 32cea4e..1df1f37 100644 --- a/examples/01-blog/Blog/Type/StoryType.php +++ b/examples/01-blog/Blog/Type/StoryType.php @@ -75,12 +75,12 @@ class StoryType extends ObjectType 'interfaces' => [ Types::node() ], - 'resolveField' => function($value, $args, $context, ResolveInfo $info) { + 'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { $method = 'resolve' . ucfirst($info->fieldName); if (method_exists($this, $method)) { - return $this->{$method}($value, $args, $context, $info); + return $this->{$method}($rootValue, $args, $context, $info); } else { - return $value->{$info->fieldName}; + return $rootValue->{$info->fieldName}; } } ]; diff --git a/examples/01-blog/Blog/Type/UserType.php b/examples/01-blog/Blog/Type/UserType.php index 9960b62..cb5922d 100644 --- a/examples/01-blog/Blog/Type/UserType.php +++ b/examples/01-blog/Blog/Type/UserType.php @@ -44,12 +44,12 @@ class UserType extends ObjectType 'interfaces' => [ Types::node() ], - 'resolveField' => function($value, $args, $context, ResolveInfo $info) { + 'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { $method = 'resolve' . ucfirst($info->fieldName); if (method_exists($this, $method)) { - return $this->{$method}($value, $args, $context, $info); + return $this->{$method}($rootValue, $args, $context, $info); } else { - return $value->{$info->fieldName}; + return $rootValue->{$info->fieldName}; } } ]; diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 532a9b0..d464b45 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -200,7 +200,7 @@ class ObjectType extends Type implements OutputType, CompositeType, NullableType } /** - * @param mixed[] $value + * @param mixed $value * @param mixed[]|null $context * * @return bool|null From c069d20ca751933483b056dd808f0476ed4e97c3 Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 30 Jun 2019 23:09:32 +0200 Subject: [PATCH 6/8] And a few more in tests --- docs/type-system/schema.md | 2 +- examples/01-blog/Blog/Type/QueryType.php | 4 ++-- tests/Executor/DeferredFieldsTest.php | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/type-system/schema.md b/docs/type-system/schema.md index 08ee12f..e3ae6e0 100644 --- a/docs/type-system/schema.md +++ b/docs/type-system/schema.md @@ -62,7 +62,7 @@ $mutationType = new ObjectType([ 'episode' => $episodeEnum, 'review' => $reviewInputObject ], - 'resolve' => function($val, $args) { + 'resolve' => function($rootValue, $args) { // TODOC } ] diff --git a/examples/01-blog/Blog/Type/QueryType.php b/examples/01-blog/Blog/Type/QueryType.php index ff85577..b7d8075 100644 --- a/examples/01-blog/Blog/Type/QueryType.php +++ b/examples/01-blog/Blog/Type/QueryType.php @@ -57,8 +57,8 @@ class QueryType extends ObjectType ], 'hello' => Type::string() ], - 'resolveField' => function($val, $args, $context, ResolveInfo $info) { - return $this->{$info->fieldName}($val, $args, $context, $info); + 'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { + return $this->{$info->fieldName}($rootValue, $args, $context, $info); } ]; parent::__construct($config); diff --git a/tests/Executor/DeferredFieldsTest.php b/tests/Executor/DeferredFieldsTest.php index a4021d2..6879e8d 100644 --- a/tests/Executor/DeferredFieldsTest.php +++ b/tests/Executor/DeferredFieldsTest.php @@ -185,7 +185,7 @@ class DeferredFieldsTest extends TestCase 'fields' => [ 'topStories' => [ 'type' => Type::listOf($this->storyType), - 'resolve' => function ($val, $args, $context, ResolveInfo $info) { + 'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return Utils::filter( @@ -198,7 +198,7 @@ class DeferredFieldsTest extends TestCase ], 'featuredCategory' => [ 'type' => $this->categoryType, - 'resolve' => function ($val, $args, $context, ResolveInfo $info) { + 'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return $this->categoryDataSource[0]; @@ -206,7 +206,7 @@ class DeferredFieldsTest extends TestCase ], 'categories' => [ 'type' => Type::listOf($this->categoryType), - 'resolve' => function ($val, $args, $context, ResolveInfo $info) { + 'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return $this->categoryDataSource; @@ -401,7 +401,7 @@ class DeferredFieldsTest extends TestCase return [ 'sync' => [ 'type' => Type::string(), - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($rootValue, $a, $c, ResolveInfo $info) { $this->paths[] = $info->path; return 'sync'; @@ -409,7 +409,7 @@ class DeferredFieldsTest extends TestCase ], 'deferred' => [ 'type' => Type::string(), - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($rootValue, $a, $c, ResolveInfo $info) { $this->paths[] = $info->path; return new Deferred(function () use ($info) { @@ -421,7 +421,7 @@ class DeferredFieldsTest extends TestCase ], 'nest' => [ 'type' => $complexType, - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($rootValue, $a, $c, ResolveInfo $info) { $this->paths[] = $info->path; return []; @@ -429,7 +429,7 @@ class DeferredFieldsTest extends TestCase ], 'deferredNest' => [ 'type' => $complexType, - 'resolve' => function ($v, $a, $c, ResolveInfo $info) { + 'resolve' => function ($rootValue, $a, $c, ResolveInfo $info) { $this->paths[] = $info->path; return new Deferred(function () use ($info) { From 3b33167c871429ee86a2cee8b45b4ec802ab9dd7 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 1 Jul 2019 12:12:01 +0200 Subject: [PATCH 7/8] Rename $rootValue where applicable --- benchmarks/Utils/SchemaGenerator.php | 2 +- docs/type-system/enum-types.md | 2 +- docs/type-system/object-types.md | 4 ++-- examples/00-hello-world/graphql.php | 2 +- examples/01-blog/Blog/Type/CommentType.php | 6 +++--- examples/01-blog/Blog/Type/StoryType.php | 6 +++--- examples/01-blog/Blog/Type/UserType.php | 6 +++--- examples/03-server/graphql.php | 2 +- tests/Executor/DeferredFieldsTest.php | 8 ++++---- tests/Executor/ExecutorTest.php | 2 +- tests/Executor/TestClasses/Adder.php | 2 +- tests/Regression/Issue396Test.php | 4 ++-- tests/Type/IntrospectionTest.php | 2 +- 13 files changed, 24 insertions(+), 24 deletions(-) diff --git a/benchmarks/Utils/SchemaGenerator.php b/benchmarks/Utils/SchemaGenerator.php index 495ad8f..1dce8f6 100644 --- a/benchmarks/Utils/SchemaGenerator.php +++ b/benchmarks/Utils/SchemaGenerator.php @@ -152,7 +152,7 @@ class SchemaGenerator ]; } - public function resolveField($rootValue, $args, $context, $resolveInfo) + public function resolveField($objectValue, $args, $context, $resolveInfo) { return $resolveInfo->fieldName . '-value'; } diff --git a/docs/type-system/enum-types.md b/docs/type-system/enum-types.md index ea62c2b..bfab3a5 100644 --- a/docs/type-system/enum-types.md +++ b/docs/type-system/enum-types.md @@ -158,7 +158,7 @@ $heroType = new ObjectType([ 'args' => [ 'episode' => Type::nonNull($enumType) ], - 'resolve' => function($rootValue, $args) { + 'resolve' => function($hero, $args) { return $args['episode'] === 5 ? true : false; } ] diff --git a/docs/type-system/object-types.md b/docs/type-system/object-types.md index c12595a..0b950c0 100644 --- a/docs/type-system/object-types.md +++ b/docs/type-system/object-types.md @@ -69,7 +69,7 @@ name | `string` | **Required.** Unique name of this object type within S fields | `array` or `callable` | **Required**. An array describing object fields or callable returning such an array. See [Fields](#field-definitions) section below for expected structure of each array entry. See also the section on [Circular types](#recurring-and-circular-types) for an explanation of when to use callable for this option. description | `string` | Plain-text description of this type for clients (e.g. used by [GraphiQL](https://github.com/graphql/graphiql) for auto-generated documentation) interfaces | `array` or `callable` | List of interfaces implemented by this type or callable returning such a list. See [Interface Types](interfaces.md) for details. See also the section on [Circular types](#recurring-and-circular-types) for an explanation of when to use callable for this option. -isTypeOf | `callable` | **function($rootValue, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Expected to return **true** if **$value** qualifies for this type (see section about [Abstract Type Resolution](interfaces.md#interface-role-in-data-fetching) for explanation). +isTypeOf | `callable` | **function($value, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Expected to return **true** if **$value** qualifies for this type (see section about [Abstract Type Resolution](interfaces.md#interface-role-in-data-fetching) for explanation). resolveField | `callable` | **function($value, $args, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Given the **$value** of this type, it is expected to return value for a field defined in **$info->fieldName**. A good place to define a type-specific strategy for field resolution. See section on [Data Fetching](../data-fetching.md) for details. # Field configuration options @@ -80,7 +80,7 @@ Option | Type | Notes name | `string` | **Required.** Name of the field. When not set - inferred from **fields** array key (read about [shorthand field definition](#shorthand-field-definitions) below) type | `Type` | **Required.** An instance of internal or custom type. Note: type must be represented by a single instance within one schema (see also [Type Registry](index.md#type-registry)) args | `array` | An array of possible type arguments. Each entry is expected to be an array with keys: **name**, **type**, **description**, **defaultValue**. See [Field Arguments](#field-arguments) section below. -resolve | `callable` | **function($rootValue, $args, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Given the **$value** of this type, it is expected to return actual value of the current field. See section on [Data Fetching](../data-fetching.md) for details +resolve | `callable` | **function($objectValue, $args, $context, [ResolveInfo](../reference.md#graphqltypedefinitionresolveinfo) $info)**
Given the **$objectValue** of this type, it is expected to return actual value of the current field. See section on [Data Fetching](../data-fetching.md) for details complexity | `callable` | **function($childrenComplexity, $args)**
Used to restrict query complexity. The feature is disabled by default, read about [Security](../security.md#query-complexity-analysis) to use it. description | `string` | Plain-text description of this field for clients (e.g. used by [GraphiQL](https://github.com/graphql/graphiql) for auto-generated documentation) deprecationReason | `string` | Text describing why this field is deprecated. When not empty - field will not be returned by introspection queries (unless forced) diff --git a/examples/00-hello-world/graphql.php b/examples/00-hello-world/graphql.php index 167b44d..7f933ad 100644 --- a/examples/00-hello-world/graphql.php +++ b/examples/00-hello-world/graphql.php @@ -35,7 +35,7 @@ try { 'x' => ['type' => Type::int()], 'y' => ['type' => Type::int()], ], - 'resolve' => function ($rootValue, $args) { + 'resolve' => function ($calc, $args) { return $args['x'] + $args['y']; }, ], diff --git a/examples/01-blog/Blog/Type/CommentType.php b/examples/01-blog/Blog/Type/CommentType.php index c61e338..8a97a3a 100644 --- a/examples/01-blog/Blog/Type/CommentType.php +++ b/examples/01-blog/Blog/Type/CommentType.php @@ -35,12 +35,12 @@ class CommentType extends ObjectType Types::htmlField('body') ]; }, - 'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { + 'resolveField' => function($comment, $args, $context, ResolveInfo $info) { $method = 'resolve' . ucfirst($info->fieldName); if (method_exists($this, $method)) { - return $this->{$method}($rootValue, $args, $context, $info); + return $this->{$method}($comment, $args, $context, $info); } else { - return $rootValue->{$info->fieldName}; + return $comment->{$info->fieldName}; } } ]; diff --git a/examples/01-blog/Blog/Type/StoryType.php b/examples/01-blog/Blog/Type/StoryType.php index 1df1f37..1abb897 100644 --- a/examples/01-blog/Blog/Type/StoryType.php +++ b/examples/01-blog/Blog/Type/StoryType.php @@ -75,12 +75,12 @@ class StoryType extends ObjectType 'interfaces' => [ Types::node() ], - 'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { + 'resolveField' => function($story, $args, $context, ResolveInfo $info) { $method = 'resolve' . ucfirst($info->fieldName); if (method_exists($this, $method)) { - return $this->{$method}($rootValue, $args, $context, $info); + return $this->{$method}($story, $args, $context, $info); } else { - return $rootValue->{$info->fieldName}; + return $story->{$info->fieldName}; } } ]; diff --git a/examples/01-blog/Blog/Type/UserType.php b/examples/01-blog/Blog/Type/UserType.php index cb5922d..b185f74 100644 --- a/examples/01-blog/Blog/Type/UserType.php +++ b/examples/01-blog/Blog/Type/UserType.php @@ -44,12 +44,12 @@ class UserType extends ObjectType 'interfaces' => [ Types::node() ], - 'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { + 'resolveField' => function($user, $args, $context, ResolveInfo $info) { $method = 'resolve' . ucfirst($info->fieldName); if (method_exists($this, $method)) { - return $this->{$method}($rootValue, $args, $context, $info); + return $this->{$method}($user, $args, $context, $info); } else { - return $rootValue->{$info->fieldName}; + return $user->{$info->fieldName}; } } ]; diff --git a/examples/03-server/graphql.php b/examples/03-server/graphql.php index 8b73f44..c0240da 100644 --- a/examples/03-server/graphql.php +++ b/examples/03-server/graphql.php @@ -35,7 +35,7 @@ try { 'x' => ['type' => Type::int()], 'y' => ['type' => Type::int()], ], - 'resolve' => function ($rootValue, $args) { + 'resolve' => function ($calc, $args) { return $args['x'] + $args['y']; }, ], diff --git a/tests/Executor/DeferredFieldsTest.php b/tests/Executor/DeferredFieldsTest.php index 787e2c9..957b5a8 100644 --- a/tests/Executor/DeferredFieldsTest.php +++ b/tests/Executor/DeferredFieldsTest.php @@ -401,7 +401,7 @@ class DeferredFieldsTest extends TestCase return [ 'sync' => [ 'type' => Type::string(), - 'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) { + 'resolve' => function ($complexType, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return 'sync'; @@ -409,7 +409,7 @@ class DeferredFieldsTest extends TestCase ], 'deferred' => [ 'type' => Type::string(), - 'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) { + 'resolve' => function ($complexType, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return new Deferred(function () use ($info) { @@ -421,7 +421,7 @@ class DeferredFieldsTest extends TestCase ], 'nest' => [ 'type' => $complexType, - 'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) { + 'resolve' => function ($complexType, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return []; @@ -429,7 +429,7 @@ class DeferredFieldsTest extends TestCase ], 'deferredNest' => [ 'type' => $complexType, - 'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) { + 'resolve' => function ($complexType, $args, $context, ResolveInfo $info) { $this->paths[] = $info->path; return new Deferred(function () use ($info) { diff --git a/tests/Executor/ExecutorTest.php b/tests/Executor/ExecutorTest.php index 76b4beb..eb8e4a1 100644 --- a/tests/Executor/ExecutorTest.php +++ b/tests/Executor/ExecutorTest.php @@ -273,7 +273,7 @@ class ExecutorTest extends TestCase 'fields' => [ 'test' => [ 'type' => Type::string(), - 'resolve' => static function ($rootValue, $args, $ctx, $_info) use (&$info) { + 'resolve' => static function ($test, $args, $ctx, $_info) use (&$info) { $info = $_info; }, ], diff --git a/tests/Executor/TestClasses/Adder.php b/tests/Executor/TestClasses/Adder.php index 416cd5e..562aba6 100644 --- a/tests/Executor/TestClasses/Adder.php +++ b/tests/Executor/TestClasses/Adder.php @@ -16,7 +16,7 @@ class Adder { $this->num = $num; - $this->test = function ($rootValue, $args, $context) { + $this->test = function ($objectValue, $args, $context) { return $this->num + $args['addend1'] + $context['addend2']; }; } diff --git a/tests/Regression/Issue396Test.php b/tests/Regression/Issue396Test.php index f7c26a9..ca5e5c0 100644 --- a/tests/Regression/Issue396Test.php +++ b/tests/Regression/Issue396Test.php @@ -30,7 +30,7 @@ class Issue396Test extends TestCase $unionResult = new UnionType([ 'name' => 'UnionResult', 'types' => [$a, $b, $c], - 'resolveType' => static function ($result, $rootValue, ResolveInfo $info) use ($a, $b, $c, &$log) : Type { + 'resolveType' => static function ($result, $value, ResolveInfo $info) use ($a, $b, $c, &$log) : Type { $log[] = [$result, $info->path]; if (stristr($result['name'], 'A')) { return $a; @@ -97,7 +97,7 @@ class Issue396Test extends TestCase 'fields' => [ 'name' => Type::string(), ], - 'resolveType' => static function ($result, $rootValue, ResolveInfo $info) use (&$a, &$b, &$c, &$log) : Type { + 'resolveType' => static function ($result, $value, ResolveInfo $info) use (&$a, &$b, &$c, &$log) : Type { $log[] = [$result, $info->path]; if (stristr($result['name'], 'A')) { return $a; diff --git a/tests/Type/IntrospectionTest.php b/tests/Type/IntrospectionTest.php index afea3d9..d38bda1 100644 --- a/tests/Type/IntrospectionTest.php +++ b/tests/Type/IntrospectionTest.php @@ -1049,7 +1049,7 @@ class IntrospectionTest extends TestCase 'field' => [ 'type' => Type::string(), 'args' => ['complex' => ['type' => $TestInputObject]], - 'resolve' => static function ($rootValue, $args) { + 'resolve' => static function ($testType, $args) { return json_encode($args['complex']); }, ], From 8c4e7b178d6c744de72c93b47e8a71d97e7fb862 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 1 Jul 2019 12:17:04 +0200 Subject: [PATCH 8/8] Fix up some overly eager renamings in the docs --- docs/data-fetching.md | 33 ++++++++++++++++----------------- docs/reference.md | 4 ++-- src/Executor/Executor.php | 18 +++++++++--------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/docs/data-fetching.md b/docs/data-fetching.md index 349e702..c5152ba 100644 --- a/docs/data-fetching.md +++ b/docs/data-fetching.md @@ -103,25 +103,25 @@ for a field you simply override this default resolver. **graphql-php** provides following default field resolver: ```php fieldName; - $property = null; +function defaultFieldResolver($objectValue, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info) + { + $fieldName = $info->fieldName; + $property = null; - if (is_array($rootValue) || $rootValue instanceof ArrayAccess) { - if (isset($rootValue[$fieldName])) { - $property = $rootValue[$fieldName]; - } - } elseif (is_object($rootValue)) { - if (isset($rootValue->{$fieldName})) { - $property = $rootValue->{$fieldName}; + if (is_array($objectValue) || $objectValue instanceof \ArrayAccess) { + if (isset($objectValue[$fieldName])) { + $property = $objectValue[$fieldName]; + } + } elseif (is_object($objectValue)) { + if (isset($objectValue->{$fieldName})) { + $property = $objectValue->{$fieldName}; + } } + + return $property instanceof Closure + ? $property($objectValue, $args, $context, $info) + : $property; } - - return $property instanceof Closure - ? $property($rootValue, $args, $context, $info) - : $property; -} ``` As you see it returns value by key (for arrays) or property (for objects). @@ -163,7 +163,6 @@ $userType = new ObjectType([ Keep in mind that **field resolver** has precedence over **default field resolver per type** which in turn has precedence over **default field resolver**. - # Solving N+1 Problem Since: 0.9.0 diff --git a/docs/reference.md b/docs/reference.md index 36d4d1a..49f2372 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -33,7 +33,7 @@ See [related documentation](executing-queries.md). * fieldResolver: * A resolver function to use when one is not provided by the schema. * If not provided, the default field resolver is used (which looks for a - * value on the root value with the field's name). + * value on the object value with the field's name). * validationRules: * A set of rules for query validation step. Default value is all available rules. * Empty array would allow to skip query validation (may be convenient for persisted @@ -998,7 +998,7 @@ visitor API: * * @api */ -static function visit($rootValue, $visitor, $keyMap = null) +static function visit($root, $visitor, $keyMap = null) ``` ```php diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 2877f9d..764fa1c 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -161,29 +161,29 @@ class Executor * and returns it as the result, or if it's a function, returns the result * of calling that function while passing along args and context. * - * @param mixed $rootValue + * @param mixed $objectValue * @param mixed[] $args * @param mixed|null $context * * @return mixed|null */ - public static function defaultFieldResolver($rootValue, $args, $context, ResolveInfo $info) + public static function defaultFieldResolver($objectValue, $args, $context, ResolveInfo $info) { $fieldName = $info->fieldName; $property = null; - if (is_array($rootValue) || $rootValue instanceof ArrayAccess) { - if (isset($rootValue[$fieldName])) { - $property = $rootValue[$fieldName]; + if (is_array($objectValue) || $objectValue instanceof ArrayAccess) { + if (isset($objectValue[$fieldName])) { + $property = $objectValue[$fieldName]; } - } elseif (is_object($rootValue)) { - if (isset($rootValue->{$fieldName})) { - $property = $rootValue->{$fieldName}; + } elseif (is_object($objectValue)) { + if (isset($objectValue->{$fieldName})) { + $property = $objectValue->{$fieldName}; } } return $property instanceof Closure - ? $property($rootValue, $args, $context, $info) + ? $property($objectValue, $args, $context, $info) : $property; } }