Skip to content

Commit

Permalink
fix(graphql): check inheritance in ResolverProvider (#6314)
Browse files Browse the repository at this point in the history
* fix(graphql): check inheritance in ResolverProvider

* fix(graphql): add unit test for interitance case

---------

Co-authored-by: josef.wagner <josef.wagner@hf-solutions.co>
  • Loading branch information
jotwea and josef.wagner authored Apr 18, 2024
1 parent a59fbee commit 50c738c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/GraphQl/State/Provider/ResolverProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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
17 changes: 17 additions & 0 deletions src/GraphQl/Tests/State/Provider/ResolverProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
use ApiPlatform\GraphQl\State\Provider\ResolverProvider;
use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\ChildFoo;
use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\ParentFoo;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use ApiPlatform\State\ProviderInterface;
use PHPUnit\Framework\TestCase;
Expand All @@ -35,4 +38,18 @@ public function testProvide(): void
$provider = new ResolverProvider($decorated, $resolverLocator);
$this->assertEquals($res, $provider->provide($operation, [], $context));
}

public function testProvideInheritedClass(): void
{
$res = new ChildFoo();
$operation = new Query(class: ParentFoo::class, resolver: 'foo');
$context = [];
$decorated = $this->createMock(ProviderInterface::class);
$resolverMock = $this->createMock(QueryItemResolverInterface::class);
$resolverMock->expects($this->once())->method('__invoke')->willReturn($res);
$resolverLocator = $this->createMock(ContainerInterface::class);
$resolverLocator->expects($this->once())->method('get')->with('foo')->willReturn($resolverMock);
$provider = new ResolverProvider($decorated, $resolverLocator);
$this->assertEquals($res, $provider->provide($operation, [], $context));
}
}

0 comments on commit 50c738c

Please sign in to comment.