Skip to content

Commit

Permalink
fix(graphql): nested collection for mongo (#6174)
Browse files Browse the repository at this point in the history
* fix(graphql): nested collection for mongo

* introduce NoopProvider

---------

Co-authored-by: josef.wagner <[email protected]>
  • Loading branch information
jotwea and josef.wagner authored Mar 15, 2024
1 parent ca6be32 commit bc96751
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion features/graphql/query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Feature: GraphQL query support
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#"

@createSchema @!mongodb
@createSchema
Scenario: Retrieve embedded collections
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 Down
3 changes: 2 additions & 1 deletion src/GraphQl/Resolver/Factory/ResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\GraphQl\Resolver\Factory;

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

Expand Down
3 changes: 2 additions & 1 deletion src/GraphQl/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\GraphQl\Serializer;

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

Expand Down
28 changes: 28 additions & 0 deletions src/GraphQl/State/Provider/NoopProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\GraphQl\State\Provider;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;

/**
* No-op Provider for GraphQl.
*/
final class NoopProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
return null;
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/TestBundle/Document/MultiRelationsDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function addOneToManyRelation(MultiRelationsRelatedDummy $relatedMultiUse

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

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

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

public function setNestedPaginatedCollection(Collection $nestedPaginatedCollection): self
Expand Down
3 changes: 2 additions & 1 deletion tests/Fixtures/TestBundle/Document/MultiRelationsNested.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

namespace ApiPlatform\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\GraphQl\State\Provider\NoopProvider;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ApiResource(graphQlOperations: [new QueryCollection(paginationEnabled: false, nested: true)])]
#[ApiResource(graphQlOperations: [new QueryCollection(paginationEnabled: false, provider: NoopProvider::class, nested: true)])]
#[ODM\EmbeddedDocument]
class MultiRelationsNested
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

namespace ApiPlatform\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\GraphQl\State\Provider\NoopProvider;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ApiResource(graphQlOperations: [new QueryCollection(nested: true)])]
#[ApiResource(graphQlOperations: [new QueryCollection(provider: NoopProvider::class, nested: true)])]
#[ODM\EmbeddedDocument]
class MultiRelationsNestedPaginated
{
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/TestBundle/Document/SoMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ class SoMany
public $id;
#[ODM\Field(nullable: true)]
public $content;

#[ODM\ReferenceOne(targetDocument: FooDummy::class, storeAs: 'id', nullable: true)]
public ?FooDummy $fooDummy;
}

0 comments on commit bc96751

Please sign in to comment.