• 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

83.1
/src/GraphQl/Action/EntrypointAction.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\Action;
15

16
use ApiPlatform\GraphQl\Error\ErrorHandlerInterface;
17
use ApiPlatform\GraphQl\ExecutorInterface;
18
use ApiPlatform\GraphQl\Type\SchemaBuilderInterface;
19
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
20
use GraphQL\Error\DebugFlag;
21
use GraphQL\Error\Error;
22
use GraphQL\Executor\ExecutionResult;
23
use Negotiation\Negotiator;
24
use Symfony\Component\HttpFoundation\JsonResponse;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
28
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
29

30
/**
31
 * GraphQL API entrypoint.
32
 *
33
 * @author Alan Poulain <contact@alanpoulain.eu>
34
 */
35
final class EntrypointAction
36
{
37
    use ContentNegotiationTrait;
38
    private int $debug;
39

40
    public function __construct(
41
        private readonly SchemaBuilderInterface $schemaBuilder,
42
        private readonly ExecutorInterface $executor,
43
        private readonly ?GraphiQlAction $graphiQlAction,
44
        private readonly ?GraphQlPlaygroundAction $graphQlPlaygroundAction,
45
        private readonly NormalizerInterface $normalizer,
46
        private readonly ErrorHandlerInterface $errorHandler,
47
        bool $debug = false,
48
        private readonly bool $graphiqlEnabled = false,
49
        private readonly bool $graphQlPlaygroundEnabled = false,
50
        private readonly ?string $defaultIde = null,
51
        ?Negotiator $negotiator = null,
52
    ) {
UNCOV
53
        $this->debug = $debug ? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE : DebugFlag::NONE;
147✔
UNCOV
54
        $this->negotiator = $negotiator ?? new Negotiator();
147✔
55
    }
56

57
    public function __invoke(Request $request): Response
58
    {
UNCOV
59
        $formats = ['json' => ['application/json'], 'html' => ['text/html']];
147✔
UNCOV
60
        $format = $this->getRequestFormat($request, $formats, false);
147✔
61

62
        try {
UNCOV
63
            if ($request->isMethod('GET') && 'html' === $format) {
147✔
64
                if ('graphiql' === $this->defaultIde && $this->graphiqlEnabled && $this->graphiQlAction) {
1✔
65
                    return ($this->graphiQlAction)($request);
1✔
66
                }
67

68
                if ('graphql-playground' === $this->defaultIde && $this->graphQlPlaygroundEnabled && $this->graphQlPlaygroundAction) {
×
69
                    return ($this->graphQlPlaygroundAction)($request);
×
70
                }
71
            }
72

UNCOV
73
            [$query, $operationName, $variables] = $this->parseRequest($request);
146✔
UNCOV
74
            if (null === $query) {
146✔
75
                throw new BadRequestHttpException('GraphQL query is not valid.');
1✔
76
            }
77

UNCOV
78
            $executionResult = $this->executor
145✔
UNCOV
79
                ->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operationName)
145✔
UNCOV
80
                ->setErrorsHandler($this->errorHandler)
145✔
UNCOV
81
                ->setErrorFormatter($this->normalizer->normalize(...));
145✔
82
        } catch (\Exception $exception) {
1✔
83
            $executionResult = (new ExecutionResult(null, [new Error($exception->getMessage(), null, null, [], null, $exception)]))
1✔
84
                ->setErrorsHandler($this->errorHandler)
1✔
85
                ->setErrorFormatter($this->normalizer->normalize(...));
1✔
86
        }
87

UNCOV
88
        return new JsonResponse($executionResult->toArray($this->debug));
146✔
89
    }
90

91
    /**
92
     * @throws BadRequestHttpException
93
     *
94
     * @return array{0: array<string, mixed>|null, 1: string, 2: array<string, mixed>}
95
     */
96
    private function parseRequest(Request $request): array
97
    {
UNCOV
98
        $queryParameters = $request->query->all();
146✔
UNCOV
99
        $query = $queryParameters['query'] ?? null;
146✔
UNCOV
100
        $operationName = $queryParameters['operationName'] ?? null;
146✔
UNCOV
101
        if ($variables = $queryParameters['variables'] ?? []) {
146✔
102
            $variables = $this->decodeVariables($variables);
3✔
103
        }
104

UNCOV
105
        if (!$request->isMethod('POST')) {
146✔
106
            return [$query, $operationName, $variables];
131✔
107
        }
108

UNCOV
109
        $contentType = method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType();
15✔
UNCOV
110
        if ('json' === $contentType) {
15✔
UNCOV
111
            return $this->parseData($query, $operationName, $variables, $request->getContent());
13✔
112
        }
113

114
        if ('graphql' === $contentType) {
2✔
115
            $query = $request->getContent();
×
116
        }
117

118
        if (\in_array($contentType, ['multipart', 'form'], true)) {
2✔
119
            return $this->parseMultipartRequest($query, $operationName, $variables, $request->request->all(), $request->files->all());
2✔
120
        }
121

122
        return [$query, $operationName, $variables];
×
123
    }
