Skip to content

Instantly share code, notes, and snippets.

@nwillc
Last active November 20, 2017 04:09
Show Gist options
  • Save nwillc/857f6f264991753977b99e6b5db77959 to your computer and use it in GitHub Desktop.
Save nwillc/857f6f264991753977b99e6b5db77959 to your computer and use it in GitHub Desktop.
A Ratpack GraphQL query handler
public class GraphQLHandler implements Handler {
// ...
@Override
public void handle(Context context) throws Exception {
context.parse(Map.class).then(payload -> {
Map<String, Object> variables =
(Map<String, Object>) payload.get("variables");
ExecutionInput executionInput =
ExecutionInput.newExecutionInput()
.query(payload.get("query").toString())
.variables(variables)
.build();
final ExecutionResult executionResult =
graphql.execute(executionInput);
Map<String, Object> result = new LinkedHashMap<>();
if (executionResult.getErrors().isEmpty()) {
result.put("data", executionResult.getData());
} else {
result.put("errors", executionResult.getErrors());
}
context.render(json(result));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment