• 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/ResolverFactory.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\Metadata\GraphQl\Mutation;
17
use ApiPlatform\Metadata\GraphQl\Operation;
18
use ApiPlatform\Metadata\GraphQl\Query;
19
use ApiPlatform\State\Pagination\ArrayPaginator;
20
use ApiPlatform\State\ProcessorInterface;
21
use ApiPlatform\State\ProviderInterface;
22
use GraphQL\Type\Definition\ResolveInfo;
23

24
class ResolverFactory implements ResolverFactoryInterface
25
{
26
    public function __construct(
27
        private readonly ProviderInterface $provider,
28
        private readonly ProcessorInterface $processor
29
    ) {
30
    }
×
31

32
    public function __invoke(string $resourceClass = null, string $rootClass = null, Operation $operation = null): callable
33
    {
34
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation) {
×
35
            // Data already fetched and normalized (field or nested resource)
36
            if ($body = $source[$info->fieldName] ?? null) {
×
37
                // special treatment for nested resources without a resolver/provider
NEW
38
                if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && !$operation->getProvider()) {
×
NEW
39
                    return $this->resolve($source, $args, $info, $rootClass, $operation, new ArrayPaginator($body, 0, \count($body)));
×
40
                }
41

UNCOV
42
                return $body;
×
43
            }
44

45
            if (null === $resourceClass && \array_key_exists($info->fieldName, $source ?? [])) {
×
46
                return $body;
×
47
            }
48

49
            // 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.
50
            if ($operation && (null === $resourceClass || null === $rootClass || (null !== $source && !\array_key_exists($info->fieldName, $source)))) {
×
51
                return null;
×
52
            }
53

NEW
54
            return $this->resolve($source, $args, $info, $rootClass, $operation, null);
×
NEW
55
        };
×
56
    }
57

58
    private function resolve(?array $source, array $args, ResolveInfo $info, string $rootClass = null, Operation $operation = null, mixed $body)
59
    {
60
        // Handles relay nodes
NEW
61
        $operation ??= new Query();
×
62

NEW
63
        $graphQlContext = [];
×
NEW
64
        $context = ['source' => $source, 'args' => $args, 'info' => $info, 'root_class' => $rootClass, 'graphql_context' => &$graphQlContext];
×
65

NEW
66
        if (null === $operation->canValidate()) {
×
NEW
67
            $operation = $operation->withValidate($operation instanceof Mutation);
×
68
        }
69

NEW
70
        $body ??= $this->provider->provide($operation, [], $context);
×
71

NEW
72
        if (null === $operation->canWrite()) {
×
NEW
73
            $operation = $operation->withWrite($operation instanceof Mutation && null !== $body);
×
74
        }
75

NEW
76
        return $this->processor->process($body, $operation, [], $context);
×
77
    }
78
}
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