Skip to content

Commit 2999d9e

Browse files
authored
fix: return null instead of exception for GraphQL Query operation (#6118)
1 parent 0b50bf6 commit 2999d9e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/GraphQl/State/Provider/ReadProvider.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
namespace ApiPlatform\GraphQl\State\Provider;
1515

16-
use ApiPlatform\Exception\ItemNotFoundException;
1716
use ApiPlatform\GraphQl\Resolver\Util\IdentifierTrait;
1817
use ApiPlatform\GraphQl\Serializer\ItemNormalizer;
1918
use ApiPlatform\GraphQl\Serializer\SerializerContextBuilderInterface;
2019
use ApiPlatform\GraphQl\Util\ArrayTrait;
2120
use ApiPlatform\Metadata\CollectionOperationInterface;
21+
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
2222
use ApiPlatform\Metadata\GraphQl\Mutation;
2323
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
2424
use ApiPlatform\Metadata\GraphQl\QueryCollection;
@@ -78,6 +78,10 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
7878
}
7979
}
8080

81+
if (null === $item) {
82+
return $item;
83+
}
84+
8185
if (!\is_object($item)) {
8286
throw new \LogicException('Item from read provider should be a nullable object.');
8387
}

src/GraphQl/Tests/State/Provider/ReadProviderTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\GraphQl\Serializer\SerializerContextBuilderInterface;
1717
use ApiPlatform\GraphQl\State\Provider\ReadProvider;
18+
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
1819
use ApiPlatform\Metadata\GraphQl\Query;
1920
use ApiPlatform\Metadata\GraphQl\QueryCollection;
2021
use ApiPlatform\Metadata\IriConverterInterface;
@@ -36,6 +37,26 @@ public function testProvide(): void
3637
$provider->provide($operation, [], $context);
3738
}
3839

40+
/**
41+
* Tests that provider returns null if resource is not found.
42+
*
43+
* @see https://github.com/api-platform/core/issues/6072
44+
*/
45+
public function testProvideNotExistedResource(): void
46+
{
47+
$context = ['args' => ['id' => '/dummy/1']];
48+
$operation = new Query(class: 'dummy');
49+
$decorated = $this->createMock(ProviderInterface::class);
50+
$iriConverter = $this->createMock(IriConverterInterface::class);
51+
$iriConverter->expects($this->once())->method('getResourceFromIri')->with('/dummy/1');
52+
$iriConverter->method('getResourceFromIri')->willThrowException(new ItemNotFoundException());
53+
$serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
54+
$provider = new ReadProvider($decorated, $iriConverter, $serializerContextBuilder, '.');
55+
$result = $provider->provide($operation, [], $context);
56+
57+
$this->assertNull($result);
58+
}
59+
3960
public function testProvideCollection(): void
4061
{
4162
$info = $this->createMock(ResolveInfo::class);

0 commit comments

Comments
 (0)