GraphQL\Executor\Values::getVariableValues PHP Method

getVariableValues() public static method

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.
public static getVariableValues ( Schema $schema, VariableDefinitionNode[] $definitionNodes, array $inputs ) : array
$schema GraphQL\Schema
$definitionNodes GraphQL\Language\AST\VariableDefinitionNode[]
$inputs array
return array
    public static function getVariableValues(Schema $schema, $definitionNodes, array $inputs)
    {
        $coercedValues = [];
        foreach ($definitionNodes as $definitionNode) {
            $varName = $definitionNode->variable->name->value;
            $varType = Utils\TypeInfo::typeFromAST($schema, $definitionNode->type);
            if (!Type::isInputType($varType)) {
                throw new Error('Variable "$' . $varName . '" expected value of type ' . '"' . Printer::doPrint($definitionNode->type) . '" which cannot be used as an input type.', [$definitionNode->type]);
            }
            if (!array_key_exists($varName, $inputs)) {
                $defaultValue = $definitionNode->defaultValue;
                if ($defaultValue) {
                    $coercedValues[$varName] = Utils\AST::valueFromAST($defaultValue, $varType);
                }
                if ($varType instanceof NonNull) {
                    throw new Error('Variable "$' . $varName . '" of required type ' . '"' . Utils::printSafe($varType) . '" was not provided.', [$definitionNode]);
                }
            } else {
                $value = $inputs[$varName];
                $errors = self::isValidPHPValue($value, $varType);
                if (!empty($errors)) {
                    $message = "\n" . implode("\n", $errors);
                    throw new Error('Variable "$' . $varName . '" got invalid value ' . json_encode($value) . '.' . $message, [$definitionNode]);
                }
                $coercedValue = self::coerceValue($varType, $value);
                Utils::invariant($coercedValue !== Utils::undefined(), 'Should have reported error.');
                $coercedValues[$varName] = $coercedValue;
            }
        }
        return $coercedValues;
    }

Usage Example

Beispiel #1
0
 /**
  * Constructs a ExecutionContext object from the arguments passed to
  * execute, which we will pass throughout the other execution methods.
  *
  * @param Schema $schema
  * @param DocumentNode $documentNode
  * @param $rootValue
  * @param $contextValue
  * @param $rawVariableValues
  * @param string $operationName
  * @return ExecutionContext
  * @throws Error
  */
 private static function buildExecutionContext(Schema $schema, DocumentNode $documentNode, $rootValue, $contextValue, $rawVariableValues, $operationName = null)
 {
     $errors = [];
     $fragments = [];
     $operation = null;
     foreach ($documentNode->definitions as $definition) {
         switch ($definition->kind) {
             case NodeKind::OPERATION_DEFINITION:
                 if (!$operationName && $operation) {
                     throw new Error('Must provide operation name if query contains multiple operations.');
                 }
                 if (!$operationName || isset($definition->name) && $definition->name->value === $operationName) {
                     $operation = $definition;
                 }
                 break;
             case NodeKind::FRAGMENT_DEFINITION:
                 $fragments[$definition->name->value] = $definition;
                 break;
             default:
                 throw new Error("GraphQL cannot execute a request containing a {$definition->kind}.", [$definition]);
         }
     }
     if (!$operation) {
         if ($operationName) {
             throw new Error("Unknown operation named \"{$operationName}\".");
         } else {
             throw new Error('Must provide an operation.');
         }
     }
     $variableValues = Values::getVariableValues($schema, $operation->variableDefinitions ?: [], $rawVariableValues ?: []);
     $exeContext = new ExecutionContext($schema, $fragments, $rootValue, $contextValue, $operation, $variableValues, $errors);
     return $exeContext;
 }
All Usage Examples Of GraphQL\Executor\Values::getVariableValues