Automattic\WooCommerce\Vendor\GraphQL\Executor

ReferenceExecutor::completeValueprotectedWC 1.0

Implements the instructions for completeValue as defined in the "Field entries" section of the spec.

If the field type is Non-Null, then this recursively completes the value for the inner type. It throws a field error if that completion returns null, as per the "Nullability" section of the spec.

If the field type is a List, then this recursively completes the value for the inner type on each item in the list.

If the field type is a Scalar or Enum, ensures the completed value is a legal value of the type by calling the serialize method of Automattic\WooCommerce\Vendor\GraphQL type definition.

If the field is an abstract type, determine the runtime type of the value and then complete based on that type.

Otherwise, the field type expects a sub-selection set, and will complete the value by evaluating all sub-selections.

Method of the class: ReferenceExecutor{}

No Hooks.

Returns

Array|Mixed|Promise|null.

Usage

// protected - for code of main (parent) or child class
$result = $this->completeValue( $returnType, $fieldNodes, $info, $path, $unaliasedPath, $result, $contextValue );
$returnType(Type) (required)
.
$fieldNodes(ArrayObject) (required)
.
$info(ResolveInfo) (required)
.
$path(list<string|int>) (required)
.
$unaliasedPath(list<string|int>) (required)
.
$result(mixed) (required)
.
$contextValue(mixed) (required)
.

ReferenceExecutor::completeValue() code WC 10.9.1

protected function completeValue(
    Type $returnType,
    \ArrayObject $fieldNodes,
    ResolveInfo $info,
    array $path,
    array $unaliasedPath,
    $result,
    $contextValue
) {
    // If result is an Error, throw a located error.
    if ($result instanceof \Throwable) {
        throw $result;
    }

    // If field type is NonNull, complete for inner type, and throw field error
    // if result is null.
    if ($returnType instanceof NonNull) {
        $completed = $this->completeValue(
            $returnType->getWrappedType(),
            $fieldNodes,
            $info,
            $path,
            $unaliasedPath,
            $result,
            $contextValue
        );
        if ($completed === null) {
            throw new InvariantViolation("Cannot return null for non-nullable field \"{$info->parentType}.{$info->fieldName}\".");
        }

        return $completed;
    }

    if ($result === null) {
        return null;
    }

    // If field type is List, complete each item in the list with the inner type
    if ($returnType instanceof ListOfType) {
        if (! is_iterable($result)) {
            $resultType = gettype($result);

            throw new InvariantViolation("Expected field {$info->parentType}.{$info->fieldName} to return iterable, but got: {$resultType}.");
        }

        return $this->completeListValue($returnType, $fieldNodes, $info, $path, $unaliasedPath, $result, $contextValue);
    }

    assert($returnType instanceof NamedType, 'Wrapping types should return early');

    // Account for invalid schema definition when typeLoader returns different
    // instance than `resolveType` or $field->getType() or $arg->getType()
    assert(
        $returnType === $this->exeContext->schema->getType($returnType->name)
        || Type::isBuiltInScalar($returnType),
        SchemaValidationContext::duplicateType($this->exeContext->schema, "{$info->parentType}.{$info->fieldName}", $returnType->name)
    );

    if ($returnType instanceof LeafType) {
        if (Type::isBuiltInScalar($returnType)) {
            $schemaType = $this->exeContext->schema->getType($returnType->name);
            assert($schemaType instanceof LeafType, "Schema must provide a LeafType for built-in scalar \"{$returnType->name}\".");
            $returnType = $schemaType;
        }

        return $this->completeLeafValue($returnType, $result);
    }

    if ($returnType instanceof AbstractType) {
        return $this->completeAbstractValue($returnType, $fieldNodes, $info, $path, $unaliasedPath, $result, $contextValue);
    }

    // Field type must be and Object, Interface or Union and expect sub-selections.
    if ($returnType instanceof ObjectType) {
        return $this->completeObjectValue($returnType, $fieldNodes, $info, $path, $unaliasedPath, $result, $contextValue);
    }

    $safeReturnType = Utils::printSafe($returnType);
    throw new \RuntimeException("Cannot complete value of unexpected type {$safeReturnType}.");
}