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

api-platform / core / 13925245599

18 Mar 2025 02:06PM UTC coverage: 61.074% (-0.9%) from 61.973%
13925245599

Pull #7031

github

web-flow
Merge 8990d8b26 into 7cb5a6db8
Pull Request #7031: fix: header parameter should be case insensitive

3 of 4 new or added lines in 1 file covered. (75.0%)

167 existing lines in 27 files now uncovered.

11314 of 18525 relevant lines covered (61.07%)

51.02 hits per line

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

0.0
/src/GraphQl/Resolver/Factory/ItemResolverFactory.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\QueryItemResolverInterface;
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\ClassInfoTrait;
25
use ApiPlatform\Metadata\Util\CloneTrait;
26
use GraphQL\Type\Definition\ResolveInfo;
27
use Psr\Container\ContainerInterface;
28

29
/**
30
 * Creates a function retrieving an item to resolve a GraphQL query.
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
 * @deprecated
37
 */
38
final class ItemResolverFactory implements ResolverFactoryInterface
39
{
40
    use ClassInfoTrait;
41
    use CloneTrait;
42

43
    public function __construct(private readonly ReadStageInterface $readStage, private readonly SecurityStageInterface $securityStage, private readonly SecurityPostDenormalizeStageInterface $securityPostDenormalizeStage, private readonly SerializeStageInterface $serializeStage, private readonly ContainerInterface $queryResolverLocator)
44
    {
UNCOV
45
    }
×
46

47
    public function __invoke(?string $resourceClass = null, ?string $rootClass = null, ?Operation $operation = null, ?PropertyMetadataFactoryInterface $propertyMetadataFactory = null): callable
48
    {
UNCOV
49
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation) {
×
50
            // Data already fetched and normalized (field or nested resource)
UNCOV
51
            if ($source && \array_key_exists($info->fieldName, $source)) {
×
UNCOV
52
                return $source[$info->fieldName];
×
53
            }
54

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

UNCOV
57
            if (!$operation) {
×
58
                $operation = new Query();
×
59
            }
60

UNCOV
61
            $item = ($this->readStage)($resourceClass, $rootClass, $operation, $resolverContext);
×
UNCOV
62
            if (null !== $item && !\is_object($item)) {
×
63
                throw new \LogicException('Item from read stage should be a nullable object.');
×
64
            }
65

UNCOV
66
            $resourceClass = $operation->getOutput()['class'] ?? $resourceClass;
×
67
            // The item retrieved can be of another type when using an identifier (see Relay Nodes at query.feature:23)
UNCOV
68
            $resourceClass = $this->getResourceClass($item, $resourceClass);
×
UNCOV
69
            $queryResolverId = $operation->getResolver();
×
UNCOV
70
            if (null !== $queryResolverId) {
×
71
                /** @var QueryItemResolverInterface $queryResolver */
72
                $queryResolver = $this->queryResolverLocator->get($queryResolverId);
×
73
                $item = $queryResolver($item, $resolverContext);
×
74
                $resourceClass = $this->getResourceClass($item, $resourceClass, \sprintf('Custom query resolver "%s"', $queryResolverId).' has to return an item of class %s but returned an item of class %s.');
×
75
            }
76

UNCOV
77
            ($this->securityStage)($resourceClass, $operation, $resolverContext + [
×
UNCOV
78
                'extra_variables' => [
×
UNCOV
79
                    'object' => $item,
×
UNCOV
80
                ],
×
UNCOV
81
            ]);
×
UNCOV
82
            ($this->securityPostDenormalizeStage)($resourceClass, $operation, $resolverContext + [
×
UNCOV
83
                'extra_variables' => [
×
UNCOV
84
                    'object' => $item,
×
UNCOV
85
                    'previous_object' => $this->clone($item),
×
UNCOV
86
                ],
×
UNCOV
87
            ]);
×
88

UNCOV
89
            return ($this->serializeStage)($item, $resourceClass, $operation, $resolverContext);
×
UNCOV
90
        };
×
91
    }
92

93
    /**
94
     * @throws \UnexpectedValueException
95
     */
96
    private function getResourceClass(?object $item, ?string $resourceClass, string $errorMessage = 'Resolver only handles items of class %s but retrieved item is of class %s.'): string
97
    {
UNCOV
98
        if (null === $item) {
×
99
            if (null === $resourceClass) {
×
100
                throw new \UnexpectedValueException('Resource class cannot be determined.');
×
101
            }
102

103
            return $resourceClass;
×
104
        }
105

UNCOV
106
        $itemClass = $this->getObjectClass($item);
×
107

UNCOV
108
        if (null === $resourceClass) {
×
109
            return $itemClass;
×
110
        }
111

UNCOV
112
        if ($resourceClass !== $itemClass && !$item instanceof $resourceClass) {
×
113
            throw new \UnexpectedValueException(\sprintf($errorMessage, (new \ReflectionClass($resourceClass))->getShortName(), (new \ReflectionClass($itemClass))->getShortName()));
×
114
        }
115

UNCOV
116
        return $resourceClass;
×
117
    }
118
}
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