Skip to content

Commit 547c4e6

Browse files
fix(graphql): item resolver inheritance error (#5910)
* fix(graphql): improve condition to allow inheritance * test: graphql inheritance --------- Co-authored-by: soyuka <[email protected]>
1 parent cd6f583 commit 547c4e6

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

src/GraphQl/Resolver/Factory/ItemResolverFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private function getResourceClass(?object $item, ?string $resourceClass, string
106106
return $itemClass;
107107
}
108108

109-
if ($resourceClass !== $itemClass) {
109+
if ($resourceClass !== $itemClass && !$item instanceof $resourceClass) {
110110
throw new \UnexpectedValueException(sprintf($errorMessage, (new \ReflectionClass($resourceClass))->getShortName(), (new \ReflectionClass($itemClass))->getShortName()));
111111
}
112112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\GraphQl\Tests\Fixtures\ApiResource;
15+
16+
class ChildFoo extends ParentFoo
17+
{
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\GraphQl\Tests\Fixtures\ApiResource;
15+
16+
class ParentFoo
17+
{
18+
}

src/GraphQl/Tests/Resolver/Factory/ItemResolverFactoryTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use ApiPlatform\GraphQl\Resolver\Stage\SecurityPostDenormalizeStageInterface;
1919
use ApiPlatform\GraphQl\Resolver\Stage\SecurityStageInterface;
2020
use ApiPlatform\GraphQl\Resolver\Stage\SerializeStageInterface;
21+
use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\ChildFoo;
2122
use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\Dummy;
23+
use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\ParentFoo;
2224
use ApiPlatform\Metadata\GraphQl\Query;
2325
use GraphQL\Type\Definition\ResolveInfo;
2426
use PHPUnit\Framework\TestCase;
@@ -245,4 +247,22 @@ public function testResolveCustomBadItem(): void
245247

246248
($this->itemResolverFactory)($resourceClass, $rootClass, $operation)($source, $args, null, $info);
247249
}
250+
251+
public function testResolveInheritedClass(): void
252+
{
253+
$resourceClass = ParentFoo::class;
254+
$rootClass = $resourceClass;
255+
$operationName = 'custom_query';
256+
$operation = (new Query())->withName($operationName);
257+
$source = ['source'];
258+
$args = ['args'];
259+
$info = $this->prophesize(ResolveInfo::class)->reveal();
260+
$info->fieldName = 'field';
261+
$resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => false];
262+
263+
$readStageItem = new ChildFoo();
264+
$this->readStageProphecy->__invoke($resourceClass, $rootClass, $operation, $resolverContext)->shouldBeCalled()->willReturn($readStageItem);
265+
266+
($this->itemResolverFactory)($resourceClass, $rootClass, $operation)($source, $args, null, $info);
267+
}
248268
}

0 commit comments

Comments
 (0)