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

api-platform / core / 8294989959

15 Mar 2024 10:45AM UTC coverage: 57.08% (+0.1%) from 56.964%
8294989959

push

github

web-flow
fix(graphql): nested collection for mongo (#6174)

* fix(graphql): nested collection for mongo

* introduce NoopProvider

---------

Co-authored-by: josef.wagner <josef.wagner@hf-solutions.co>

0 of 3 new or added lines in 3 files covered. (0.0%)

9554 of 16738 relevant lines covered (57.08%)

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

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

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

43
                return $body;
×
44
            }
45

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc