Consistently name the $rootValue argument

This commit is contained in:
spawnia 2019-06-23 17:30:57 +02:00
parent 93ccd7351d
commit 03c33c9dc2
5 changed files with 38 additions and 37 deletions

View File

@ -103,22 +103,22 @@ for a field you simply override this default resolver.
**graphql-php** provides following default field resolver: **graphql-php** provides following default field resolver:
```php ```php
<?php <?php
function defaultFieldResolver($source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info) function defaultFieldResolver($rootValue, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info)
{ {
$fieldName = $info->fieldName; $fieldName = $info->fieldName;
$property = null; $property = null;
if (is_array($source) || $source instanceof \ArrayAccess) { if (is_array($rootValue) || $rootValue instanceof ArrayAccess) {
if (isset($source[$fieldName])) { if (isset($rootValue[$fieldName])) {
$property = $source[$fieldName]; $property = $rootValue[$fieldName];
} }
} else if (is_object($source)) { } elseif (is_object($rootValue)) {
if (isset($source->{$fieldName})) { if (isset($rootValue->{$fieldName})) {
$property = $source->{$fieldName}; $property = $rootValue->{$fieldName};
} }
} }
return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; return $property instanceof Closure ? $property($rootValue, $args, $context, $info) : $property;
} }
``` ```

View File

