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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

10372 of 12438 relevant lines covered (83.39%)

11.97 hits per line

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

97.5
/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\Util\ClassInfoTrait;
24
use ApiPlatform\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
    }
10✔
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) {
10✔
47
            // Data already fetched and normalized (field or nested resource)
48
            if ($source && \array_key_exists($info->fieldName, $source)) {
10✔
49
                return $source[$info->fieldName];
2✔
50
            }
51

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

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

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

63
            $resourceClass = $operation->getOutput()['class'] ?? $resourceClass;
7✔
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);
7✔
66
            $queryResolverId = $operation->getResolver();
5✔
67
            if (null !== $queryResolverId) {
5✔
68
                /** @var QueryItemResolverInterface $queryResolver */
69
                $queryResolver = $this->queryResolverLocator->get($queryResolverId);
2✔
70
                $item = $queryResolver($item, $resolverContext);
2✔
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.');
2✔
72
            }
73

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

86
            return ($this->serializeStage)($item, $resourceClass, $operation, $resolverContext);
4✔
87
        };
10✔
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) {
7✔
96
            if (null === $resourceClass) {
2✔
97
                throw new \UnexpectedValueException('Resource class cannot be determined.');
1✔
98
            }
99

100
            return $resourceClass;
1✔
101
        }
102

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

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

109
        if ($resourceClass !== $itemClass) {
4✔
110
            throw new \UnexpectedValueException(sprintf($errorMessage, (new \ReflectionClass($resourceClass))->getShortName(), (new \ReflectionClass($itemClass))->getShortName()));
2✔
111
        }
112

113
        return $resourceClass;
3✔
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