Skip to content

Commit

Permalink
fix(graphql): item resolver inheritance error (#5910)
Browse files Browse the repository at this point in the history
* fix(graphql): improve condition to allow inheritance

* test: graphql inheritance

---------

Co-authored-by: soyuka <soyuka@users.noreply.github.com>
  • Loading branch information
charlieandroid55 and soyuka committed Oct 27, 2023
1 parent cd6f583 commit 547c4e6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Factory/ItemResolverFactory.php
Expand Up @@ -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()));
}

Expand Down
18 changes: 18 additions & 0 deletions src/GraphQl/Tests/Fixtures/ApiResource/ChildFoo.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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
{
}
18 changes: 18 additions & 0 deletions src/GraphQl/Tests/Fixtures/ApiResource/ParentFoo.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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
{
}
20 changes: 20 additions & 0 deletions src/GraphQl/Tests/Resolver/Factory/ItemResolverFactoryTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 547c4e6

Please sign in to comment.