@ -33,7 +33,7 @@ See [related documentation](executing-queries.md).
* fieldResolver: * fieldResolver:
* A resolver function to use when one is not provided by the schema. * 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 * 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: * validationRules:
* A set of rules for query validation step. Default value is all available rules. * 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 * Empty array would allow to skip query validation (may be convenient for persisted

View File

@ -50,7 +50,7 @@ class ExecutionContext
public function __construct( public function __construct(
$schema, $schema,
$fragments, $fragments,
$root, $rootValue,
$contextValue, $contextValue,
$operation, $operation,
$variables, $variables,
@ -60,7 +60,7 @@ class ExecutionContext
) { ) {
$this->schema = $schema; $this->schema = $schema;
$this->fragments = $fragments; $this->fragments = $fragments;
$this->rootValue = $root; $this->rootValue = $rootValue;
$this->contextValue = $contextValue; $this->contextValue = $contextValue;
$this->operation = $operation; $this->operation = $operation;
$this->variableValues = $variables; $this->variableValues = $variables;

View File

@ -157,31 +157,31 @@ class Executor
/** /**
* If a resolve function is not given, then a default resolve behavior is used * 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 * 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. * of calling that function while passing along args and context.
* *
* @param mixed $source * @param mixed $rootValue
* @param mixed[] $args * @param mixed[] $args
* @param mixed|null $context * @param mixed|null $context
* *
* @return mixed|null * @return mixed|null
*/ */
public static function defaultFieldResolver($source, $args, $context, ResolveInfo $info) public static function defaultFieldResolver($rootValue, $args, $context, ResolveInfo $info)
{ {
$fieldName = $info->fieldName; $fieldName = $info->fieldName;
$property = null; $property = null;
if (is_array($source) || $source instanceof ArrayAccess) { if (is_array($rootValue) || $rootValue instanceof ArrayAccess) {
if (isset($source[$fieldName])) { if (isset($rootValue[$fieldName])) {
$property = $source[$fieldName]; $property = $rootValue[$fieldName];
} }
} elseif (is_object($source)) { } elseif (is_object($rootValue)) {
if (isset($source->{$fieldName})) { if (isset($rootValue->{$fieldName})) {
$property = $source->{$fieldName}; $property = $rootValue->{$fieldName};
} }
} }
return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; return $property instanceof Closure ? $property($rootValue, $args, $context, $info) : $property;
} }
} }

View File

@ -237,7 +237,7 @@ class ReferenceExecutor implements ExecutorImplementation
/** /**
* Implements the "Evaluating operations" section of the spec. * Implements the "Evaluating operations" section of the spec.
* *
* @param mixed[] $rootValue * @param mixed $rootValue
* *
* @return Promise|stdClass|mixed[] * @return Promise|stdClass|mixed[]
*/ */
@ -463,21 +463,21 @@ class ReferenceExecutor implements ExecutorImplementation
* Implements the "Evaluating selection sets" section of the spec * Implements the "Evaluating selection sets" section of the spec
* for "write" mode. * for "write" mode.
* *
* @param mixed[] $sourceValue * @param mixed $rootValue
* @param mixed[] $path * @param mixed[] $path
* @param ArrayObject $fields * @param ArrayObject $fields
* *
* @return Promise|stdClass|mixed[] * @return Promise|stdClass|mixed[]
*/ */
private function executeFieldsSerially(ObjectType $parentType, $sourceValue, $path, $fields) private function executeFieldsSerially(ObjectType $parentType, $rootValue, $path, $fields)
{ {
$result = $this->promiseReduce( $result = $this->promiseReduce(
array_keys($fields->getArrayCopy()), array_keys($fields->getArrayCopy()),
function ($results, $responseName) use ($path, $parentType, $sourceValue, $fields) { function ($results, $responseName) use ($path, $parentType, $rootValue, $fields) {
$fieldNodes = $fields[$responseName]; $fieldNodes = $fields[$responseName];
$fieldPath = $path; $fieldPath = $path;
$fieldPath[] = $responseName; $fieldPath[] = $responseName;
$result = $this->resolveField($parentType, $sourceValue, $fieldNodes, $fieldPath); $result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath);
if ($result === self::$UNDEFINED) { if ($result === self::$UNDEFINED) {
return $results; return $results;
} }
@ -505,18 +505,19 @@ class ReferenceExecutor implements ExecutorImplementation
} }
/** /**
* Resolves the field on the given source object. In particular, this * Resolves the field on the given root value.
* 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 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 FieldNode[] $fieldNodes
* @param mixed[] $path * @param mixed[] $path
* *
* @return mixed[]|Exception|mixed|null * @return mixed[]|Exception|mixed|null
*/ */
private function resolveField(ObjectType $parentType, $source, $fieldNodes, $path) private function resolveField(ObjectType $parentType, $rootValue, $fieldNodes, $path)
{ {
$exeContext = $this->exeContext; $exeContext = $this->exeContext;
$fieldNode = $fieldNodes[0]; $fieldNode = $fieldNodes[0];
@ -557,7 +558,7 @@ class ReferenceExecutor implements ExecutorImplementation
$fieldDef, $fieldDef,
$fieldNode, $fieldNode,
$resolveFn, $resolveFn,
$source, $rootValue,
$context, $context,
$info $info
); );
@ -614,13 +615,13 @@ class ReferenceExecutor implements ExecutorImplementation
* @param FieldDefinition $fieldDef * @param FieldDefinition $fieldDef
* @param FieldNode $fieldNode * @param FieldNode $fieldNode
* @param callable $resolveFn * @param callable $resolveFn
* @param mixed $source * @param mixed $rootValue
* @param mixed $context * @param mixed $context
* @param ResolveInfo $info * @param ResolveInfo $info
* *
* @return Throwable|Promise|mixed * @return Throwable|Promise|mixed
*/ */
private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $source, $context, $info) private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $rootValue, $context, $info)
{ {
try { try {
// Build hash of arguments from the field.arguments AST, using the // Build hash of arguments from the field.arguments AST, using the
@ -631,7 +632,7 @@ class ReferenceExecutor implements ExecutorImplementation
$this->exeContext->variableValues $this->exeContext->variableValues
); );
return $resolveFn($source, $args, $context, $info); return $resolveFn($rootValue, $args, $context, $info);
} catch (Exception $error) { } catch (Exception $error) {
return $error; return $error;
} catch (Throwable $error) { } catch (Throwable $error) {