Skip to content

Commit 982a19e

Browse files
committed
Removed HttpKit malarky
1 parent f15b174 commit 982a19e

4 files changed

Lines changed: 11 additions & 78 deletions

File tree

src/test/groovy/example/http/HttpKit.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/test/groovy/example/http/HttpMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ private void handleStarWars(HttpServletRequest request, HttpServletResponse resp
5959
// this builds out the parameters we need like the graphql query
6060
QueryParameters parameters = QueryParameters.from(request);
6161
if (parameters.getQuery() == null) {
62+
//
63+
// how to handle nonsensical requests is up to your application
6264
response.setStatus(400);
6365
return;
6466
}

src/test/groovy/example/http/JsonKit.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
public class JsonKit {
1717
static final Gson GSON = new GsonBuilder()
18+
//
19+
// This is important because the graphql spec says that null values should be present
20+
//
1821
.serializeNulls()
1922
.create();
2023

src/test/groovy/example/http/QueryParameters.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Collections;
77
import java.util.HashMap;
88
import java.util.Map;
9-
import java.util.Optional;
109

1110
/**
1211
* Graphql clients can send GET or POST HTTP requests. The spec does not make an explicit
@@ -42,43 +41,20 @@ public Map<String, Object> getVariables() {
4241
static QueryParameters from(HttpServletRequest request) {
4342
QueryParameters parameters = new QueryParameters();
4443
if ("POST".equalsIgnoreCase(request.getMethod())) {
45-
//
46-
// the graphql guidance says :
47-
//
48-
// If the "application/graphql" Content-Type header is present, treat
49-
// the HTTP POST body contents as the GraphQL query string.
50-
if ("application/graphql".equals(request.getContentType())) {
51-
parameters.query = readPostBody(request);
52-
} else {
53-
Map<String, Object> json = readJSON(request);
54-
55-
//
56-
// the graphql guidance says :
57-
//
58-
// - If the "query" query string parameter is present
59-
// - ..., it should be parsed and handled
60-
// - in the same way as the HTTP GET case.
61-
//
62-
Optional<String> queryStr = getQueryQueryParameter(request);
63-
parameters.query = queryStr.orElseGet(() -> (String) json.get("query"));
64-
parameters.operationName = (String) json.get("operationName");
65-
parameters.variables = getVariablesFromPost(json.get("variables"));
66-
}
44+
Map<String, Object> json = readJSON(request);
45+
parameters.query = (String) json.get("query");
46+
parameters.operationName = (String) json.get("operationName");
47+
parameters.variables = getVariables(json.get("variables"));
6748
} else {
6849
parameters.query = request.getParameter("query");
6950
parameters.operationName = request.getParameter("operationName");
70-
parameters.variables = getVariablesFromPost(request.getParameter("variables"));
51+
parameters.variables = getVariables(request.getParameter("variables"));
7152
}
7253
return parameters;
7354
}
7455

75-
private static Optional<String> getQueryQueryParameter(HttpServletRequest request) {
76-
// http servlet spec getParameter() does not distinguish between POST body or GET query strings
77-
// so we have to parse for it in this case
78-
return HttpKit.getQueryParameter(request,"query");
79-
}
8056

81-
private static Map<String, Object> getVariablesFromPost(Object variables) {
57+
private static Map<String, Object> getVariables(Object variables) {
8258
if (variables instanceof Map) {
8359
Map<?, ?> inputVars = (Map) variables;
8460
Map<String, Object> vars = new HashMap<>();

0 commit comments

Comments
 (0)