diff --git a/src/GraphQl/Resolver/Factory/ItemResolverFactory.php b/src/GraphQl/Resolver/Factory/ItemResolverFactory.php index a08f909bf31..28169adb6ff 100644 --- a/src/GraphQl/Resolver/Factory/ItemResolverFactory.php +++ b/src/GraphQl/Resolver/Factory/ItemResolverFactory.php @@ -106,7 +106,7 @@ private function getResourceClass(?object $item, ?string $resourceClass, string return $itemClass; } - if ($resourceClass !== $itemClass) { + if ($resourceClass !== $itemClass && !$item instanceof $resourceClass) { throw new \UnexpectedValueException(sprintf($errorMessage, (new \ReflectionClass($resourceClass))->getShortName(), (new \ReflectionClass($itemClass))->getShortName())); } diff --git a/src/GraphQl/Tests/Fixtures/ApiResource/ChildFoo.php b/src/GraphQl/Tests/Fixtures/ApiResource/ChildFoo.php new file mode 100644 index 00000000000..79d678b7c06 --- /dev/null +++ b/src/GraphQl/Tests/Fixtures/ApiResource/ChildFoo.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\GraphQl\Tests\Fixtures\ApiResource; + +class ChildFoo extends ParentFoo +{ +} diff --git a/src/GraphQl/Tests/Fixtures/ApiResource/ParentFoo.php b/src/GraphQl/Tests/Fixtures/ApiResource/ParentFoo.php new file mode 100644 index 00000000000..37ec3e5e39d --- /dev/null +++ b/src/GraphQl/Tests/Fixtures/ApiResource/ParentFoo.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\GraphQl\Tests\Fixtures\ApiResource; + +class ParentFoo +{ +} diff --git a/src/GraphQl/Tests/Resolver/Factory/ItemResolverFactoryTest.php b/src/GraphQl/Tests/Resolver/Factory/ItemResolverFactoryTest.php index c57e49f218b..7ab86f63f5f 100644 --- a/src/GraphQl/Tests/Resolver/Factory/ItemResolverFactoryTest.php +++ b/src/GraphQl/Tests/Resolver/Factory/ItemResolverFactoryTest.php @@ -18,7 +18,9 @@ use ApiPlatform\GraphQl\Resolver\Stage\SecurityPostDenormalizeStageInterface; use ApiPlatform\GraphQl\Resolver\Stage\SecurityStageInterface; use ApiPlatform\GraphQl\Resolver\Stage\SerializeStageInterface; +use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\ChildFoo; use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\Dummy; +use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\ParentFoo; use ApiPlatform\Metadata\GraphQl\Query; use GraphQL\Type\Definition\ResolveInfo; use PHPUnit\Framework\TestCase; @@ -245,4 +247,22 @@ public function testResolveCustomBadItem(): void ($this->itemResolverFactory)($resourceClass, $rootClass, $operation)($source, $args, null, $info); } + + public function testResolveInheritedClass(): void + { + $resourceClass = ParentFoo::class; + $rootClass = $resourceClass; + $operationName = 'custom_query'; + $operation = (new Query())->withName($operationName); + $source = ['source']; + $args = ['args']; + $info = $this->prophesize(ResolveInfo::class)->reveal(); + $info->fieldName = 'field'; + $resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => false]; + + $readStageItem = new ChildFoo(); + $this->readStageProphecy->__invoke($resourceClass, $rootClass, $operation, $resolverContext)->shouldBeCalled()->willReturn($readStageItem); + + ($this->itemResolverFactory)($resourceClass, $rootClass, $operation)($source, $args, null, $info); + } }