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

api-platform / core / 9881030386

10 Jul 2024 08:30PM UTC coverage: 64.71% (-0.03%) from 64.738%
9881030386

push

github

web-flow
feat(symfony): skip error handler (#6463)

1 of 1 new or added line in 1 file covered. (100.0%)

187 existing lines in 30 files now uncovered.

11475 of 17733 relevant lines covered (64.71%)

68.98 hits per line

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

80.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\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Util\CloneTrait;
25
use ApiPlatform\State\Pagination\ArrayPaginator;
26
use GraphQL\Type\Definition\ResolveInfo;
27
use Psr\Container\ContainerInterface;
28

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

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

44
    public function __invoke(?string $resourceClass = null, ?string $rootClass = null, ?Operation $operation = null, ?PropertyMetadataFactoryInterface $propertyMetadataFactory = null): callable
45
    {
UNCOV
46
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation): ?array {
1✔
47
            // 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.
UNCOV
48
            if (null === $resourceClass || null === $rootClass || (null !== $source && !\array_key_exists($info->fieldName, $source))) {
1✔
49
                return null;
×
50
            }
51

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

UNCOV
56
            $resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => true, 'is_mutation' => false, 'is_subscription' => false];
1✔
57

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

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

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

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

UNCOV
89
            return ($this->serializeStage)($collection, $resourceClass, $operation, $resolverContext);
1✔
UNCOV
90
        };
1✔
91
    }
92
}
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