• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 7355537923

29 Dec 2023 09:42AM UTC coverage: 37.321% (-0.009%) from 37.33%
7355537923

push

github

web-flow
GraphQL: Nested Collections (#6038)

* feat(graphql): support nested collections

* null safe operator

---------

Co-authored-by: josef.wagner <josef.wagner@hf-solutions.co>
Co-authored-by: Antoine Bluchet <soyuka@users.noreply.github.com>

11 of 29 new or added lines in 4 files covered. (37.93%)

71 existing lines in 9 files now uncovered.

10360 of 27759 relevant lines covered (37.32%)

28.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/GraphQl/Resolver/Factory/CollectionResolverFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Resolver\Factory;
15

16
use ApiPlatform\GraphQl\Resolver\QueryCollectionResolverInterface;
17
use ApiPlatform\GraphQl\Resolver\Stage\ReadStageInterface;
18
use ApiPlatform\GraphQl\Resolver\Stage\SecurityPostDenormalizeStageInterface;
19
use ApiPlatform\GraphQl\Resolver\Stage\SecurityStageInterface;
20
use ApiPlatform\GraphQl\Resolver\Stage\SerializeStageInterface;
21
use ApiPlatform\Metadata\GraphQl\Operation;
22
use ApiPlatform\Metadata\GraphQl\Query;
23
use ApiPlatform\Metadata\Util\CloneTrait;
24
use ApiPlatform\State\Pagination\ArrayPaginator;
25
use GraphQL\Type\Definition\ResolveInfo;
26
use Psr\Container\ContainerInterface;
27

28
/**
29
 * Creates a function retrieving a collection to resolve a GraphQL query or a field returned by a mutation.
30
 *
31
 * @author Alan Poulain <contact@alanpoulain.eu>
32
 * @author Kévin Dunglas <dunglas@gmail.com>
33
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
34
 */
35
final class CollectionResolverFactory implements ResolverFactoryInterface
36
{
37
    use CloneTrait;
38

39
    public function __construct(private readonly ReadStageInterface $readStage, private readonly SecurityStageInterface $securityStage, private readonly SecurityPostDenormalizeStageInterface $securityPostDenormalizeStage, private readonly SerializeStageInterface $serializeStage, private readonly ContainerInterface $queryResolverLocator)
40
    {
41
    }
×
42

43
    public function __invoke(string $resourceClass = null, string $rootClass = null, Operation $operation = null): callable
44
    {
45
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation): ?array {
×
46
            // If authorization has failed for a relation field (e.g. via ApiProperty security), the field is not present in the source: null can be returned directly to ensure the collection isn't in the response.
47
            if (null === $resourceClass || null === $rootClass || (null !== $source && !\array_key_exists($info->fieldName, $source))) {
×
48
                return null;
×
49
            }
50

51
            if (is_a($resourceClass, \BackedEnum::class, true) && $source && \array_key_exists($info->fieldName, $source)) {
×
52
                return $source[$info->fieldName];
×
53
            }
54

55
            $resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => true, 'is_mutation' => false, 'is_subscription' => false];
×
56

NEW
57
            if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && !$operation->getProvider() && $source && \array_key_exists($info->fieldName, $source)) {
×
NEW
58
                return ($this->serializeStage)(new ArrayPaginator($source[$info->fieldName], 0, \count($source[$info->fieldName])), $resourceClass, $operation, $resolverContext);
×
59
            }
60

61
            $collection = ($this->readStage)($resourceClass, $rootClass, $operation, $resolverContext);
×
62
            if (!is_iterable($collection)) {
×
63
                throw new \LogicException('Collection from read stage should be iterable.');
×
64
            }
65

66
            $queryResolverId = $operation->getResolver();
×
67
            if (null !== $queryResolverId) {
×
68
                /** @var QueryCollectionResolverInterface $queryResolver */
69
                $queryResolver = $this->queryResolverLocator->get($queryResolverId);
×
70
                $collection = $queryResolver($collection, $resolverContext);
×
71
            }
72

73
            // Only perform security stage on the top-level query
74
            if (null === $source) {
×
75
                ($this->securityStage)($resourceClass, $operation, $resolverContext + [
×
76
                    'extra_variables' => [
×
77
                        'object' => $collection,
×
78
                    ],
×
79
                ]);
×
80
                ($this->securityPostDenormalizeStage)($resourceClass, $operation, $resolverContext + [
×
81
                    'extra_variables' => [
×
82
                        'object' => $collection,
×
83
                        'previous_object' => $this->clone($collection),
×
84
                    ],
×
85
                ]);
×
86
            }
87

88
            return ($this->serializeStage)($collection, $resourceClass, $operation, $resolverContext);
×
89
        };
×
90
    }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc