Skip to content

Commit

Permalink
Test error handling with multiple top-level fields
Browse files Browse the repository at this point in the history
Resolves #2418
  • Loading branch information
spawnia committed Jun 29, 2023
1 parent 0ea0245 commit 2ecc00c
Showing 1 changed file with 68 additions and 10 deletions.
78 changes: 68 additions & 10 deletions tests/Integration/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public function testIgnoresInvalidJSONVariables(): void
public function testHandlesErrorInResolver(): void
{
$message = 'foo';
$this->mockResolver()
->willThrowException(new Error($message));
$this->mockResolver(fn () => throw new Error($message));

$this->schema = /** @lang GraphQL */ '
type Query {
Expand Down Expand Up @@ -94,8 +93,7 @@ public function testRethrowsInternalExceptions(): void
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.debug', DebugFlag::INCLUDE_DEBUG_MESSAGE);

$this->mockResolver()
->willThrowException(new \Exception('foo'));
$this->mockResolver(fn () => throw new \Exception('foo'));

$this->schema = /** @lang GraphQL */ '
type Query {
Expand Down Expand Up @@ -146,6 +144,69 @@ public function testReturnsMultipleErrors(): void
->assertGraphQLErrorMessage('Field TestInput.integer of required type Int! was not provided.');
}

public function testReturnsPartialDataIfNullableFieldFails(): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.debug', DebugFlag::INCLUDE_DEBUG_MESSAGE);

$successValue = 1;
$this->mockResolver($successValue, 'success');

$error = new \Exception('fail');
$this->mockResolver(fn () => throw $error, 'fail');

$this->schema = /** @lang GraphQL */ '
type Query {
success: Int! @mock(key: "success")
fail: Int @mock(key: "fail")
}
';

$this->graphQL(/** @lang GraphQL */ '
{
success
fail
}
')
->assertStatus(200)
->assertJson([
'data' => [
'success' => $successValue,
'fail' => null,
],
])
->assertGraphQLError($error);
}

public function testReturnsNoDataIfNonNullableFieldFails(): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.debug', DebugFlag::INCLUDE_DEBUG_MESSAGE);

$successValue = 1;
$this->mockResolver($successValue, 'success');

$error = new \Exception('fail');
$this->mockResolver(fn () => throw $error, 'fail');

$this->schema = /** @lang GraphQL */ '
type Query {
success: Int! @mock(key: "success")
fail: Int! @mock(key: "fail")
}
';

$this->graphQL(/** @lang GraphQL */ '
{
success
fail
}
')
->assertStatus(200)
->assertJsonMissingPath('data')
->assertGraphQLError($error);
}

public function testUnknownTypeInVariableDefinition(): void
{
$this->schema = /** @lang GraphQL */ '
Expand All @@ -170,8 +231,7 @@ public function testAssertGraphQLDebugMessage(): void

$message = 'foo';

$this->mockResolver()
->willThrowException(new \Exception($message));
$this->mockResolver(fn () => throw new \Exception($message));

$this->schema = /** @lang GraphQL */ '
type Query {
Expand All @@ -195,8 +255,7 @@ public function testAssertGraphQLErrorClientSafe(): void
{
$error = new Error('foo');

$this->mockResolver()
->willThrowException($error);
$this->mockResolver(fn () => throw $error);

$this->schema = /** @lang GraphQL */ '
type Query {
Expand All @@ -221,8 +280,7 @@ public function testAssertGraphQLErrorNonClientSafe(): void

$exception = new \Exception('foo');

$this->mockResolver()
->willThrowException($exception);
$this->mockResolver(fn () => throw $exception);

$this->schema = /** @lang GraphQL */ '
type Query {
Expand Down

0 comments on commit 2ecc00c

Please sign in to comment.