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

api-platform / core / 9881030386

10 Jul 2024 08:30PM UTC coverage: 64.71% (-0.03%) from 64.738%
9881030386

push

github

web-flow
feat(symfony): skip error handler (#6463)

1 of 1 new or added line in 1 file covered. (100.0%)

187 existing lines in 30 files now uncovered.

11475 of 17733 relevant lines covered (64.71%)

68.98 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
    {
UNCOV
43
    }
8✔
44

45
    public function __invoke(?string $resourceClass = null, ?string $rootClass = null, ?Operation $operation = null, ?PropertyMetadataFactoryInterface $propertyMetadataFactory = null): callable
46
    {
UNCOV
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)
UNCOV
49
            if ($source && \array_key_exists($info->fieldName, $source)) {
7✔
UNCOV
50
                return $source[$info->fieldName];
1✔
51
            }
52

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

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

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

UNCOV
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)
UNCOV
66
            $resourceClass = $this->getResourceClass($item, $resourceClass);
7✔
UNCOV
67
            $queryResolverId = $operation->getResolver();
7✔
UNCOV
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

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

UNCOV
87
            return ($this->serializeStage)($item, $resourceClass, $operation, $resolverContext);
7✔
UNCOV
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
    {
UNCOV
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

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

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

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

UNCOV
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