/**
* 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;
}