|
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
|
55 |
|
public function parse(Request $request) |
|
25
|
|
|
{ |
|
26
|
|
|
// Extracts the GraphQL request parameters |
|
27
|
55 |
|
$parsedBody = $this->getParsedBody($request); |
|
28
|
53 |
|
$data = $this->getParams($request, $parsedBody); |
|
29
|
|
|
|
|
30
|
51 |
|
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
|
55 |
|
private function getParsedBody(Request $request) |
|
41
|
|
|
{ |
|
42
|
55 |
|
$body = $request->getContent(); |
|
43
|
55 |
|
$type = explode(';', $request->headers->get('content-type'))[0]; |
|
44
|
|
|
|
|
45
|
|
|
switch ($type) { |
|
46
|
|
|
// Plain string |
|
47
|
55 |
|
case static::CONTENT_TYPE_GRAPHQL: |
|
48
|
3 |
|
$parsedBody = [static::PARAM_QUERY => $body]; |
|
49
|
3 |
|
break; |
|
50
|
|
|
|
|
51
|
|
|
// JSON object |
|
52
|
52 |
|
case static::CONTENT_TYPE_JSON: |
|
53
|
2 |
|
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
|
1 |
|
$parsedBody = json_decode($body, true); |
|
58
|
|
|
|
|
59
|
1 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
60
|
1 |
|
throw new BadRequestHttpException('POST body sent invalid JSON'); |
|
61
|
|
|
} |
|
62
|
|
|
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
|
53 |
|
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
|
53 |
|
private function getParams(Request $request, array $data = []) |
|
87
|
|
|
{ |
|
88
|
|
|
// Add default request parameters |
|
89
|
53 |
|
$data = array_filter($data) + [ |
|
90
|
53 |
|
static::PARAM_QUERY => null, |
|
91
|
53 |
|
static::PARAM_VARIABLES => null, |
|
92
|
53 |
|
static::PARAM_OPERATION_NAME => null, |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
// Keep a reference to the query-string |
|
96
|
53 |
|
$qs = $request->query; |
|
97
|
|
|
|
|
98
|
|
|
// Override request using query-string parameters |
|
99
|
53 |
|
$query = $qs->has(static::PARAM_QUERY) ? $qs->get(static::PARAM_QUERY) : $data[static::PARAM_QUERY]; |
|
100
|
53 |
|
$variables = $qs->has(static::PARAM_VARIABLES) ? $qs->get(static::PARAM_VARIABLES) : $data[static::PARAM_VARIABLES]; |
|
101
|
53 |
|
$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
|
53 |
|
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
|
52 |
|
if (is_string($variables)) { |
|
111
|
1 |
|
$variables = json_decode($variables, true); |
|
112
|
|
|
|
|
113
|
1 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
114
|
1 |
|
throw new BadRequestHttpException('Variables are invalid JSON'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return [ |
|
119
|
51 |
|
static::PARAM_QUERY => $query, |
|
120
|
51 |
|
static::PARAM_VARIABLES => $variables, |
|
121
|
51 |
|
static::PARAM_OPERATION_NAME => $operationName, |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|