Skip to content

Commit

Permalink
feat(graphql): test for nested collection
Browse files Browse the repository at this point in the history
  • Loading branch information
josef.wagner committed Dec 12, 2023
1 parent 44aa9c6 commit 506cb81
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
9 changes: 8 additions & 1 deletion features/graphql/query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Feature: GraphQL query support

@createSchema
Scenario: Retrieve an item with different relations to the same resource
Given there are 2 multiRelationsDummy objects having each a manyToOneRelation, 2 manyToManyRelations and 3 oneToManyRelations
Given there are 2 multiRelationsDummy objects having each a manyToOneRelation, 2 manyToManyRelations, 3 oneToManyRelations and 4 embeddedRelations
When I send the following GraphQL request:
"""
{
Expand All @@ -49,6 +49,9 @@ Feature: GraphQL query support
}
}
}
nestedCollection {
name
}
}
}
"""
Expand All @@ -67,6 +70,10 @@ Feature: GraphQL query support
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges[1].node.id" should not be null
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges[0].node.name" should match "#RelatedOneToManyDummy(1|3)2#"
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges[2].node.name" should match "#RelatedOneToManyDummy(1|3)2#"
And the JSON node "data.multiRelationsDummy.nestedCollection[0].name" should be equal to "NestedDummy1"
And the JSON node "data.multiRelationsDummy.nestedCollection[1].name" should be equal to "NestedDummy2"
And the JSON node "data.multiRelationsDummy.nestedCollection[2].name" should be equal to "NestedDummy3"
And the JSON node "data.multiRelationsDummy.nestedCollection[3].name" should be equal to "NestedDummy4"

@createSchema @!mongodb
Scenario: Retrieve an item with child relation to the same resource
Expand Down
19 changes: 17 additions & 2 deletions tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Document\LinkHandledDummy as LinkHandledDummyDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MaxDepthDummy as MaxDepthDummyDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MultiRelationsDummy as MultiRelationsDummyDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MultiRelationsNested as MultiRelationsNestedDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MultiRelationsRelatedDummy as MultiRelationsRelatedDummyDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MusicGroup as MusicGroupDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\NetworkPathDummy as NetworkPathDummyDocument;
Expand Down Expand Up @@ -157,6 +158,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\LinkHandledDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MultiRelationsDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MultiRelationsNested;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MultiRelationsRelatedDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MusicGroup;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\NetworkPathDummy;
Expand Down Expand Up @@ -801,9 +803,9 @@ public function thereAreDummyObjectsWithRelatedDummies(int $nb, int $nbrelated):
}

/**
* @Given there are :nb multiRelationsDummy objects having each a manyToOneRelation, :nbmtmr manyToManyRelations and :nbotmr oneToManyRelations
* @Given there are :nb multiRelationsDummy objects having each a manyToOneRelation, :nbmtmr manyToManyRelations, :nbotmr oneToManyRelations and :nber embeddedRelations
*/
public function thereAreMultiRelationsDummyObjectsHavingEachAManyToOneRelationManyToManyRelationsAndOneToManyRelations(int $nb, int $nbmtmr, int $nbotmr): void
public function thereAreMultiRelationsDummyObjectsHavingEachAManyToOneRelationManyToManyRelationsOneToManyRelationsAndEmbeddedRelations(int $nb, int $nbmtmr, int $nbotmr, int $nber): void
{
for ($i = 1; $i <= $nb; ++$i) {
$relatedDummy = $this->buildMultiRelationsRelatedDummy();
Expand All @@ -830,6 +832,14 @@ public function thereAreMultiRelationsDummyObjectsHavingEachAManyToOneRelationMa
$dummy->addOneToManyRelation($oneToManyItem);
}

$embedded = new ArrayCollection();
for ($j = 1; $j <= $nber; ++$j) {
$embeddedItem = $this->buildMultiRelationsNested();
$embeddedItem->name = 'NestedDummy'.$j;
$embedded->add($embeddedItem);
}
$dummy->setNestedCollection($embedded);

$this->manager->persist($relatedDummy);
$this->manager->persist($dummy);
}
Expand Down Expand Up @@ -2599,6 +2609,11 @@ private function buildMultiRelationsRelatedDummy(): MultiRelationsRelatedDummy|M
return $this->isOrm() ? new MultiRelationsRelatedDummy() : new MultiRelationsRelatedDummyDocument();
}

private function buildMultiRelationsNested(): MultiRelationsNested|MultiRelationsNestedDocument
{
return $this->isOrm() ? new MultiRelationsNested() : new MultiRelationsNestedDocument();
}

private function buildMusicGroup(): MusicGroup|MusicGroupDocument
{
return $this->isOrm() ? new MusicGroup() : new MusicGroupDocument();
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/TestBundle/Document/MultiRelationsDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ class MultiRelationsDummy
#[ODM\ReferenceMany(targetDocument: MultiRelationsRelatedDummy::class, mappedBy: 'oneToManyRelation', storeAs: 'id')]
public Collection $oneToManyRelations;

/** @var array<MultiRelationsNested> */
#[ODM\EmbedMany]
private array $nestedCollection;

public function __construct()
{
$this->manyToManyRelations = new ArrayCollection();
$this->oneToManyRelations = new ArrayCollection();
$this->nestedCollection = [];
}

public function getId(): ?int
Expand All @@ -76,4 +81,16 @@ public function addOneToManyRelation(MultiRelationsRelatedDummy $relatedMultiUse
{
$this->oneToManyRelations->add($relatedMultiUsedDummy);
}

public function getNestedCollection(): Collection
{
return new ArrayCollection($this->nestedCollection);
}

public function setNestedCollection(Collection $nestedCollection): self
{
$this->nestedCollection = $nestedCollection->toArray();

return $this;
}
}
25 changes: 25 additions & 0 deletions tests/Fixtures/TestBundle/Document/MultiRelationsNested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ApiResource(graphQlOperations: [new QueryCollection(paginationEnabled: false, nested: true)])]
class MultiRelationsNested
{
public ?string $name;
}
17 changes: 17 additions & 0 deletions tests/Fixtures/TestBundle/Entity/MultiRelationsDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ class MultiRelationsDummy
#[ORM\OneToMany(targetEntity: MultiRelationsRelatedDummy::class, mappedBy: 'oneToManyRelation')]
public Collection $oneToManyRelations;

/** @var array<MultiRelationsNested> */
#[ORM\Column(type: 'json')]
private array $nestedCollection;

public function __construct()
{
$this->manyToManyRelations = new ArrayCollection();
$this->oneToManyRelations = new ArrayCollection();
$this->nestedCollection = [];
}

public function getId(): ?int
Expand All @@ -78,4 +83,16 @@ public function addOneToManyRelation(MultiRelationsRelatedDummy $relatedMultiUse
{
$this->oneToManyRelations->add($relatedMultiUsedDummy);
}

public function getNestedCollection(): Collection
{
return new ArrayCollection($this->nestedCollection);
}

public function setNestedCollection(Collection $nestedCollection): self
{
$this->nestedCollection = $nestedCollection->toArray();

return $this;
}
}
25 changes: 25 additions & 0 deletions tests/Fixtures/TestBundle/Entity/MultiRelationsNested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use Doctrine\ORM\Mapping as ORM;

#[ApiResource(graphQlOperations: [new QueryCollection(paginationEnabled: false, nested: true)])]
class MultiRelationsNested
{
public ?string $name;
}

0 comments on commit 506cb81

Please sign in to comment.