Last active
February 25, 2020 20:53
-
-
Save jsnowfreedman/d36f9e21495238487f97260368557a03 to your computer and use it in GitHub Desktop.
This file contains 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
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); |
This file contains 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 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