toArray(); } if ($result instanceof Promise) { return $result->then(function(ExecutionResult $executionResult) { return $executionResult->toArray(); }); } throw new InvariantViolation("Unexpected execution result"); } /** * Same as `execute`, but returns instance of ExecutionResult instead of array, * which can be used for custom error formatting or adding extensions to response * * @param Schema $schema * @param string|DocumentNode $source * @param mixed $rootValue * @param mixed $contextValue * @param array|null $variableValues * @param string|null $operationName * @param callable $fieldResolver * @param PromiseAdapter $promiseAdapter * * @return ExecutionResult|Promise */ public static function executeAndReturnResult( Schema $schema, $source, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null, callable $fieldResolver = null, PromiseAdapter $promiseAdapter = null ) { try { if ($source instanceof DocumentNode) { $documentNode = $source; } else { $documentNode = Parser::parse(new Source($source ?: '', 'GraphQL')); } /** @var QueryComplexity $queryComplexity */ $queryComplexity = DocumentValidator::getRule('QueryComplexity'); $queryComplexity->setRawVariableValues($variableValues); $validationErrors = DocumentValidator::validate($schema, $documentNode); if (!empty($validationErrors)) { return new ExecutionResult(null, $validationErrors); } else { return Executor::execute( $schema, $documentNode, $rootValue, $contextValue, $variableValues, $operationName, $fieldResolver, $promiseAdapter ); } } catch (Error $e) { return new ExecutionResult(null, [$e]); } } /** * @return array */ public static function getInternalDirectives() { return array_values(Directive::getInternalDirectives()); } /** * @param callable $fn */ public static function setDefaultFieldResolver(callable $fn) { Executor::setDefaultFieldResolver($fn); } /** * @param PromiseAdapter|null $promiseAdapter */ public static function setPromiseAdapter(PromiseAdapter $promiseAdapter = null) { Executor::setPromiseAdapter($promiseAdapter); } }