Skip to content

Instantly share code, notes, and snippets.

@nwillc
Created October 13, 2016 02:37
Show Gist options
  • Save nwillc/1b168093b8f28bd383374bc37b3b11ca to your computer and use it in GitHub Desktop.
Save nwillc/1b168093b8f28bd383374bc37b3b11ca to your computer and use it in GitHub Desktop.
Attaching graphql-java to sparkjava
private Map<String, Object> graphql(Request request, Response response) {
Map<String, Object> payload;
payload = getMapper().readValue(request.body(), Map.class);
Map<String,Object> variables = (Map<String, Object>) payload.get("variables");
ExecutionResult executionResult = graphql.execute(payload.get("query").toString(), null, null, variables);
Map<String, Object> result = new LinkedHashMap<>();
if (executionResult.getErrors().size() > 0) {
result.put("errors", executionResult.getErrors());
}
result.put("data", executionResult.getData());
response.type("application/json");
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment