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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 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
    ) {
UNCOV
37
        if (!$operationMetadataFactory) {
148✔
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
    {
UNCOV
44
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation, $propertyMetadataFactory) {
146✔
UNCOV
45
            if (\array_key_exists($info->fieldName, $source ?? [])) {
125✔
UNCOV
46
                $body = $source[$info->fieldName];
46✔
47

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

UNCOV
60
                $propertyMetadata = $rootClass ? $propertyMetadataFactory?->create($rootClass, $info->fieldName) : null;
46✔
UNCOV
61
                $type = $propertyMetadata?->getBuiltinTypes()[0] ?? null;
46✔
62
                // Data already fetched and normalized (field or nested resource)
UNCOV
63
                if ($body || null === $resourceClass || ($type && !$type->isCollection())) {
46✔
UNCOV
64
                    return $body;
31✔
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.
UNCOV
69
            if ($operation && (null === $resourceClass || null === $rootClass || (null !== $source && !\array_key_exists($info->fieldName, $source)))) {
125✔
70
                return null;
2✔
71
            }
72

UNCOV
73
            return $this->resolve($source, $args, $info, $rootClass, $operation, null);
125✔
UNCOV
74
        };
146✔
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
UNCOV
80
        if (!$operation) {
125✔
UNCOV
81
            if (!isset($args['id'])) {
2✔
82
                throw new NotFoundHttpException('No node found.');
×
83
            }
84

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

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

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

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

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

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