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

api-platform / core / 9937215276

15 Jul 2024 09:45AM UTC coverage: 64.687%. Remained the same
9937215276

push

github

web-flow
chore: symfony 7.1 dependency and branch alias (#6468)

* chore: symfony 7.1 dependency and branch alias

* chore: main_request

11480 of 17747 relevant lines covered (64.69%)

69.1 hits per line

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

75.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
final class ItemResolverFactory implements ResolverFactoryInterface
37
{
38
    use ClassInfoTrait;
39
    use CloneTrait;
40

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

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

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

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

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

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

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

87
            return ($this->serializeStage)($item, $resourceClass, $operation, $resolverContext);
7✔
88
        };
7✔
89
    }
90

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

101
            return $resourceClass;
×
102
        }
103

104
        $itemClass = $this->getObjectClass($item);
7✔
105

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

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

114
        return $resourceClass;
7✔
115
    }
116
}
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