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

api-platform / core / 6665412360

27 Oct 2023 09:27AM UTC coverage: 37.477% (-0.02%) from 37.494%
6665412360

push

github

web-flow
fix(graphql): item resolver inheritance error  (#5910)

* fix(graphql): improve condition to allow inheritance

* test: graphql inheritance

---------

Co-authored-by: soyuka <soyuka@users.noreply.github.com>

13 of 13 new or added lines in 2 files covered. (100.0%)

10316 of 27526 relevant lines covered (37.48%)

20.61 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\Util\ClassInfoTrait;
24
use ApiPlatform\Metadata\Util\CloneTrait;
25
use GraphQL\Type\Definition\ResolveInfo;
26
use Psr\Container\ContainerInterface;
27

28
/**
29
 * Creates a function retrieving an item to resolve a GraphQL query.
30
 *
31
 * @author Alan Poulain <contact@alanpoulain.eu>
32
 * @author Kévin Dunglas <dunglas@gmail.com>
33
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
34
 */
35
final class ItemResolverFactory implements ResolverFactoryInterface
36
{
37
    use ClassInfoTrait;
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
    {
42
    }
×
43

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

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

54
            if (!$operation) {
×
55
                $operation = new Query();
×
56
            }
57

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

63
            $resourceClass = $operation->getOutput()['class'] ?? $resourceClass;
×
64
            // The item retrieved can be of another type when using an identifier (see Relay Nodes at query.feature:23)
65
            $resourceClass = $this->getResourceClass($item, $resourceClass);
×
66
            $queryResolverId = $operation->getResolver();
×
67
            if (null !== $queryResolverId) {
×
68
                /** @var QueryItemResolverInterface $queryResolver */
69
                $queryResolver = $this->queryResolverLocator->get($queryResolverId);
×
70
                $item = $queryResolver($item, $resolverContext);
×
71
                $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.');
×
72
            }
73

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

86
            return ($this->serializeStage)($item, $resourceClass, $operation, $resolverContext);
×
87
        };
×
88
    }
89

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

100
            return $resourceClass;
×
101
        }
102

103
        $itemClass = $this->getObjectClass($item);
×
104

105
        if (null === $resourceClass) {
×
106
            return $itemClass;
×
107
        }
108

109
        if ($resourceClass !== $itemClass && !$item instanceof $resourceClass) {
×
110
            throw new \UnexpectedValueException(sprintf($errorMessage, (new \ReflectionClass($resourceClass))->getShortName(), (new \ReflectionClass($itemClass))->getShortName()));
×
111
        }
112

113
        return $resourceClass;
×
114
    }
115
}
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