Skip to content

Commit 9b0cfdb

Browse files
committed
Not found entity will throw a UserError, not an Error
This is because the error category "graphql" is reserved for errors produced by query parsing or validation. We should never have used it. So now the error will be of category "user". And it becomes easier to difference between broken GraphQL syntax and merley invalid values.
1 parent 8c9f64d commit 9b0cfdb

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/Definition/EntityID.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace GraphQL\Doctrine\Definition;
66

77
use Doctrine\ORM\EntityManager;
8-
use GraphQL\Error\Error;
8+
use GraphQL\Error\UserError;
99

1010
/**
1111
* A object used to fetch the entity from DB on demand.
@@ -47,7 +47,7 @@ public function getEntity(): object
4747
{
4848
$entity = $this->entityManager->getRepository($this->className)->find($this->id);
4949
if (!$entity) {
50-
throw new Error('Entity not found for class `' . $this->className . '` and ID `' . $this->id . '`.');
50+
throw new UserError('Entity not found for class `' . $this->className . '` and ID `' . $this->id . '`.');
5151
}
5252

5353
return $entity;

tests/Definition/EntityIDTypeTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GraphQL\Doctrine\Definition\EntityIDType;
88
use GraphQL\Error\Error;
9+
use GraphQL\Error\UserError;
910
use GraphQL\Language\AST\BooleanValueNode;
1011
use GraphQL\Language\AST\StringValueNode;
1112
use GraphQLTests\Doctrine\Blog\Model\User;
@@ -72,15 +73,19 @@ public function testCanGetEntityFromRepositoryWhenReadingLiteral(): void
7273

7374
public function testNonExistingEntityThrowErrorWhenReadingLiteral(): void
7475
{
75-
$this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
7676
$ast = new StringValueNode(['value' => 'non-existing-id']);
77-
$this->type->parseLiteral($ast)->getEntity();
77+
$value = $this->type->parseLiteral($ast);
78+
79+
$this->expectException(UserError::class);
80+
$this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
81+
$value->getEntity();
7882
}
7983

8084
public function testWillThrowIfParsingInvalidLiteralValue(): void
8185
{
82-
$this->expectException(Error::class);
8386
$ast = new BooleanValueNode(['value' => false]);
87+
88+
$this->expectException(Error::class);
8489
$this->type->parseLiteral($ast);
8590
}
8691

tests/InputTypesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public function testNamespaceSupportInput(): void
4949

5050
public function testInputWithoutTypeMustThrow(): void
5151
{
52-
$this->expectExceptionMessage('Could not find type for parameter `$bar` for method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeInput::setFoo()`. Either type hint the parameter, or specify the type with `#[API\Input]` attribute.');
5352
$type = $this->types->getInput(Blog\Model\Special\NoTypeInput::class);
53+
54+
$this->expectExceptionMessage('Could not find type for parameter `$bar` for method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeInput::setFoo()`. Either type hint the parameter, or specify the type with `#[API\Input]` attribute.');
5455
$type->getFields();
5556
}
5657
}

tests/OutputTypesTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,17 @@ public function testNamespaceSupportOutput(): void
6363

6464
public function testFieldWithoutTypeMustThrow(): void
6565
{
66-
$this->expectExceptionMessage('Could not find type for method `GraphQLTests\Doctrine\Blog\Model\Special\NoType::getWithoutTypeHint()`. Either type hint the return value, or specify the type with `#[API\Field]` attribute.');
6766
$type = $this->types->getOutput(Blog\Model\Special\NoType::class);
67+
68+
$this->expectExceptionMessage('Could not find type for method `GraphQLTests\Doctrine\Blog\Model\Special\NoType::getWithoutTypeHint()`. Either type hint the return value, or specify the type with `#[API\Field]` attribute.');
6869
$type->getFields();
6970
}
7071

7172
public function testFieldReturningCollectionWithoutTypeMustThrow(): void
7273
{
73-
$this->expectExceptionMessage('The method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeCollection::getFoos()` is type hinted with a return type of `Doctrine\Common\Collections\Collection`, but the entity contained in that collection could not be automatically detected. Either fix the type hint, fix the doctrine mapping, or specify the type with `#[API\Field]` attribute.');
7474
$type = $this->types->getOutput(Blog\Model\Special\NoTypeCollection::class);
75+
76+
$this->expectExceptionMessage('The method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeCollection::getFoos()` is type hinted with a return type of `Doctrine\Common\Collections\Collection`, but the entity contained in that collection could not be automatically detected. Either fix the type hint, fix the doctrine mapping, or specify the type with `#[API\Field]` attribute.');
7577
$type->getFields();
7678
}
7779

@@ -83,22 +85,25 @@ public function testCannotGetInvalidType(): void
8385

8486
public function testArgumentWithoutTypeMustThrow(): void
8587
{
86-
$this->expectExceptionMessage('Could not find type for parameter `$bar` for method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeArgument::getFoo()`. Either type hint the parameter, or specify the type with `#[API\Argument]` attribute.');
8788
$type = $this->types->getOutput(Blog\Model\Special\NoTypeArgument::class);
89+
90+
$this->expectExceptionMessage('Could not find type for parameter `$bar` for method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeArgument::getFoo()`. Either type hint the parameter, or specify the type with `#[API\Argument]` attribute.');
8891
$type->getFields();
8992
}
9093

9194
public function testFieldWithArrayArgumentMustThrow(): void
9295
{
93-
$this->expectExceptionMessage('The parameter `$arg1` on method `GraphQLTests\Doctrine\Blog\Model\Special\ArrayArgument::getWithParams()` is type hinted as `array` and is not overridden via `#[API\Argument]` attribute. Either change the type hint or specify the type with `#[API\Argument]` attribute.');
9496
$type = $this->types->getOutput(Blog\Model\Special\ArrayArgument::class);
97+
98+
$this->expectExceptionMessage('The parameter `$arg1` on method `GraphQLTests\Doctrine\Blog\Model\Special\ArrayArgument::getWithParams()` is type hinted as `array` and is not overridden via `#[API\Argument]` attribute. Either change the type hint or specify the type with `#[API\Argument]` attribute.');
9599
$type->getFields();
96100
}
97101

98102
public function testFieldWithObjectTypeArgumentMustThrow(): void
99103
{
100-
$this->expectExceptionMessage('Type for parameter `$user` for method `GraphQLTests\Doctrine\Blog\Model\Special\ObjectTypeArgument::getWithParams()` must be an instance of `GraphQL\Type\Definition\InputType`, but was `GraphQL\Type\Definition\ObjectType`. Use `#[API\Argument]` attribute to specify a custom InputType.');
101104
$type = $this->types->getOutput(Blog\Model\Special\ObjectTypeArgument::class);
105+
106+
$this->expectExceptionMessage('Type for parameter `$user` for method `GraphQLTests\Doctrine\Blog\Model\Special\ObjectTypeArgument::getWithParams()` must be an instance of `GraphQL\Type\Definition\InputType`, but was `GraphQL\Type\Definition\ObjectType`. Use `#[API\Argument]` attribute to specify a custom InputType.');
102107
$type->getFields();
103108
}
104109

0 commit comments

Comments
 (0)