Skip to content

Commit 4daa7b5

Browse files
committed
- Modify variables setter to use JSON format instead of string format, as is now document in the latest specification here : http://graphql.org/learn/serving-over-http/#post-request
This change is also now done on GraphiQL : graphql/graphiql#168 - Add support for GET parameters (query and variables) as is also specified here : http://graphql.org/learn/serving-over-http/#get-request - Fix some NPEs when no parameters where specified.
1 parent e70580b commit 4daa7b5

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/main/java/graphql/servlet/GraphQLServlet.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,8 @@ public String executeQuery(String query) {
168168
public static class Request {
169169
@Getter @Setter
170170
private String query;
171-
@Getter
171+
@Getter @Setter
172172
private Map<String, Object> variables = new HashMap<>();
173-
174-
@SneakyThrows
175-
public void setVariables(String variables) {
176-
this.variables = new ObjectMapper().readValue(variables, new TypeReference<Map<String, Object>>() {});
177-
}
178-
179173
@Getter @Setter
180174
private String operationName;
181175
}
@@ -190,7 +184,15 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
190184
if (path.contentEquals("/schema.json")) {
191185
query(CharStreams.toString(new InputStreamReader(GraphQLServlet.class.getResourceAsStream("introspectionQuery"))), new HashMap<>(), schema, req, resp, context);
192186
} else {
193-
query(req.getParameter("q"), new HashMap<>(), readOnlySchema, req, resp, context);
187+
if (req.getParameter("q") != null) {
188+
query(req.getParameter("q"), new HashMap<>(), readOnlySchema, req, resp, context);
189+
} else if (req.getParameter("query") != null) {
190+
Map<String,Object> variables = new HashMap<>();
191+
if (req.getParameter("variables") != null) {
192+
variables.putAll(new ObjectMapper().readValue(req.getParameter("variables"), new TypeReference<Map<String,Object>>() {}));
193+
}
194+
query(req.getParameter("query"), variables, readOnlySchema, req, resp, context);
195+
}
194196
}
195197
}
196198

@@ -220,7 +222,11 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
220222
inputStream = req.getInputStream();
221223
}
222224
Request request = new ObjectMapper().readValue(inputStream, Request.class);
223-
query(request.query, request.variables, schema, req, resp, context);
225+
Map<String,Object> variables = request.variables;
226+
if (variables == null) {
227+
variables = new HashMap<>();
228+
}
229+
query(request.query, variables, schema, req, resp, context);
224230
}
225231

226232
private void query(String query, Map<String, Object> variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException {

0 commit comments

Comments
 (0)