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

api-platform / core / 7930644932

16 Feb 2024 12:36PM UTC coverage: 66.683% (+0.005%) from 66.678%
7930644932

push

github

web-flow
fix: return null instead of exception for GraphQL Query operation (#6118)

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

53 existing lines in 19 files now uncovered.

16304 of 24450 relevant lines covered (66.68%)

37.74 hits per line

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

85.19
/src/State/Provider/ReadProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\Put;
19
use ApiPlatform\Metadata\Util\CloneTrait;
20
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
21
use ApiPlatform\State\Exception\ProviderNotFoundException;
22
use ApiPlatform\State\ProviderInterface;
23
use ApiPlatform\State\UriVariablesResolverTrait;
24
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
25
use ApiPlatform\State\Util\RequestParser;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27

28
/**
29
 * Retrieves data from the applicable data provider, based on the current IRI, and sets it as a request parameter called data.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class ReadProvider implements ProviderInterface
34
{
35
    use CloneTrait;
36
    use OperationRequestInitiatorTrait;
37
    use UriVariablesResolverTrait;
38

39
    public function __construct(
40
        private readonly ProviderInterface $provider,
41
        private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null,
42
    ) {
43
    }
95✔
44

45
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
46
    {
47
        if (!$operation instanceof HttpOperation) {
95✔
48
            return null;
×
49
        }
50

51
        $request = ($context['request'] ?? null);
95✔
52

53
        if (!$operation->canRead()) {
95✔
UNCOV
54
            return null;
9✔
55
        }
56

57
        if (null === $filters = $request?->attributes->get('_api_filters')) {
86✔
58
            $queryString = RequestParser::getQueryString($request);
86✔
59
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
86✔
60
        }
61

62
        if ($filters) {
86✔
63
            $context['filters'] = $filters;
×
64
        }
65

66
        if ($this->serializerContextBuilder && $request) {
86✔
67
            // Builtin data providers are able to use the serialization context to automatically add join clauses
68
            $context += $this->serializerContextBuilder->createFromRequest($request, true, [
86✔
69
                'resource_class' => $operation->getClass(),
86✔
70
                'operation' => $operation,
86✔
71
            ]);
86✔
72
        }
73

74
        try {
75
            $data = $this->provider->provide($operation, $uriVariables, $context);
86✔
UNCOV
76
        } catch (ProviderNotFoundException $e) {
3✔
77
            $data = null;
×
78
        }
79

80
        if (
81
            null === $data
83✔
82
            && 'POST' !== $operation->getMethod()
83✔
83
            && ('PUT' !== $operation->getMethod()
83✔
84
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
83✔
85
            )
86
        ) {
87
            throw new NotFoundHttpException('Not Found');
×
88
        }
89

90
        $request?->attributes->set('data', $data);
83✔
91
        $request?->attributes->set('previous_data', $this->clone($data));
83✔
92

93
        return $data;
83✔
94
    }
95
}
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