Automattic\WooCommerce\Vendor\GraphQL\Executor

ReferenceExecutor::buildExecutionContextprotected staticWC 1.0

Constructs an ExecutionContext object from the arguments passed to execute, which we will pass throughout the other execution methods.

Method of the class: ReferenceExecutor{}

No Hooks.

Returns

ExecutionContext|list.

Usage

$result = ReferenceExecutor::buildExecutionContext( $schema, $documentNode, $rootValue, $contextValue, $rawVariableValues, ?string $operationName, $fieldResolver, $argsMapper, $promiseAdapter );
$schema(Schema) (required)
.
$documentNode(DocumentNode) (required)
.
$rootValue(mixed) (required)
.
$contextValue(mixed) (required)
.
$rawVariableValues(array) (required)
.
?string $operationName(required)
.
$fieldResolver(callable) (required)
.
$argsMapper(callable) (required)
.
$promiseAdapter(PromiseAdapter) (required)
.

ReferenceExecutor::buildExecutionContext() code WC 10.9.1

protected static function buildExecutionContext(
    Schema $schema,
    DocumentNode $documentNode,
    $rootValue,
    $contextValue,
    array $rawVariableValues,
    ?string $operationName,
    callable $fieldResolver,
    callable $argsMapper,
    PromiseAdapter $promiseAdapter
) {
    /** @var list<Error> $errors */
    $errors = [];

    /** @var array<string, FragmentDefinitionNode> $fragments */
    $fragments = [];

    /** @var OperationDefinitionNode|null $operation */
    $operation = null;

    /** @var bool $hasMultipleAssumedOperations */
    $hasMultipleAssumedOperations = false;

    foreach ($documentNode->definitions as $definition) {
        switch (true) {
            case $definition instanceof OperationDefinitionNode:
                if ($operationName === null && $operation !== null) {
                    $hasMultipleAssumedOperations = true;
                }

                if (
                    $operationName === null
                    || (isset($definition->name) && $definition->name->value === $operationName)
                ) {
                    $operation = $definition;
                }

                break;
            case $definition instanceof FragmentDefinitionNode:
                $fragments[$definition->name->value] = $definition;
                break;
        }
    }

    if ($operation === null) {
        $message = $operationName === null
            ? 'Must provide an operation.'
            : "Unknown operation named \"{$operationName}\".";
        $errors[] = new Error($message);
    } elseif ($hasMultipleAssumedOperations) {
        $errors[] = new Error(
            'Must provide operation name if query contains multiple operations.'
        );
    }

    $variableValues = null;
    if ($operation !== null) {
        [$coercionErrors, $coercedVariableValues] = Values::getVariableValues(
            $schema,
            $operation->variableDefinitions,
            $rawVariableValues
        );
        if ($coercionErrors === null) {
            $variableValues = $coercedVariableValues;
        } else {
            $errors = array_merge($errors, $coercionErrors);
        }
    }

    if ($errors !== []) {
        return $errors;
    }

    assert($operation instanceof OperationDefinitionNode, 'Has operation if no errors.');
    assert(is_array($variableValues), 'Has variables if no errors.');

    return new ExecutionContext(
        $schema,
        $fragments,
        $rootValue,
        $contextValue,
        $operation,
        $variableValues,
        $errors,
        $fieldResolver,
        $argsMapper,
        $promiseAdapter
    );
}