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

api-platform / core / 15023181448

14 May 2025 02:19PM UTC coverage: 0.0% (-8.4%) from 8.418%
15023181448

Pull #7139

github

web-flow
Merge 9f45709da into 1862d03b7
Pull Request #7139: refactor(symfony): remove obsolete option `validator.query-parameter-validation`

0 of 4 new or added lines in 1 file covered. (0.0%)

11266 existing lines in 366 files now uncovered.

0 of 50828 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
30
use Symfony\Component\TypeInfo\Type\CollectionType;
31

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

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

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

UNCOV
62
                $propertyMetadata = $rootClass ? $propertyMetadataFactory?->create($rootClass, $info->fieldName) : null;
×
63

UNCOV
64
                if (method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
65
                    $type = $propertyMetadata?->getNativeType();
×
66

67
                    // Data already fetched and normalized (field or nested resource)
UNCOV
68
                    if ($body || null === $resourceClass || ($type && !$type->isSatisfiedBy(fn ($t) => $t instanceof CollectionType))) {
×
UNCOV
69
                        return $body;
×
70
                    }
71
                } else {
72
                    $type = $propertyMetadata?->getBuiltinTypes()[0] ?? null;
×
73

74
                    // Data already fetched and normalized (field or nested resource)
75
                    if ($body || null === $resourceClass || ($type && !$type->isCollection())) {
×
76
                        return $body;
×
77
                    }
78
                }
79
            }
80

81
            // 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.
UNCOV
82
            if ($operation && (null === $resourceClass || null === $rootClass || (null !== $source && !\array_key_exists($info->fieldName, $source)))) {
×
83
                return null;
×
84
            }
85

UNCOV
86
            return $this->resolve($source, $args, $info, $rootClass, $operation, null);
×
UNCOV
87
        };
×
88
    }
89

90
    private function resolve(?array $source, array $args, ResolveInfo $info, ?string $rootClass = null, ?Operation $operation = null, mixed $body = null)
91
    {
92
        // Handles relay nodes
UNCOV
93
        if (!$operation) {
×
UNCOV
94
            if (!isset($args['id'])) {
×
95
                throw new NotFoundHttpException('No node found.');
×
96
            }
97

UNCOV
98
            $operation = $this->operationMetadataFactory->create($args['id']);
×
99
        }
100

UNCOV
101
        $graphQlContext = [];
×
UNCOV
102
        $context = ['source' => $source, 'args' => $args, 'info' => $info, 'root_class' => $rootClass, 'graphql_context' => &$graphQlContext];
×
103

UNCOV
104
        if (null === $operation->canValidate()) {
×
UNCOV
105
            $operation = $operation->withValidate($operation instanceof Mutation && !$operation instanceof DeleteOperationInterface);
×
106
        }
107

UNCOV
108
        $body ??= $this->provider->provide($operation, [], $context);
×
109

UNCOV
110
        if (null === $operation->canWrite()) {
×
UNCOV
111
            $operation = $operation->withWrite($operation instanceof Mutation && null !== $body);
×
112
        }
113

UNCOV
114
        return $this->processor->process($body, $operation, [], $context);
×
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