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
Exemplo n.º 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;
 }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
0
 /**
  * Constructs a ExecutionContext object from the arguments passed to
  * execute, which we will pass throughout the other execution methods.
  */
 private static function buildExecutionContext(Schema $schema, Document $documentAst, $rootValue, $rawVariableValues, $operationName = null)
 {
     $errors = [];
     $operations = [];
     $fragments = [];
     foreach ($documentAst->definitions as $statement) {
         switch ($statement->kind) {
             case Node::OPERATION_DEFINITION:
                 $operations[$statement->name ? $statement->name->value : ''] = $statement;
                 break;
             case Node::FRAGMENT_DEFINITION:
                 $fragments[$statement->name->value] = $statement;
                 break;
         }
     }
     if (!$operationName && count($operations) !== 1) {
         throw new Error('Must provide operation name if query contains multiple operations.');
     }
     $opName = $operationName ?: key($operations);
     if (empty($operations[$opName])) {
         throw new Error('Unknown operation named ' . $opName);
     }
     $operation = $operations[$opName];
     $variableValues = Values::getVariableValues($schema, $operation->variableDefinitions ?: [], $rawVariableValues ?: []);
     $exeContext = new ExecutionContext($schema, $fragments, $rootValue, $operation, $variableValues, $errors);
     return $exeContext;
 }