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 implements ParserInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param Request $request |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
56 |
|
public function parse(Request $request) |
25
|
|
|
{ |
26
|
|
|
// Extracts the GraphQL request parameters |
27
|
56 |
|
$parsedBody = $this->getParsedBody($request); |
28
|
54 |
|
$data = $this->getParams($request, $parsedBody); |
29
|
|
|
|
30
|
52 |
|
return $data; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Gets the body from the request based on Content-Type header. |
35
|
|
|
* |
36
|
|
|
* @param Request $request |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
56 |
|
private function getParsedBody(Request $request) |
41
|
|
|
{ |
42
|
56 |
|
$body = $request->getContent(); |
43
|
56 |
|
$type = explode(';', $request->headers->get('content-type'))[0]; |
44
|
|
|
|
45
|
|
|
switch ($type) { |
46
|
|
|
// Plain string |
47
|
56 |
|
case static::CONTENT_TYPE_GRAPHQL: |
48
|
3 |
|
$parsedBody = [static::PARAM_QUERY => $body]; |
49
|
3 |
|
break; |
50
|
|
|
|
51
|
|
|
// JSON object |
52
|
53 |
|
case static::CONTENT_TYPE_JSON: |
53
|
3 |
|
if (empty($body)) { |
54
|
1 |
|
throw new BadRequestHttpException('The request content body must not be empty when using json content type request.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$parsedBody = json_decode($body, true); |
58
|
|
|
|
59
|
2 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
60
|
1 |
|
throw new BadRequestHttpException('POST body sent invalid JSON'); |
61
|
|
|
} |
62
|
1 |
|
break; |
63
|
|
|
|
64
|
|
|
// URL-encoded query-string |
65
|
50 |
|
case static::CONTENT_TYPE_FORM: |
66
|
49 |
|
case static::CONTENT_TYPE_FORM_DATA: |
67
|
1 |
|
$parsedBody = $request->request->all(); |
68
|
1 |
|
break; |
69
|
|
|
|
70
|
|
|
default: |
71
|
49 |
|
$parsedBody = []; |
72
|
49 |
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
54 |
|
return $parsedBody; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets the GraphQL parameters from the request. |
80
|
|
|
* |
81
|
|
|
* @param Request $request |
82
|
|
|
* @param array $data |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
54 |
|
private function getParams(Request $request, array $data = []) |
87
|
|
|
{ |
88
|
|
|
// Add default request parameters |
89
|
54 |
|
$data = array_filter($data) + [ |
90
|
54 |
|
static::PARAM_QUERY => null, |
91
|
54 |
|
static::PARAM_VARIABLES => null, |
92
|
54 |
|
static::PARAM_OPERATION_NAME => null, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
// Keep a reference to the query-string |
96
|
54 |
|
$qs = $request->query; |
97
|
|
|
|
98
|
|
|
// Override request using query-string parameters |
99
|
54 |
|
$query = $qs->has(static::PARAM_QUERY) ? $qs->get(static::PARAM_QUERY) : $data[static::PARAM_QUERY]; |
100
|
54 |
|
$variables = $qs->has(static::PARAM_VARIABLES) ? $qs->get(static::PARAM_VARIABLES) : $data[static::PARAM_VARIABLES]; |
101
|
54 |
|
$operationName = $qs->has(static::PARAM_OPERATION_NAME) ? $qs->get(static::PARAM_OPERATION_NAME) : $data[static::PARAM_OPERATION_NAME]; |
102
|
|
|
|
103
|
|
|
// `query` parameter is mandatory. |
104
|
54 |
|
if (empty($query)) { |
105
|
1 |
|
throw new BadRequestHttpException('Must provide query parameter'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Variables can be defined using a JSON-encoded object. |
109
|
|
|
// If the parsing fails, an exception will be thrown. |
110
|
53 |
|
if (is_string($variables)) { |
111
|
2 |
|
$variables = json_decode($variables, true); |
112
|
|
|
|
113
|
2 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
114
|
1 |
|
throw new BadRequestHttpException('Variables are invalid JSON'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return [ |
119
|
52 |
|
static::PARAM_QUERY => $query, |
120
|
52 |
|
static::PARAM_VARIABLES => $variables, |
121
|
52 |
|
static::PARAM_OPERATION_NAME => $operationName, |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|