Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add errors key when empty #766

Merged
merged 4 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Executor/ExecutionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,15 @@ public function toArray(int $debug = DebugFlag::NONE) : array
return array_map($formatter, $errors);
};

$result['errors'] = $errorsHandler(
$handledErrors = $errorsHandler(
$this->errors,
FormattedError::prepareFormatter($this->errorFormatter, $debug)
);

// While we know that there were errors initially, they might have been discarded
if ($handledErrors !== []) {
$result['errors'] = $handledErrors;
}
Comment on lines +150 to +153

This comment was marked as resolved.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling the errors could also mean to translate the messages, format them nicely, whatever!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see :D Yeah then I suppose it makes sense 🙂

}

if ($this->data !== null) {
Expand Down
19 changes: 16 additions & 3 deletions tests/Executor/ExecutionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GraphQL\Tests\Executor;

use GraphQL\Error\Error;
use GraphQL\Executor\ExecutionResult;
use PHPUnit\Framework\TestCase;

Expand All @@ -13,17 +14,29 @@ public function testToArrayWithoutExtensions() : void
{
$executionResult = new ExecutionResult();

self::assertEquals([], $executionResult->toArray());
self::assertSame([], $executionResult->toArray());
}

public function testToArrayExtensions() : void
{
$executionResult = new ExecutionResult(null, [], ['foo' => 'bar']);

self::assertEquals(['extensions' => ['foo' => 'bar']], $executionResult->toArray());
self::assertSame(['extensions' => ['foo' => 'bar']], $executionResult->toArray());

$executionResult->extensions = ['bar' => 'foo'];

self::assertEquals(['extensions' => ['bar' => 'foo']], $executionResult->toArray());
self::assertSame(['extensions' => ['bar' => 'foo']], $executionResult->toArray());
}

public function testNoEmptyErrors() : void
{
$executionResult = new ExecutionResult(null, [new Error()]);
$executionResult->setErrorsHandler(
static function () : array {
return [];
}
);

self::assertSame([], $executionResult->toArray());
}
}