Last active
November 20, 2017 04:09
-
-
Save nwillc/857f6f264991753977b99e6b5db77959 to your computer and use it in GitHub Desktop.
A Ratpack GraphQL query handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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