-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathGraphQLController.php
More file actions
201 lines (168 loc) · 5.9 KB
/
GraphQLController.php
File metadata and controls
201 lines (168 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Date: 25.11.15
*
* @author Portey Vasil <portey@gmail.com>
*/
namespace Youshido\GraphQLBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Youshido\GraphQL\Exception\ConfigurationException;
use Youshido\GraphQLBundle\Exception\UnableToInitializeSchemaServiceException;
use Youshido\GraphQLBundle\Execution\Processor;
class GraphQLController extends AbstractController
{
/**
* @Route("/graphql")
*
* @throws \Exception
*
* @return JsonResponse
*/
public function defaultAction()
{
try {
$this->initializeSchemaService();
} catch (UnableToInitializeSchemaServiceException $e) {
return new JsonResponse(
[['message' => 'Schema class ' . $this->getSchemaClass() . ' does not exist']],
200,
$this->getResponseHeaders()
);
}
if ($this->get('request_stack')->getCurrentRequest()->getMethod() == 'OPTIONS') {
return $this->createEmptyResponse();
}
list($queries, $isMultiQueryRequest) = $this->getPayload();
$queryResponses = array_map(function($queryData) {
return $this->executeQuery($queryData['query'], $queryData['variables']);
}, $queries);
$response = new JsonResponse($isMultiQueryRequest ? $queryResponses : $queryResponses[0], 200, $this->getParameter('graphql.response.headers'));
if ($this->getParameter('graphql.response.json_pretty')) {
$response->setEncodingOptions($response->getEncodingOptions() | JSON_PRETTY_PRINT);
}
return $response;
}
private function createEmptyResponse()
{
return new JsonResponse([], 200, $this->getResponseHeaders());
}
private function executeQuery($query, $variables)
{
/** @var Processor $processor */
$processor = $this->get('graphql.processor');
$processor->processPayload($query, $variables);
return $processor->getResponseData();
}
/**
* @return array
*
* @throws \Exception
*/
private function getPayload()
{
$request = $this->get('request_stack')->getCurrentRequest();
$query = $request->get('query', null);
$variables = $request->get('variables', []);
$isMultiQueryRequest = false;
$queries = [];
$variables = is_string($variables) ? json_decode($variables, true) ?: [] : [];
$content = $request->getContent();
if (!empty($content)) {
if ($request->headers->has('Content-Type') && 'application/graphql' == $request->headers->get('Content-Type')) {
$queries[] = [
'query' => $content,
'variables' => [],
];
} else {
$params = json_decode($content, true);
if ($params) {
// check for a list of queries
if (isset($params[0]) === true) {
$isMultiQueryRequest = true;
} else {
$params = [$params];
}
foreach ($params as $queryParams) {
$query = isset($queryParams['query']) ? $queryParams['query'] : $query;
if (isset($queryParams['variables'])) {
if (is_string($queryParams['variables'])) {
$variables = json_decode($queryParams['variables'], true) ?: $variables;
} else {
$variables = $queryParams['variables'];
}
$variables = is_array($variables) ? $variables : [];
}
$queries[] = [
'query' => $query,
'variables' => $variables,
];
}
}
}
} else {
$queries[] = [
'query' => $query,
'variables' => $variables,
];
}
return [$queries, $isMultiQueryRequest];
}
/**
* @throws \Exception
*/
private function initializeSchemaService()
{
if ($this->container->initialized('graphql.schema')) {
return;
}
$this->container->set('graphql.schema', $this->makeSchemaService());
}
/**
* @return object
*
* @throws \Exception
*/
private function makeSchemaService()
{
if ($this->container->has($this->getSchemaService())) {
return $this->container->get($this->getSchemaService());
}
$schemaClass = $this->getSchemaClass();
if (!$schemaClass || !class_exists($schemaClass)) {
throw new UnableToInitializeSchemaServiceException();
}
if ($this->container->has($schemaClass)) {
return $this->container->get($schemaClass);
}
$schema = new $schemaClass();
if ($schema instanceof ContainerAwareInterface) {
$schema->setContainer($this->container);
}
return $schema;
}
/**
* @return string
*/
private function getSchemaClass()
{
return $this->getParameter('graphql.schema_class');
}
/**
* @return string
*/
private function getSchemaService()
{
$serviceName = $this->getParameter('graphql.schema_service');
if (substr($serviceName, 0, 1) === '@') {
return substr($serviceName, 1, strlen($serviceName) - 1);
}
return $serviceName;
}
private function getResponseHeaders()
{
return $this->getParameter('graphql.response.headers');
}
}