Skip to content

Commit

Permalink
fix(graphql): nested collection for mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
josef.wagner committed Feb 22, 2024
1 parent 7694219 commit 0f68456
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 54 deletions.
48 changes: 0 additions & 48 deletions features/graphql/query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,6 @@ Feature: GraphQL query support
And the JSON node "data.dummy.name_converted" should be equal to "Converted 1"

@createSchema
Scenario: Retrieve an item with different relations to the same resource
Given there are 2 multiRelationsDummy objects having each a manyToOneRelation, 2 manyToManyRelations, 3 oneToManyRelations and 4 embeddedRelations
When I send the following GraphQL request:
"""
{
multiRelationsDummy(id: "/multi_relations_dummies/2") {
id
name
manyToOneRelation {
id
name
}
manyToManyRelations {
edges{
node {
id
name
}
}
}
oneToManyRelations {
edges{
node {
id
name
}
}
}
}
}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.multiRelationsDummy.id" should be equal to "/multi_relations_dummies/2"
And the JSON node "data.multiRelationsDummy.name" should be equal to "Dummy #2"
And the JSON node "data.multiRelationsDummy.manyToOneRelation.id" should not be null
And the JSON node "data.multiRelationsDummy.manyToOneRelation.name" should be equal to "RelatedManyToOneDummy #2"
And the JSON node "data.multiRelationsDummy.manyToManyRelations.edges" should have 2 element
And the JSON node "data.multiRelationsDummy.manyToManyRelations.edges[1].node.id" should not be null
And the JSON node "data.multiRelationsDummy.manyToManyRelations.edges[0].node.name" should match "#RelatedManyToManyDummy(1|2)2#"
And the JSON node "data.multiRelationsDummy.manyToManyRelations.edges[1].node.name" should match "#RelatedManyToManyDummy(1|2)2#"
And the JSON node "data.multiRelationsDummy.oneToManyRelations.edges" should have 3 element
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#"

@createSchema @!mongodb
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
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Factory/ResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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() || $operation->getProvider() === $resourceClass)) {
return $this->resolve($source, $args, $info, $rootClass, $operation, new ArrayPaginator($body, 0, \count($body)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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() || $operation->getProvider() === $resourceClass)) {
return [...$attributeValue];
}

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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: self::class, nested: true)])]
#[ODM\EmbeddedDocument]
class MultiRelationsNested
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ApiResource(graphQlOperations: [new QueryCollection(nested: true)])]
#[ApiResource(graphQlOperations: [new QueryCollection(provider: self::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 0f68456

Please sign in to comment.