ExecutionResult does not follow spec
See original GitHub issueThe spec says
https://facebook.github.io/graphql/#sec-Response-Format
Response Format
A response to a GraphQL operation must be a map.
If the operation included execution, the response map must contain a first entry with key data. The value of this entry is described in the “Data” section. If the operation failed before execution, due to a syntax error, missing information, or validation error, this entry must not be present.
If the operation encountered any errors, the response map must contain a next entry with key errors. The value of this entry is described in the “Errors” section. If the operation completed without encountering any errors, this entry must not be present.
So ExecutionResult is a map like thing that should NOT have an error field if there are no errors and vice versa.
So for example this in invalid
{
"errors": [],
"data": {
"hero": {
"name": "R2-D2"
}
}
}
since it has data in it but no errors
But JSON serialisation libraries such as GSON and others WILL include these fields.
private void returnAsJson(HttpServletResponse response, ExecutionResult executionResult) {
response.setContentType("application/json");
GSON.toJson(executionResult, response.getWriter());
}
I propose that we have a toMap() method on execution result that means its easy for HTTP service layers to implement the spec properly.
private void returnAsJson(HttpServletResponse response, ExecutionResult executionResult) {
response.setContentType("application/json");
GSON.toJson(executionResult.toMap(), response.getWriter());
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
So the spec says that if null is present and there are no errors then that is the result of the query
So graphql JSON encoder code is responsible for setting it up to encode nulls if it wants to follow spec
Because the graphql-java library BY design does not want to get into the game of build JSON serialisation strategies
Would we would need one for Jackson / GSON / JSONP / Java 9 / SimpleJson …