Skip to content

fix(graphql): item resolver inheritance error #5910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Factory/ItemResolverFactory.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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
Original file line number Diff line number Diff line change
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);
}
}