Skip to content

Commit 45aa037

Browse files
author
josef.wagner
committed
introduce NoopProvider
1 parent 4263314 commit 45aa037

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

features/graphql/query.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +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
7172
Scenario: Retrieve embedded collections
7273
Given there are 2 multiRelationsDummy objects having each a manyToOneRelation, 2 manyToManyRelations, 3 oneToManyRelations and 4 embeddedRelations
7374
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() || $operation->getProvider() === $resourceClass)) {
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() || $operation->getProvider() === $resourceClass)) {
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/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, provider: self::class, 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(provider: self::class, nested: true)])]
21+
#[ApiResource(graphQlOperations: [new QueryCollection(provider: NoopProvider::class, nested: true)])]
2122
#[ODM\EmbeddedDocument]
2223
class MultiRelationsNestedPaginated
2324
{

0 commit comments

Comments
 (0)