1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
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
|
|
|
namespace Overblog\GraphQLBundle\Request; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
16
|
|
|
|
17
|
|
|
class Parser |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Parses the HTTP request and extracts the GraphQL request parameters. |
21
|
|
|
* |
22
|
|
|
* @param Request $request |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
28 |
|
public function parse(Request $request) |
27
|
|
|
{ |
28
|
|
|
// Extracts the GraphQL request parameters |
29
|
28 |
|
$parsedBody = $this->getParsedBody($request); |
30
|
27 |
|
$data = $this->getParams($request, $parsedBody); |
31
|
|
|
|
32
|
25 |
|
return $data; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Gets the body from the request based on Content-Type header. |
37
|
|
|
* |
38
|
|
|
* @param Request $request |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
28 |
|
private function getParsedBody(Request $request) |
43
|
|
|
{ |
44
|
28 |
|
$body = $request->getContent(); |
45
|
28 |
|
$type = explode(';', $request->headers->get('content-type'))[0]; |
46
|
|
|
|
47
|
|
|
switch ($type) { |
48
|
|
|
// Plain string |
49
|
28 |
|
case 'application/graphql': |
50
|
1 |
|
$parsedBody = ['query' => $body]; |
51
|
1 |
|
break; |
52
|
|
|
|
53
|
|
|
// JSON object |
54
|
27 |
|
case 'application/json': |
55
|
2 |
|
$json = json_decode($body, true); |
56
|
|
|
|
57
|
2 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
58
|
1 |
|
throw new BadRequestHttpException( |
59
|
1 |
|
sprintf('POST body sent invalid JSON [%s]', json_last_error_msg()) |
60
|
1 |
|
); |
61
|
|
|
} |
62
|
1 |
|
$parsedBody = $json; |
63
|
1 |
|
break; |
64
|
|
|
|
65
|
|
|
// URL-encoded query-string |
66
|
25 |
|
case 'application/x-www-form-urlencoded': |
67
|
25 |
|
case 'multipart/form-data': |
68
|
1 |
|
$parsedBody = $request->request->all(); |
69
|
1 |
|
break; |
70
|
|
|
|
71
|
24 |
|
default: |
72
|
24 |
|
$parsedBody = []; |
73
|
24 |
|
break; |
74
|
24 |
|
} |
75
|
|
|
|
76
|
27 |
|
return $parsedBody; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets the GraphQL parameters from the request. |
81
|
|
|
* |
82
|
|
|
* @param Request $request |
83
|
|
|
* @param array $data |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
27 |
|
private function getParams(Request $request, array $data = []) |
88
|
|
|
{ |
89
|
|
|
// Add default request parameters |
90
|
|
|
$data = $data + [ |
91
|
27 |
|
'query' => null, |
92
|
27 |
|
'variables' => null, |
93
|
27 |
|
'operationName' => null, |
94
|
27 |
|
]; |
95
|
|
|
|
96
|
|
|
// Keep a reference to the query-string |
97
|
27 |
|
$qs = $request->query; |
98
|
|
|
|
99
|
|
|
// Override request using query-string parameters |
100
|
27 |
|
$query = $qs->has('query') ? $qs->get('query') : $data['query']; |
101
|
27 |
|
$variables = $qs->has('variables') ? $qs->get('variables') : $data['variables']; |
102
|
27 |
|
$operationName = $qs->has('operationName') ? $qs->get('operationName') : $data['operationName']; |
103
|
|
|
|
104
|
|
|
// `query` parameter is mandatory. |
105
|
27 |
|
if (empty($query)) { |
106
|
1 |
|
throw new BadRequestHttpException('Must provide query parameter'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Variables can be defined using a JSON-encoded object. |
110
|
|
|
// If the parsing fails, an exception will be thrown. |
111
|
26 |
|
if (is_string($variables)) { |
112
|
2 |
|
$variables = json_decode($variables, true); |
113
|
|
|
|
114
|
2 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
115
|
1 |
|
throw new BadRequestHttpException('Variables are invalid JSON'); |
116
|
|
|
} |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
return [ |
120
|
25 |
|
'query' => $query, |
121
|
25 |
|
'variables' => $variables, |
122
|
25 |
|
'operationName' => $operationName, |
123
|
25 |
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|