Skip to content
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
4 changes: 2 additions & 2 deletions src/ResolveUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/TypeMismatchRuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

namespace TheCodingMachine\GraphQLite;

use GraphQL\Type\Definition\Type;

use function gettype;

/**
* An exception thrown when a resolver returns a value that is not compatible with the GraphQL type.
*/
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
Expand Down
2 changes: 2 additions & 0 deletions tests/ResolveUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand All @@ -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()));
}

Expand Down
Loading