Refactored facade to simplify custom output formatting

This commit is contained in:
vladar 2015-11-02 20:39:51 +06:00
parent c81605bfb1
commit 68fb4ceb9c
3 changed files with 21 additions and 3 deletions

View File

@ -25,6 +25,9 @@ class ExecutionResult
$this->errors = $errors; $this->errors = $errors;
} }
/**
* @return array
*/
public function toArray() public function toArray()
{ {
$result = ['data' => $this->data]; $result = ['data' => $this->data];

View File

@ -1,6 +1,7 @@
<?php <?php
namespace GraphQL; namespace GraphQL;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor; use GraphQL\Executor\Executor;
use GraphQL\Language\Parser; use GraphQL\Language\Parser;
use GraphQL\Language\Source; use GraphQL\Language\Source;
@ -17,6 +18,19 @@ class GraphQL
* @return array * @return array
*/ */
public static function execute(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null) public static function execute(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
{
return self::executeAndReturnResult($schema, $requestString, $rootValue, $variableValues, $operationName)->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 { try {
$source = new Source($requestString ?: '', 'GraphQL request'); $source = new Source($requestString ?: '', 'GraphQL request');
@ -24,12 +38,12 @@ class GraphQL
$validationErrors = DocumentValidator::validate($schema, $documentAST); $validationErrors = DocumentValidator::validate($schema, $documentAST);
if (!empty($validationErrors)) { if (!empty($validationErrors)) {
return ['errors' => array_map(['GraphQL\Error', 'formatError'], $validationErrors)]; return new ExecutionResult(null, $validationErrors);
} else { } else {
return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName)->toArray(); return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName);
} }
} catch (Error $e) { } catch (Error $e) {
return ['errors' => [Error::formatError($e)]]; return new ExecutionResult(null, [$e]);
} }
} }
} }

View File

@ -1358,6 +1358,7 @@ class IntrospectionTest extends \PHPUnit_Framework_TestCase
} }
'; ';
$expected = [ $expected = [
'data' => null,
'errors' => [ 'errors' => [
FormattedError::create( FormattedError::create(
ProvidedNonNullArguments::missingFieldArgMessage('__type', 'name', 'String!'), [new SourceLocation(3, 9)] ProvidedNonNullArguments::missingFieldArgMessage('__type', 'name', 'String!'), [new SourceLocation(3, 9)]