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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

0.0
/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\Util\ClassInfoTrait;
28
use ApiPlatform\Metadata\Util\CloneTrait;
29
use GraphQL\Type\Definition\ResolveInfo;
30
use Psr\Container\ContainerInterface;
31

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

43
    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)
44
    {
45
    }
×
46

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

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

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

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

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

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

81
            $mutationResolverId = $operation->getResolver();
×
82
            if (null !== $mutationResolverId) {
×
83
                /** @var MutationResolverInterface $mutationResolver */
84
                $mutationResolver = $this->mutationResolverLocator->get($mutationResolverId);
×
85
                $item = $mutationResolver($item, $resolverContext);
×
86
                if (null !== $item && $resourceClass !== $itemClass = $this->getObjectClass($item)) {
×
87
                    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()));
×
88
                }
89
            }
90

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

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

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

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

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

© 2026 Coveralls, Inc