|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Request; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
9
|
|
|
use function array_filter; |
|
10
|
|
|
use function explode; |
|
11
|
|
|
use function is_string; |
|
12
|
|
|
use function json_decode; |
|
13
|
|
|
use function json_last_error; |
|
14
|
|
|
use const JSON_ERROR_NONE; |
|
15
|
|
|
|
|
16
|
|
|
class Parser implements ParserInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use UploadParserTrait; |
|
19
|
|
|
|
|
20
|
101 |
|
public function parse(Request $request): array |
|
21
|
|
|
{ |
|
22
|
|
|
// Extracts the GraphQL request parameters |
|
23
|
101 |
|
$parsedBody = $this->getParsedBody($request); |
|
24
|
|
|
|
|
25
|
99 |
|
return $this->getParams($request, $parsedBody); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets the body from the request based on Content-Type header. |
|
30
|
|
|
*/ |
|
31
|
101 |
|
private function getParsedBody(Request $request): array |
|
32
|
|
|
{ |
|
33
|
101 |
|
$body = $request->getContent(); |
|
34
|
101 |
|
$method = $request->getMethod(); |
|
35
|
101 |
|
$contentType = explode(';', (string) $request->headers->get('content-type'), 2)[0]; |
|
36
|
|
|
|
|
37
|
|
|
switch ($contentType) { |
|
38
|
|
|
// Plain string |
|
39
|
101 |
|
case static::CONTENT_TYPE_GRAPHQL: |
|
40
|
3 |
|
$parsedBody = [static::PARAM_QUERY => $body]; |
|
41
|
3 |
|
break; |
|
42
|
|
|
|
|
43
|
|
|
// JSON object |
|
44
|
98 |
|
case static::CONTENT_TYPE_JSON: |
|
45
|
4 |
|
if (empty($body)) { |
|
46
|
2 |
|
if (Request::METHOD_GET === $method) { |
|
47
|
1 |
|
$parsedBody = []; |
|
48
|
1 |
|
break; |
|
49
|
|
|
} |
|
50
|
1 |
|
throw new BadRequestHttpException('The request content body must not be empty when using json content type request.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
$parsedBody = json_decode($body, true); |
|
54
|
|
|
|
|
55
|
2 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
56
|
1 |
|
throw new BadRequestHttpException('POST body sent invalid JSON'); |
|
57
|
|
|
} |
|
58
|
1 |
|
break; |
|
59
|
|
|
|
|
60
|
|
|
// URL-encoded query-string |
|
61
|
94 |
|
case static::CONTENT_TYPE_FORM: |
|
62
|
1 |
|
$parsedBody = $request->request->all(); |
|
63
|
1 |
|
break; |
|
64
|
|
|
|
|
65
|
93 |
|
case static::CONTENT_TYPE_FORM_DATA: |
|
66
|
6 |
|
$parsedBody = $this->handleUploadedFiles($request->request->all(), $request->files->all()); |
|
67
|
6 |
|
break; |
|
68
|
|
|
|
|
69
|
|
|
default: |
|
70
|
87 |
|
$parsedBody = []; |
|
71
|
87 |
|
break; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
99 |
|
return $parsedBody; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Gets the GraphQL parameters from the request. |
|
79
|
|
|
*/ |
|
80
|
99 |
|
private function getParams(Request $request, array $data = []): array |
|
81
|
|
|
{ |
|
82
|
|
|
// Add default request parameters |
|
83
|
99 |
|
$data = array_filter($data) + [ |
|
84
|
99 |
|
static::PARAM_QUERY => null, |
|
85
|
99 |
|
static::PARAM_VARIABLES => null, |
|
86
|
99 |
|
static::PARAM_OPERATION_NAME => null, |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
// Use all query parameters, since starting from Symfony 6 there will be an exception accessing array parameters |
|
90
|
|
|
// via request->query->get(key), and another exception accessing non-array parameter via request->query->all(key) |
|
91
|
99 |
|
$queryParameters = $request->query->all(); |
|
92
|
|
|
|
|
93
|
|
|
// Override request using query-string parameters |
|
94
|
99 |
|
$query = $queryParameters[static::PARAM_QUERY] ?? $data[static::PARAM_QUERY]; |
|
95
|
99 |
|
$variables = $queryParameters[static::PARAM_VARIABLES] ?? $data[static::PARAM_VARIABLES]; |
|
96
|
99 |
|
$operationName = $queryParameters[static::PARAM_OPERATION_NAME] ?? $data[static::PARAM_OPERATION_NAME]; |
|
97
|
|
|
|
|
98
|
|
|
// `query` parameter is mandatory. |
|
99
|
99 |
|
if (empty($query)) { |
|
100
|
1 |
|
throw new BadRequestHttpException('Must provide query parameter'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Variables can be defined using a JSON-encoded object. |
|
104
|
|
|
// If the parsing fails, an exception will be thrown. |
|
105
|
98 |
|
if (is_string($variables)) { |
|
106
|
33 |
|
$variables = json_decode($variables, true); |
|
107
|
|
|
|
|
108
|
33 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
109
|
1 |
|
throw new BadRequestHttpException('Variables are invalid JSON'); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return [ |
|
114
|
97 |
|
static::PARAM_QUERY => $query, |
|
115
|
97 |
|
static::PARAM_VARIABLES => $variables, |
|
116
|
97 |
|
static::PARAM_OPERATION_NAME => $operationName, |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|