GraphQL\Executor\Values::getArgumentValues PHP Method

getArgumentValues() public static method

Prepares an object map of argument values given a list of argument definitions and list of argument AST nodes.
public static getArgumentValues ( FieldDefinition | Directive $def, FieldNode | DirectiveNode $node, $variableValues ) : array
$def GraphQL\Type\Definition\FieldDefinition | GraphQL\Type\Definition\Directive
$node GraphQL\Language\AST\FieldNode | GraphQL\Language\AST\DirectiveNode
$variableValues
return array
    public static function getArgumentValues($def, $node, $variableValues)
    {
        $argDefs = $def->args;
        $argNodes = $node->arguments;
        if (!$argDefs || null === $argNodes) {
            return [];
        }
        $coercedValues = [];
        $undefined = Utils::undefined();
        /** @var ArgumentNode[] $argNodeMap */
        $argNodeMap = $argNodes ? Utils::keyMap($argNodes, function (ArgumentNode $arg) {
            return $arg->name->value;
        }) : [];
        foreach ($argDefs as $argDef) {
            $name = $argDef->name;
            $argType = $argDef->getType();
            $argumentNode = isset($argNodeMap[$name]) ? $argNodeMap[$name] : null;
            if (!$argumentNode) {
                if ($argDef->defaultValueExists()) {
                    $coercedValues[$name] = $argDef->defaultValue;
                } else {
                    if ($argType instanceof NonNull) {
                        throw new Error('Argument "' . $name . '" of required type ' . '"' . Utils::printSafe($argType) . '" was not provided.', [$node]);
                    }
                }
            } else {
                if ($argumentNode->value instanceof VariableNode) {
                    $variableName = $argumentNode->value->name->value;
                    if ($variableValues && array_key_exists($variableName, $variableValues)) {
                        // Note: this does not check that this variable value is correct.
                        // This assumes that this query has been validated and the variable
                        // usage here is of the correct type.
                        $coercedValues[$name] = $variableValues[$variableName];
                    } else {
                        if ($argDef->defaultValueExists()) {
                            $coercedValues[$name] = $argDef->defaultValue;
                        } else {
                            if ($argType instanceof NonNull) {
                                throw new Error('Argument "' . $name . '" of required type "' . Utils::printSafe($argType) . '" was ' . 'provided the variable "$' . $variableName . '" which was not provided ' . 'a runtime value.', [$argumentNode->value]);
                            }
                        }
                    }
                } else {
                    $valueNode = $argumentNode->value;
                    $coercedValue = Utils\AST::valueFromAST($valueNode, $argType, $variableValues);
                    if ($coercedValue === $undefined) {
                        $errors = DocumentValidator::isValidLiteralValue($argType, $valueNode);
                        $message = !empty($errors) ? "\n" . implode("\n", $errors) : '';
                        throw new Error('Argument "' . $name . '" got invalid value ' . Printer::doPrint($valueNode) . '.' . $message, [$argumentNode->value]);
                    }
                    $coercedValues[$name] = $coercedValue;
                }
            }
        }
        return $coercedValues;
    }

Usage Example

Example #1
0
 private function buildFieldArguments(FieldNode $node)
 {
     $rawVariableValues = $this->getRawVariableValues();
     $astFieldInfo = $this->astFieldInfo($node);
     $fieldDef = $astFieldInfo[1];
     $args = [];
     if ($fieldDef instanceof FieldDefinition) {
         $variableValues = Values::getVariableValues($this->context->getSchema(), $this->variableDefs, $rawVariableValues);
         $args = Values::getArgumentValues($fieldDef, $node, $variableValues);
     }
     return $args;
 }
All Usage Examples Of GraphQL\Executor\Values::getArgumentValues