Skip to content

Commit bc96751

Browse files
jotweajosef.wagner
andauthored
fix(graphql): nested collection for mongo (#6174)
* fix(graphql): nested collection for mongo * introduce NoopProvider --------- Co-authored-by: josef.wagner <[email protected]>
1 parent ca6be32 commit bc96751

File tree

8 files changed

+42
-7
lines changed

8 files changed

+42
-7
lines changed

features/graphql/query.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Feature: GraphQL query support
6868
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges[0].node.name" should match "#RelatedOneToManyDummy(1|3)2#"
6969
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges[2].node.name" should match "#RelatedOneToManyDummy(1|3)2#"
7070

71-
@createSchema @!mongodb
71+
@createSchema
7272
Scenario: Retrieve embedded collections
7373
Given there are 2 multiRelationsDummy objects having each a manyToOneRelation, 2 manyToManyRelations, 3 oneToManyRelations and 4 embeddedRelations
7474
When I send the following GraphQL request:

src/GraphQl/Resolver/Factory/ResolverFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\GraphQl\Resolver\Factory;
1515

16+
use ApiPlatform\GraphQl\State\Provider\NoopProvider;
1617
use ApiPlatform\Metadata\GraphQl\Mutation;
1718
use ApiPlatform\Metadata\GraphQl\Operation;
1819
use ApiPlatform\Metadata\GraphQl\Query;
@@ -35,7 +36,7 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul
3536
// Data already fetched and normalized (field or nested resource)
3637
if ($body = $source[$info->fieldName] ?? null) {
3738
// special treatment for nested resources without a resolver/provider
38-
if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && !$operation->getProvider()) {
39+
if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && (!$operation->getProvider() || NoopProvider::class === $operation->getProvider())) {
3940
return $this->resolve($source, $args, $info, $rootClass, $operation, new ArrayPaginator($body, 0, \count($body)));
4041
}
4142

src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\GraphQl\Serializer;
1515

16+
use ApiPlatform\GraphQl\State\Provider\NoopProvider;
1617
use ApiPlatform\Metadata\ApiProperty;
1718
use ApiPlatform\Metadata\GraphQl\Query;
1819
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
@@ -113,7 +114,7 @@ protected function normalizeCollectionOfRelations(ApiProperty $propertyMetadata,
113114
{
114115
// check for nested collection
115116
$operation = $this->resourceMetadataCollectionFactory?->create($resourceClass)->getOperation(forceCollection: true, forceGraphQl: true);
116-
if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && !$operation->getProvider()) {
117+
if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && (!$operation->getProvider() || NoopProvider::class === $operation->getProvider())) {
117118
return [...$attributeValue];
118119
}
119120

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\GraphQl\State\Provider;
15+
16+
use ApiPlatform\Metadata\Operation;
17+
use ApiPlatform\State\ProviderInterface;
18+
19+
/**
20+
* No-op Provider for GraphQl.
21+
*/
22+
final class NoopProvider implements ProviderInterface
23+
{
24+
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
25+
{
26+
return null;
27+
}
28+
}

tests/Fixtures/TestBundle/Document/MultiRelationsDummy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function addOneToManyRelation(MultiRelationsRelatedDummy $relatedMultiUse
8989

9090
public function getNestedCollection(): Collection
9191
{
92-
return $this->nestedCollection;
92+
return $this->nestedCollection->map(fn ($entry) => ['name' => $entry->name]);
9393
}
9494

9595
public function setNestedCollection(Collection $nestedCollection): self
@@ -101,7 +101,7 @@ public function setNestedCollection(Collection $nestedCollection): self
101101

102102
public function getNestedPaginatedCollection(): Collection
103103
{
104-
return $this->nestedPaginatedCollection;
104+
return $this->nestedPaginatedCollection->map(fn ($entry) => ['name' => $entry->name]);
105105
}
106106

107107
public function setNestedPaginatedCollection(Collection $nestedPaginatedCollection): self

tests/Fixtures/TestBundle/Document/MultiRelationsNested.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document;
1515

16+
use ApiPlatform\GraphQl\State\Provider\NoopProvider;
1617
use ApiPlatform\Metadata\ApiResource;
1718
use ApiPlatform\Metadata\GraphQl\QueryCollection;
1819
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
1920

20-
#[ApiResource(graphQlOperations: [new QueryCollection(paginationEnabled: false, nested: true)])]
21+
#[ApiResource(graphQlOperations: [new QueryCollection(paginationEnabled: false, provider: NoopProvider::class, nested: true)])]
2122
#[ODM\EmbeddedDocument]
2223
class MultiRelationsNested
2324
{

tests/Fixtures/TestBundle/Document/MultiRelationsNestedPaginated.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document;
1515

16+
use ApiPlatform\GraphQl\State\Provider\NoopProvider;
1617
use ApiPlatform\Metadata\ApiResource;
1718
use ApiPlatform\Metadata\GraphQl\QueryCollection;
1819
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
1920

20-
#[ApiResource(graphQlOperations: [new QueryCollection(nested: true)])]
21+
#[ApiResource(graphQlOperations: [new QueryCollection(provider: NoopProvider::class, nested: true)])]
2122
#[ODM\EmbeddedDocument]
2223
class MultiRelationsNestedPaginated
2324
{

tests/Fixtures/TestBundle/Document/SoMany.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ class SoMany
2929
public $id;
3030
#[ODM\Field(nullable: true)]
3131
public $content;
32+
33+
#[ODM\ReferenceOne(targetDocument: FooDummy::class, storeAs: 'id', nullable: true)]
34+
public ?FooDummy $fooDummy;
3235
}

0 commit comments

Comments
 (0)