Skip to content
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

Querying a child relation of the same entity type not possible in GraphQL #5310

Closed
NicoHaase opened this issue Jan 6, 2023 · 2 comments
Closed

Comments

@NicoHaase
Copy link
Contributor

NicoHaase commented Jan 6, 2023

API Platform version(s) affected: 3.0.8

Description
In my application, I use a parent/child relation with entities of the same type, like the following:

<?php

declare(strict_types=1);

namespace App\Entity\QualityManual;

use ApiPlatform\Metadata\GraphQl\QueryCollection;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource(
    graphQlOperations: [
        new Query(name: 'item_query'),
        new QueryCollection(name: 'collection_query'),
    ]
)]
#[ORM\Entity]
class Chapter
{
    #[ORM\Id]
    #[ORM\Column(type: 'guid', unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
    private ?string $id = null;

    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: Chapter::class, cascade: ['remove'])]
    #[ORM\OrderBy(['position' => 'ASC'])]
    private Collection $children;

    public function __construct()
    {
        $this->children  = new ArrayCollection();
    }

    public function getId(): ?string
    {
        return $this->id;
    }
}

When I try to select a single Chapter entity and the number of it's children, the result is not built properly. As far as I can debug this, it's a problem of these lines:

if ($resourceClass && $rootOperation->getClass() && $this->resourceClassResolver->isResourceClass($resourceClass) && $rootOperation->getClass() !== $resourceClass) {
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$resourceOperation = $resourceMetadataCollection->getOperation($isCollectionType ? 'collection_query' : 'item_query');
}

Due to the last condition, the $resourceOperation is not properly set to a collection operation, but to an item operaion. This results in an error in

$collection = ($this->readStage)($resourceClass, $rootClass, $operation, $resolverContext);
if (!is_iterable($collection)) {
throw new \LogicException('Collection from read stage should be iterable.');
}

as the resulting collection is not a collection, but null (even if there are entities matching this criteria).

How to reproduce

  • use the entity definition as above
  • run a query like { chapter(id: "/api/chapters/chapterId") { id, children { totalCount } } }

Additional Context

@alanpoulain
Copy link
Member

It would help if you could create a PR with a failing Behat test on the 3.0 branch.

@develth
Copy link
Contributor

develth commented Jan 10, 2023

So, you do not get an error like in #5007 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants