Skip to content

Commit 68fb4ce

Browse files
committed
Refactored facade to simplify custom output formatting
1 parent c81605b commit 68fb4ce

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/Executor/ExecutionResult.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function __construct(array $data = null, array $errors = [])
2525
$this->errors = $errors;
2626
}
2727

28+
/**
29+
* @return array
30+
*/
2831
public function toArray()
2932
{
3033
$result = ['data' => $this->data];

src/GraphQL.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace GraphQL;
33

4+
use GraphQL\Executor\ExecutionResult;
45
use GraphQL\Executor\Executor;
56
use GraphQL\Language\Parser;
67
use GraphQL\Language\Source;
@@ -17,19 +18,32 @@ class GraphQL
1718
* @return array
1819
*/
1920
public static function execute(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
21+
{
22+
return self::executeAndReturnResult($schema, $requestString, $rootValue, $variableValues, $operationName)->toArray();
23+
}
24+
25+
/**
26+
* @param Schema $schema
27+
* @param $requestString
28+
* @param null $rootValue
29+
* @param null $variableValues
30+
* @param null $operationName
31+
* @return array|ExecutionResult
32+
*/
33+
public static function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
2034
{
2135
try {
2236
$source = new Source($requestString ?: '', 'GraphQL request');
2337
$documentAST = Parser::parse($source);
2438
$validationErrors = DocumentValidator::validate($schema, $documentAST);
2539

2640
if (!empty($validationErrors)) {
27-
return ['errors' => array_map(['GraphQL\Error', 'formatError'], $validationErrors)];
41+
return new ExecutionResult(null, $validationErrors);
2842
} else {
29-
return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName)->toArray();
43+
return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName);
3044
}
3145
} catch (Error $e) {
32-
return ['errors' => [Error::formatError($e)]];
46+
return new ExecutionResult(null, [$e]);
3347
}
3448
}
3549
}

tests/Type/IntrospectionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ public function testFailsAsExpectedOnThe__typeRootFieldWithoutAnArg()
13581358
}
13591359
';
13601360
$expected = [
1361+
'data' => null,
13611362
'errors' => [
13621363
FormattedError::create(
13631364
ProvidedNonNullArguments::missingFieldArgMessage('__type', 'name', 'String!'), [new SourceLocation(3, 9)]

0 commit comments

Comments
 (0)