src/Executor/Values.php
public static function getVariableValues(Schema $schema, $varDefNodes, array $inputs)
    {
        $errors        = [];
        $coercedValues = [];
        foreach ($varDefNodes as $varDefNode) {
            $varName = $varDefNode->variable->name->value;
            /** @var InputType|Type $varType */
            $varType = TypeInfo::typeFromAST($schema, $varDefNode->type);

            if (Type::isInputType($varType)) {
                if (array_key_exists($varName, $inputs)) {
                    $value   = $inputs[$varName];
                    $coerced = Value::coerceValue($value, $varType, $varDefNode);
                    /** @var Error[] $coercionErrors */
                    $coercionErrors = $coerced['errors'];
                    if (empty($coercionErrors)) {
                        $coercedValues[$varName] = $coerced['value'];
                    } else {
                        $messagePrelude = sprintf(
                            'Variable "$%s" got invalid value %s; ',
                            $varName,
                            Utils::printSafeJson($value)
                        );

                        foreach ($coercionErrors as $error) {
                            $errors[] = new Error(
                                $messagePrelude . $error->getMessage(),
                                $error->getNodes(),
                                $error->getSource(),
                                $error->getPositions(),
                                $error->getPath(),
                                $error,
                                $error->getExtensions()
                            );
                        }
                    }
                } else {
                    if ($varType instanceof NonNull) {
                        $errors[] = new Error(
                            sprintf(
                                'Variable "$%s" of required type "%s" was not provided.',
                                $varName,
                                $varType
                            ),
                            [$varDefNode]
                        );
                    } elseif ($varDefNode->defaultValue) {
                        $coercedValues[$varName] = AST::valueFromAST($varDefNode->defaultValue, $varType);
                    }
                }
            } else {
                $errors[] = new Error(
                    sprintf(
                        'Variable "$%s" expected value of type "%s" which cannot be used as an input type.',
                        $varName,
                        Printer::doPrint($varDefNode->type)
                    ),
                    [$varDefNode->type]
                );
            }
        }

        if (! empty($errors)) {
            return [$errors, null];
        }

        return [null, $coercedValues];
    }
public static function getVariableValues(Schema $schema, $varDefNodes, array $inputs)
    {
        $errors        = [];
        $coercedValues = [];
        foreach ($varDefNodes as $varDefNode) {
            $varName = $varDefNode->variable->name->value;
            /** @var InputType|Type $varType */
            $varType = TypeInfo::typeFromAST($schema, $varDefNode->type);

            if (Type::isInputType($varType)) {
                if (array_key_exists($varName, $inputs)) {
                    $value   = $inputs[$varName];
                    $coerced = Value::coerceValue($value, $varType, $varDefNode);
                    /** @var Error[] $coercionErrors */
                    $coercionErrors = $coerced['errors'];
                    if (empty($coercionErrors)) {
                        $coercedValues[$varName] = $coerced['value'];
                    } else {
                        $messagePrelude = sprintf(
                            'Variable "$%s" got invalid value %s; ',
                            $varName,
                            Utils::printSafeJson($value)
                        );

                        foreach ($coercionErrors as $error) {
                            $errors[] = new Error(
                                $messagePrelude . $error->getMessage(),
                                $error->getNodes(),
                                $error->getSource(),
                                $error->getPositions(),
                                $error->getPath(),
                                $error,
                                $error->getExtensions()
                            );
                        }
                    }
                } else {
                    if ($varType instanceof NonNull) {
                        $errors[] = new Error(
                            sprintf(
                                'Variable "$%s" of required type "%s" was not provided.',
                                $varName,
                                $varType
                            ),
                            [$varDefNode]
                        );
                    } elseif ($varDefNode->defaultValue) {
                        $coercedValues[$varName] = AST::valueFromAST($varDefNode->defaultValue, $varType);
                    }
                }
            } else {
                $errors[] = new Error(
                    sprintf(
                        'Variable "$%s" expected value of type "%s" which cannot be used as an input type.',
                        $varName,
                        Printer::doPrint($varDefNode->type)
                    ),
                    [$varDefNode->type]
                );
            }
        }

        if (! empty($errors)) {
            return [$errors, null];
        }

        return [null, $coercedValues];
    }
public static function getVariableValues ( Schema $ schema , $ varDefNodes , array $ inputs ) { $ errors = [ ] ; $ coercedValues = [ ] ; foreach ( $ varDefNodes as $ varDefNode ) { $ varName = $ varDefNode -> variable -> name -> value ; /** @var InputType|Type $varType */ $ varType = TypeInfo :: typeFromAST ( $ schema , $ varDefNode -> type ) ; if ( Type :: isInputType ( $ varType ) ) { if ( array_key_exists ( $ varName , $ inputs ) ) { $ value = $ inputs [ $ varName ] ; $ coerced = Value :: coerceValue ( $ value , $ varType , $ varDefNode ) ; /** @var Error[] $coercionErrors */ $ coercionErrors = $ coerced [ 'errors' ] ; if ( empty ( $ coercionErrors ) ) { $ coercedValues [ $ varName ] = $ coerced [ 'value' ] ; } else { $ messagePrelude = sprintf ( 'Variable "$%s" got invalid value %s; ' , $ varName , Utils :: printSafeJson ( $ value ) ) ; foreach ( $ coercionErrors as $ error ) { $ errors [ ] = new Error ( $ messagePrelude . $ error -> getMessage ( ) , $ error -> getNodes ( ) , $ error -> getSource ( ) , $ error -> getPositions ( ) , $ error -> getPath ( ) , $ error , $ error -> getExtensions ( ) ) ; } } } else { if ( $ varType instanceof NonNull ) { $ errors [ ] = new Error ( sprintf ( 'Variable "$%s" of required type "%s" was not provided.' , $ varName , $ varType ) , [ $ varDefNode ] ) ; } elseif ( $ varDefNode -> defaultValue ) { $ coercedValues [ $ varName ] = AST :: valueFromAST ( $ varDefNode -> defaultValue , $ varType ) ; } } } else { $ errors [ ] = new Error ( sprintf ( 'Variable "$%s" expected value of type "%s" which cannot be used as an input type.' , $ varName , Printer :: doPrint ( $ varDefNode -> type ) ) , [ $ varDefNode -> type ] ) ; } } if ( ! empty ( $ errors ) ) { return [ $ errors , null ] ; } return [ null , $ coercedValues ] ; }
Prepares an object map of variables of the correct type based on the provided
variable definitions and arbitrary input. If the input cannot be coerced
to match the variable definitions, a Error will be thrown.

@param VariableDefinitionNode[] $varDefNodes
@param mixed[]                  $inputs

@return mixed[]
Prepares an object map of variables of the correct type based on the provided variable definitions and arbitrary input . If the input cannot be coerced to match the variable definitions a Error will be thrown .