mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-29 00:25:17 +03:00
Refactored facade to simplify custom output formatting
This commit is contained in:
parent
c81605bfb1
commit
68fb4ceb9c
@ -25,6 +25,9 @@ class ExecutionResult
|
||||
$this->errors = $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$result = ['data' => $this->data];
|
||||
|
@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)]
|
||||
|
Loading…
Reference in New Issue
Block a user