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;
}
/**
* @return array
*/
public function toArray()
{
$result = ['data' => $this->data];

View File

@ -1,6 +1,7 @@
<?php
namespace GraphQL;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
@ -17,6 +18,19 @@ class GraphQL
* @return array
*/
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 {
$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]);
}
}
}

View File

@ -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)]