Skip to content

Commit 3d8371a

Browse files
fix(graphql): use depth for nested resource class operation (#5314)
* test: add reproducer for bug 5310 * fix(graphql): use depth for nested resource class operation Co-authored-by: Alan Poulain <[email protected]>
1 parent 7fce4eb commit 3d8371a

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

features/graphql/query.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ Feature: GraphQL query support
6868
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges[0].node.name" should be equal to "RelatedOneToManyDummy12"
6969
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges[2].node.name" should be equal to "RelatedOneToManyDummy32"
7070

71+
@createSchema @!mongodb
72+
Scenario: Retrieve an item with child relation to the same resource
73+
Given there are tree dummies
74+
When I send the following GraphQL request:
75+
"""
76+
{
77+
treeDummies {
78+
edges {
79+
node {
80+
id
81+
children {
82+
totalCount
83+
}
84+
}
85+
}
86+
}
87+
}
88+
"""
89+
Then the response status code should be 200
90+
And the response should be in JSON
91+
And the header "Content-Type" should be equal to "application/json"
92+
And the JSON node "errors" should not exist
93+
And the JSON node "data.treeDummies.edges[0].node.id" should be equal to "/tree_dummies/1"
94+
And the JSON node "data.treeDummies.edges[0].node.children.totalCount" should be equal to "1"
95+
And the JSON node "data.treeDummies.edges[1].node.id" should be equal to "/tree_dummies/2"
96+
And the JSON node "data.treeDummies.edges[1].node.children.totalCount" should be equal to "0"
97+
7198
@createSchema
7299
Scenario: Retrieve a Relay Node
73100
Given there are 2 dummy objects with relatedDummy

src/GraphQl/Type/FieldsBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
262262
}
263263

264264
$resourceOperation = $rootOperation;
265-
if ($resourceClass && $rootOperation->getClass() && $this->resourceClassResolver->isResourceClass($resourceClass) && $rootOperation->getClass() !== $resourceClass) {
265+
if ($resourceClass && $depth >= 1 && $this->resourceClassResolver->isResourceClass($resourceClass)) {
266266
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
267267
$resourceOperation = $resourceMetadataCollection->getOperation($isCollectionType ? 'collection_query' : 'item_query');
268268
}

tests/Behat/DoctrineContext.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SymfonyUuidDummy;
168168
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Taxon;
169169
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ThirdLevel;
170+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\TreeDummy;
170171
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\UrlEncodedId;
171172
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\User;
172173
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\UuidIdentifierDummy;
@@ -799,6 +800,22 @@ public function thereAreMultiRelationsDummyObjectsHavingEachAManyToOneRelationMa
799800
$this->manager->flush();
800801
}
801802

803+
/**
804+
* @Given there are tree dummies
805+
*/
806+
public function thereAreTreeDummies(): void
807+
{
808+
$parentDummy = new TreeDummy();
809+
$this->manager->persist($parentDummy);
810+
811+
$childDummy = new TreeDummy();
812+
$childDummy->setParent($parentDummy);
813+
814+
$this->manager->persist($childDummy);
815+
816+
$this->manager->flush();
817+
}
818+
802819
/**
803820
* @Given there are :nb dummy objects with dummyDate
804821
* @Given there is :nb dummy object with dummyDate
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\GraphQl\Query;
18+
use ApiPlatform\Metadata\GraphQl\QueryCollection;
19+
use Doctrine\Common\Collections\ArrayCollection;
20+
use Doctrine\Common\Collections\Collection;
21+
use Doctrine\ORM\Mapping as ORM;
22+
23+
#[ApiResource(graphQlOperations: [new Query(), new QueryCollection()])]
24+
#[ORM\Entity]
25+
class TreeDummy
26+
{
27+
#[ORM\Column(type: 'integer', nullable: true)]
28+
#[ORM\Id]
29+
#[ORM\GeneratedValue(strategy: 'AUTO')]
30+
private ?int $id = null;
31+
32+
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
33+
public ?TreeDummy $parent = null;
34+
35+
/** @var Collection<int, TreeDummy> */
36+
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
37+
public Collection $children;
38+
39+
public function __construct()
40+
{
41+
$this->children = new ArrayCollection();
42+
}
43+
44+
public function getId(): ?int
45+
{
46+
return $this->id;
47+
}
48+
49+
public function getParent(): ?self
50+
{
51+
return $this->parent;
52+
}
53+
54+
public function setParent(?self $parent): void
55+
{
56+
$this->parent = $parent;
57+
}
58+
59+
public function getChildren(): Collection
60+
{
61+
return $this->children;
62+
}
63+
}

0 commit comments

Comments
 (0)