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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 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

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) {
×
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) {
×
UNCOV
45
            if (\array_key_exists($info->fieldName, $source ?? [])) {
×
UNCOV
46
                $body = $source[$info->fieldName];
×
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())) {
×
50
                    return \is_array($body) ? $this->resolve(
×
51
                        $source,
×
52
                        $args,
×
53
                        $info,
×
54
                        $rootClass,
×
55
                        $operation,
×
56
                        new ArrayPaginator($body, 0, \count($body))
×
57
                    ) : $body;
×
58
                }
59

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

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

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

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

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

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

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

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