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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

94.12
/src/GraphQl/Resolver/Factory/ResolverFactory.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\State\Provider\NoopProvider;
17
use ApiPlatform\Metadata\DeleteOperationInterface;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\GraphQl\Mutation;
20
use ApiPlatform\Metadata\GraphQl\Operation;
21
use ApiPlatform\Metadata\GraphQl\Query;
22
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\State\Pagination\ArrayPaginator;
25
use ApiPlatform\State\ProcessorInterface;
26
use ApiPlatform\State\ProviderInterface;
27
use GraphQL\Type\Definition\ResolveInfo;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29

30
class ResolverFactory implements ResolverFactoryInterface
31
{
32
    public function __construct(
33
        private readonly ProviderInterface $provider,
34
        private readonly ProcessorInterface $processor,
35
        private readonly ?OperationMetadataFactoryInterface $operationMetadataFactory = null,
36
    ) {
37
        if (!$operationMetadataFactory) {
161✔
38
            throw new InvalidArgumentException(\sprintf('Not injecting the "%s" exposes Relay nodes to a security risk.', OperationMetadataFactoryInterface::class));
×
39
        }
40
    }
41

42
    public function __invoke(?string $resourceClass = null, ?string $rootClass = null, ?Operation $operation = null, ?PropertyMetadataFactoryInterface $propertyMetadataFactory = null): callable
43
    {
44
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation, $propertyMetadataFactory) {
159✔
45
            if (\array_key_exists($info->fieldName, $source ?? [])) {
138✔
46
                $body = $source[$info->fieldName];
48✔
47

48
                // special treatment for nested resources without a resolver/provider
49
                if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && (!$operation->getProvider() || NoopProvider::class === $operation->getProvider())) {
48✔
UNCOV
50
                    return \is_array($body) ? $this->resolve(
2✔
UNCOV
51
                        $source,
2✔
UNCOV
52
                        $args,
2✔
UNCOV
53
                        $info,
2✔
UNCOV
54
                        $rootClass,
2✔
UNCOV
55
                        $operation,
2✔
UNCOV
56
                        new ArrayPaginator($body, 0, \count($body))
2✔
UNCOV
57
                    ) : $body;
2✔
58
                }
59

60
                $propertyMetadata = $rootClass ? $propertyMetadataFactory?->create($rootClass, $info->fieldName) : null;
48✔
61
                $type = $propertyMetadata?->getBuiltinTypes()[0] ?? null;
48✔
62
                // Data already fetched and normalized (field or nested resource)
63
                if ($body || null === $resourceClass || ($type && !$type->isCollection())) {
48✔
64
                    return $body;
33✔
65
                }
66
            }
67

68
            // If authorization has failed for a relation field (e.g. via ApiProperty security), the field is not present in the source: null can be returned directly to ensure the collection isn't in the response.
69
            if ($operation && (null === $resourceClass || null === $rootClass || (null !== $source && !\array_key_exists($info->fieldName, $source)))) {
138✔
UNCOV
70
                return null;
2✔
71
            }
72

73
            return $this->resolve($source, $args, $info, $rootClass, $operation, null);
138✔
74
        };
159✔
75
    }
76

77
    private function resolve(?array $source, array $args, ResolveInfo $info, ?string $rootClass = null, ?Operation $operation = null, mixed $body = null)
78
    {
79
        // Handles relay nodes
80
        if (!$operation) {
138✔
81
            if (!isset($args['id'])) {
3✔
82
                throw new NotFoundHttpException('No node found.');
×
83
            }
84

85
            $operation = $this->operationMetadataFactory->create($args['id']);
3✔
86
        }
87

88
        $graphQlContext = [];
138✔
89
        $context = ['source' => $source, 'args' => $args, 'info' => $info, 'root_class' => $rootClass, 'graphql_context' => &$graphQlContext];
138✔
90

91
        if (null === $operation->canValidate()) {
138✔
92
            $operation = $operation->withValidate($operation instanceof Mutation && !$operation instanceof DeleteOperationInterface);
135✔
93
        }
94

95
        $body ??= $this->provider->provide($operation, [], $context);
138✔
96

97
        if (null === $operation->canWrite()) {
120✔
98
            $operation = $operation->withWrite($operation instanceof Mutation && null !== $body);
119✔
99
        }
100

101
        return $this->processor->process($body, $operation, [], $context);
120✔
102
    }
103
}
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