diff --git a/src/ResolveUtils.php b/src/ResolveUtils.php index b3aa657668..2f95d35f32 100644 --- a/src/ResolveUtils.php +++ b/src/ResolveUtils.php @@ -24,7 +24,7 @@ final class ResolveUtils public static function assertInnerReturnType(mixed $result, Type $type): void { if ($type instanceof NonNull && $result === null) { - throw TypeMismatchRuntimeException::unexpectedNullValue(); + throw TypeMismatchRuntimeException::unexpectedNullValue($type); } if ($result === null) { return; @@ -56,7 +56,7 @@ public static function assertInnerReturnType(mixed $result, Type $type): void public static function assertInnerInputType(mixed $input, Type $type): void { if ($type instanceof NonNull && $input === null) { - throw TypeMismatchRuntimeException::unexpectedNullValue(); + throw TypeMismatchRuntimeException::unexpectedNullValue($type); } if ($input === null) { return; diff --git a/src/TypeMismatchRuntimeException.php b/src/TypeMismatchRuntimeException.php index c73e323dac..d121bb4809 100644 --- a/src/TypeMismatchRuntimeException.php +++ b/src/TypeMismatchRuntimeException.php @@ -4,6 +4,8 @@ namespace TheCodingMachine\GraphQLite; +use GraphQL\Type\Definition\Type; + use function gettype; /** @@ -11,9 +13,15 @@ */ class TypeMismatchRuntimeException extends GraphQLRuntimeException { - public static function unexpectedNullValue(): self + public static function unexpectedNullValue(Type|null $expectedType = null): self { - return new self('Unexpected null value for non nullable field.'); + $expectedMessageTail = ''; + if ($expectedType !== null) { + $expectedMessageTail = ' Expected: "' . $expectedType->toString() . '"'; + // ToDo: support for NULL $expectedType should be dropped in the next major + } + + return new self('Unexpected null value for non nullable field.' . $expectedMessageTail); } public static function expectedIterable(mixed $result): self diff --git a/tests/ResolveUtilsTest.php b/tests/ResolveUtilsTest.php index ca5a1e634c..041f42d4e7 100644 --- a/tests/ResolveUtilsTest.php +++ b/tests/ResolveUtilsTest.php @@ -12,6 +12,7 @@ class ResolveUtilsTest extends TestCase public function testAssertNull(): void { $this->expectException(TypeMismatchRuntimeException::class); + $this->expectExceptionMessage('Unexpected null value for non nullable field. Expected: "String!"'); ResolveUtils::assertInnerReturnType(null, Type::nonNull(Type::string())); } @@ -30,6 +31,7 @@ public function testAssertObjectOk(): void public function testAssertInputNull(): void { $this->expectException(TypeMismatchRuntimeException::class); + $this->expectExceptionMessage('Unexpected null value for non nullable field. Expected: "String!"'); ResolveUtils::assertInnerInputType(null, Type::nonNull(Type::string())); }