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

api-platform / core / 6981542872

24 Nov 2023 01:48PM UTC coverage: 37.261% (-0.02%) from 37.284%
6981542872

push

github

web-flow
feat(graphql): support enum collection as property (#5955)

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

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

2 existing lines in 2 files now uncovered.

10287 of 27608 relevant lines covered (37.26%)

20.52 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\Util\CloneTrait;
23
use GraphQL\Type\Definition\ResolveInfo;
24
use Psr\Container\ContainerInterface;
25

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

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

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

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

UNCOV
53
            $resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => true, 'is_mutation' => false, 'is_subscription' => false];
×
54

55
            $collection = ($this->readStage)($resourceClass, $rootClass, $operation, $resolverContext);
×
56
            if (!is_iterable($collection)) {
×
57
                throw new \LogicException('Collection from read stage should be iterable.');
×
58
            }
59

60
            $queryResolverId = $operation->getResolver();
×
61
            if (null !== $queryResolverId) {
×
62
                /** @var QueryCollectionResolverInterface $queryResolver */
63
                $queryResolver = $this->queryResolverLocator->get($queryResolverId);
×
64
                $collection = $queryResolver($collection, $resolverContext);
×
65
            }
66

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

82
            return ($this->serializeStage)($collection, $resourceClass, $operation, $resolverContext);
×
83
        };
×
84
    }
85
}
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