diff --git a/src/Executor/ExecutionResult.php b/src/Executor/ExecutionResult.php index f1b4487..074f200 100644 --- a/src/Executor/ExecutionResult.php +++ b/src/Executor/ExecutionResult.php @@ -25,6 +25,9 @@ class ExecutionResult $this->errors = $errors; } + /** + * @return array + */ public function toArray() { $result = ['data' => $this->data]; diff --git a/src/GraphQL.php b/src/GraphQL.php index 42811c3..22b91b9 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -1,6 +1,7 @@ toArray(); + } + + /** + * @param Schema $schema + * @param $requestString + * @param null $rootValue + * @param null $variableValues + * @param null $operationName + * @return array|ExecutionResult + */ + public static function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null) { try { $source = new Source($requestString ?: '', 'GraphQL request'); @@ -24,12 +38,12 @@ class GraphQL $validationErrors = DocumentValidator::validate($schema, $documentAST); if (!empty($validationErrors)) { - return ['errors' => array_map(['GraphQL\Error', 'formatError'], $validationErrors)]; + return new ExecutionResult(null, $validationErrors); } else { - return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName)->toArray(); + return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName); } } catch (Error $e) { - return ['errors' => [Error::formatError($e)]]; + return new ExecutionResult(null, [$e]); } } } diff --git a/tests/Type/IntrospectionTest.php b/tests/Type/IntrospectionTest.php index c9f28bc..1f1c0e5 100644 --- a/tests/Type/IntrospectionTest.php +++ b/tests/Type/IntrospectionTest.php @@ -1358,6 +1358,7 @@ class IntrospectionTest extends \PHPUnit_Framework_TestCase } '; $expected = [ + 'data' => null, 'errors' => [ FormattedError::create( ProvidedNonNullArguments::missingFieldArgMessage('__type', 'name', 'String!'), [new SourceLocation(3, 9)]