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:
```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;
$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;
}
```

View File

@ -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

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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) {