From 34fcda9b503bc928e7e9e1ad53756bfc96f73b11 Mon Sep 17 00:00:00 2001 From: charlieandroid55 Date: Fri, 20 Oct 2023 09:43:24 -0400 Subject: [PATCH 1/2] fix(graphql): improve condition to allow inheritance --- src/GraphQl/Resolver/Factory/ItemResolverFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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())); } From d74b318d109938ab850978deaf2c9186e10f5198 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 27 Oct 2023 11:11:11 +0200 Subject: [PATCH 2/2] test: graphql inheritance --- .../Tests/Fixtures/ApiResource/ChildFoo.php | 18 +++++++++++++++++ .../Tests/Fixtures/ApiResource/ParentFoo.php | 18 +++++++++++++++++ .../Factory/ItemResolverFactoryTest.php | 20 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/GraphQl/Tests/Fixtures/ApiResource/ChildFoo.php create mode 100644 src/GraphQl/Tests/Fixtures/ApiResource/ParentFoo.php 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); + } }