124

125
    /**
126
     * @param array<string,mixed> $variables
127
     *
128
     * @throws BadRequestHttpException
129
     *
130
     * @return array{0: array<string, mixed>, 1: string, 2: array<string, mixed>}
131
     */
132
    private function parseData(?string $query, ?string $operationName, array $variables, string $jsonContent): array
133
    {
UNCOV
134
        if (!\is_array($data = json_decode($jsonContent, true, 512, \JSON_ERROR_NONE))) {
15✔
135
            throw new BadRequestHttpException('GraphQL data is not valid JSON.');
×
136
        }
137

UNCOV
138
        if (isset($data['query'])) {
15✔
UNCOV
139
            $query = $data['query'];
15✔
140
        }
141

UNCOV
142
        if (isset($data['variables'])) {
15✔
UNCOV
143
            $variables = \is_array($data['variables']) ? $data['variables'] : $this->decodeVariables($data['variables']);
10✔
144
        }
145

UNCOV
146
        if (isset($data['operationName'])) {
15✔
147
            $operationName = $data['operationName'];
×
148
        }
149

UNCOV
150
        return [$query, $operationName, $variables];
15✔
151
    }
152

153
    /**
154
     * @param array<string,mixed> $variables
155
     * @param array<string,mixed> $bodyParameters
156
     * @param array<string,mixed> $files
157
     *
158
     * @throws BadRequestHttpException
159
     *
160
     * @return array{0: array<string, mixed>, 1: string, 2: array<string, mixed>}
161
     */
162
    private function parseMultipartRequest(?string $query, ?string $operationName, array $variables, array $bodyParameters, array $files): array
163
    {
164
        if ((null === $operations = $bodyParameters['operations'] ?? null) || (null === $map = $bodyParameters['map'] ?? null)) {
2✔
165
            throw new BadRequestHttpException('GraphQL multipart request does not respect the specification.');
×
166
        }
167

168
        [$query, $operationName, $variables] = $this->parseData($query, $operationName, $variables, $operations);
2✔
169

170
        /** @var string $map */
171
        if (!\is_array($decodedMap = json_decode($map, true, 512, \JSON_ERROR_NONE))) {
2✔
172
            throw new BadRequestHttpException('GraphQL multipart request map is not valid JSON.');
×
173
        }
174

175
        $variables = $this->applyMapToVariables($decodedMap, $variables, $files);
2✔
176

177
        return [$query, $operationName, $variables];
2✔
178
    }
179

180
    /**
181
     * @param array<string,mixed> $map
182
     * @param array<string,mixed> $variables
183
     * @param array<string,mixed> $files
184
     *
185
     * @throws BadRequestHttpException
186
     */
187
    private function applyMapToVariables(array $map, array $variables, array $files): array
188
    {
189
        foreach ($map as $key => $value) {
2✔
190
            if (null === $file = $files[$key] ?? null) {
2✔
191
                throw new BadRequestHttpException('GraphQL multipart request file has not been sent correctly.');
×
192
            }
193

194
            foreach ($value as $mapValue) {
2✔
195
                $path = explode('.', (string) $mapValue);
2✔
196

197
                if ('variables' !== $path[0]) {
2✔
198
                    throw new BadRequestHttpException('GraphQL multipart request path in map is invalid.');
×
199
                }
200

201
                unset($path[0]);
2✔
202

203
                $mapPathExistsInVariables = array_reduce($path, static fn (array $inVariables, string $pathElement) => \array_key_exists($pathElement, $inVariables) ? $inVariables[$pathElement] : false, $variables);
2✔
204

205
                if (false === $mapPathExistsInVariables) {
2✔
206
                    throw new BadRequestHttpException('GraphQL multipart request path in map does not match the variables.');
×
207
                }
208

209
                $variableFileValue = &$variables;
2✔
210
                foreach ($path as $pathValue) {
2✔
211
                    $variableFileValue = &$variableFileValue[$pathValue];
2✔
212
                }
213
                $variableFileValue = $file;
2✔
214
            }
215
        }
216

217
        return $variables;
2✔
218
    }
219

220
    /**
221
     * @throws BadRequestHttpException
222
     *
223
     * @return array<string, mixed>
224
     */
225
    private function decodeVariables(string $variables): array
226
    {
227
        if (!\is_array($decoded = json_decode($variables, true, 512, \JSON_ERROR_NONE))) {
3✔
228
            throw new BadRequestHttpException('GraphQL variables are not valid JSON.');
×
229
        }
230

231
        return $decoded;
3✔
232
    }
233
}
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