Skip to content

Instantly share code, notes, and snippets.

@jsnowfreedman
Last active February 25, 2020 20:53
Show Gist options
  • Save jsnowfreedman/d36f9e21495238487f97260368557a03 to your computer and use it in GitHub Desktop.
Save jsnowfreedman/d36f9e21495238487f97260368557a03 to your computer and use it in GitHub Desktop.
Spark.post("/graphql", (request, response) -> {
response.type("application/json");
final String body = request.body();
final GqlRequestBody gqlRequestBody = gson.fromJson(body, GqlRequestBody.class);
if (gqlRequestBody.getQuery() != null) {
ExecutionInput.Builder executionInput = ExecutionInput.newExecutionInput().query(gqlRequestBody.getQuery());
if (gqlRequestBody.getVariables() != null) {
executionInput.variables(gqlRequestBody.getVariables());
}
if (gqlRequestBody.getOperationName() != null) {
executionInput.operationName(gqlRequestBody.getOperationName());
}
return build.execute(executionInput.build()).toSpecification();
} else {
return "";
}
}, gson::toJson);
public static class GqlRequestBody {
String query;
Map<String, Object> variables;
String operationName;
@Override
public String toString() {
return new StringJoiner(", ", GqlRequestBody.class.getSimpleName() + "[", "]")
.add("query='" + query + "'")
.add("variables=" + variables)
.add("operationName='" + operationName + "'")
.toString();
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public Map<String, Object> getVariables() {
return variables;
}
public void setVariables(Map<String, Object> variables) {
this.variables = variables;
}
public String getOperationName() {
return operationName;
}
public void setOperationName(String operationName) {
this.operationName = operationName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment