Skip to content

Commit cb7c195

Browse files
authored
✨ Improve exception message for unexpected null value - mention expected type (#762)
1 parent 5ed662c commit cb7c195

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/ResolveUtils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class ResolveUtils
2424
public static function assertInnerReturnType(mixed $result, Type $type): void
2525
{
2626
if ($type instanceof NonNull && $result === null) {
27-
throw TypeMismatchRuntimeException::unexpectedNullValue();
27+
throw TypeMismatchRuntimeException::unexpectedNullValue($type);
2828
}
2929
if ($result === null) {
3030
return;
@@ -56,7 +56,7 @@ public static function assertInnerReturnType(mixed $result, Type $type): void
5656
public static function assertInnerInputType(mixed $input, Type $type): void
5757
{
5858
if ($type instanceof NonNull && $input === null) {
59-
throw TypeMismatchRuntimeException::unexpectedNullValue();
59+
throw TypeMismatchRuntimeException::unexpectedNullValue($type);
6060
}
6161
if ($input === null) {
6262
return;

src/TypeMismatchRuntimeException.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44

55
namespace TheCodingMachine\GraphQLite;
66

7+
use GraphQL\Type\Definition\Type;
8+
79
use function gettype;
810

911
/**
1012
* An exception thrown when a resolver returns a value that is not compatible with the GraphQL type.
1113
*/
1214
class TypeMismatchRuntimeException extends GraphQLRuntimeException
1315
{
14-
public static function unexpectedNullValue(): self
16+
public static function unexpectedNullValue(Type|null $expectedType = null): self
1517
{
16-
return new self('Unexpected null value for non nullable field.');
18+
$expectedMessageTail = '';
19+
if ($expectedType !== null) {
20+
$expectedMessageTail = ' Expected: "' . $expectedType->toString() . '"';
21+
// ToDo: support for NULL $expectedType should be dropped in the next major
22+
}
23+
24+
return new self('Unexpected null value for non nullable field.' . $expectedMessageTail);
1725
}
1826

1927
public static function expectedIterable(mixed $result): self

tests/ResolveUtilsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ResolveUtilsTest extends TestCase
1212
public function testAssertNull(): void
1313
{
1414
$this->expectException(TypeMismatchRuntimeException::class);
15+
$this->expectExceptionMessage('Unexpected null value for non nullable field. Expected: "String!"');
1516
ResolveUtils::assertInnerReturnType(null, Type::nonNull(Type::string()));
1617
}
1718

@@ -30,6 +31,7 @@ public function testAssertObjectOk(): void
3031
public function testAssertInputNull(): void
3132
{
3233
$this->expectException(TypeMismatchRuntimeException::class);
34+
$this->expectExceptionMessage('Unexpected null value for non nullable field. Expected: "String!"');
3335
ResolveUtils::assertInnerInputType(null, Type::nonNull(Type::string()));
3436
}
3537

0 commit comments

Comments
 (0)