• 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

2.13
/src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.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\MutationResolverInterface;
17
use ApiPlatform\GraphQl\Resolver\Stage\DeserializeStageInterface;
18
use ApiPlatform\GraphQl\Resolver\Stage\ReadStageInterface;
19
use ApiPlatform\GraphQl\Resolver\Stage\SecurityPostDenormalizeStageInterface;
20
use ApiPlatform\GraphQl\Resolver\Stage\SecurityPostValidationStageInterface;
21
use ApiPlatform\GraphQl\Resolver\Stage\SecurityStageInterface;
22
use ApiPlatform\GraphQl\Resolver\Stage\SerializeStageInterface;
23
use ApiPlatform\GraphQl\Resolver\Stage\ValidateStageInterface;
24
use ApiPlatform\GraphQl\Resolver\Stage\WriteStageInterface;
25
use ApiPlatform\Metadata\DeleteOperationInterface;
26
use ApiPlatform\Metadata\GraphQl\Operation;
27
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
28
use ApiPlatform\Metadata\Util\ClassInfoTrait;
29
use ApiPlatform\Metadata\Util\CloneTrait;
30
use GraphQL\Type\Definition\ResolveInfo;
31
use Psr\Container\ContainerInterface;
32

33
/**
34
 * Creates a function resolving a GraphQL mutation of an item.
35
 *
36
 * @author Alan Poulain <contact@alanpoulain.eu>
37
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
38
 */
39
final class ItemMutationResolverFactory implements ResolverFactoryInterface
40
{
41
    use ClassInfoTrait;
42
    use CloneTrait;
43

44
    public function __construct(private readonly ReadStageInterface $readStage, private readonly SecurityStageInterface $securityStage, private readonly SecurityPostDenormalizeStageInterface $securityPostDenormalizeStage, private readonly SerializeStageInterface $serializeStage, private readonly DeserializeStageInterface $deserializeStage, private readonly WriteStageInterface $writeStage, private readonly ValidateStageInterface $validateStage, private readonly ContainerInterface $mutationResolverLocator, private readonly SecurityPostValidationStageInterface $securityPostValidationStage)
45
    {
46
    }
8✔
47

48
    public function __invoke(?string $resourceClass = null, ?string $rootClass = null, ?Operation $operation = null, ?PropertyMetadataFactoryInterface $propertyMetadataFactory = null): callable
49
    {
50
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation): ?array {
×
51
            if (null === $resourceClass || null === $operation) {
×
52
                return null;
×
53
            }
54

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

57
            $item = ($this->readStage)($resourceClass, $rootClass, $operation, $resolverContext);
×
58
            if (null !== $item && !\is_object($item)) {
×
59
                throw new \LogicException('Item from read stage should be a nullable object.');
×
60
            }
61
            ($this->securityStage)($resourceClass, $operation, $resolverContext + [
×
62
                'extra_variables' => [
×
63
                    'object' => $item,
×
64
                ],
×
65
            ]);
×
66
            $previousItem = $this->clone($item);
×
67

68
            if ('delete' === $operation->getName() || $operation instanceof DeleteOperationInterface) {
×
69
                ($this->securityPostDenormalizeStage)($resourceClass, $operation, $resolverContext + [
×
70
                    'extra_variables' => [
×
71
                        'object' => $item,
×
72
                        'previous_object' => $previousItem,
×
73
                    ],
×
74
                ]);
×
75
                $item = ($this->writeStage)($item, $resourceClass, $operation, $resolverContext);
×
76

77
                return ($this->serializeStage)($item, $resourceClass, $operation, $resolverContext);
×
78
            }
79

80
            $item = ($this->deserializeStage)($item, $resourceClass, $operation, $resolverContext);
×
81

82
            $mutationResolverId = $operation->getResolver();
×
83
            if (null !== $mutationResolverId) {
×
84
                /** @var MutationResolverInterface $mutationResolver */
85
                $mutationResolver = $this->mutationResolverLocator->get($mutationResolverId);
×
86
                $item = $mutationResolver($item, $resolverContext);
×
87
                if (null !== $item && $resourceClass !== $itemClass = $this->getObjectClass($item)) {
×
88
                    throw new \LogicException(sprintf('Custom mutation resolver "%s" has to return an item of class %s but returned an item of class %s.', $mutationResolverId, $operation->getShortName(), (new \ReflectionClass($itemClass))->getShortName()));
×
89
                }
90
            }
91

92
            ($this->securityPostDenormalizeStage)($resourceClass, $operation, $resolverContext + [
×
93
                'extra_variables' => [
×
94
                    'object' => $item,
×
95
                    'previous_object' => $previousItem,
×
96
                ],
×
97
            ]);
×
98

99
            if (null !== $item) {
×
100
                ($this->validateStage)($item, $resourceClass, $operation, $resolverContext);
×
101

102
                ($this->securityPostValidationStage)($resourceClass, $operation, $resolverContext + [
×
103
                    'extra_variables' => [
×
104
                        'object' => $item,
×
105
                        'previous_object' => $previousItem,
×
106
                    ],
×
107
                ]);
×
108

109
                $persistResult = ($this->writeStage)($item, $resourceClass, $operation, $resolverContext);
×
110
            }
111

112
            return ($this->serializeStage)($persistResult ?? $item, $resourceClass, $operation, $resolverContext);
×
113
        };
×
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