Skip to content

ExecutionResult does not follow spec #473

Closed

Description

@bbakerman

The 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());
}

Activity

  1. guy120494 commented on May 30, 2017

    @guy120494
    Contributor

    What would happened if execustionResult returns null when error is empty?
    Is there any convention in JSON libraries what happens? I know that in GSON it ignores the null values (unless specific asked not to)

  2. bbakerman commented on May 31, 2017

    @bbakerman
    MemberAuthor

    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

  3. andimarek commented on Jun 18, 2017

    @andimarek
    Member

    See PR #480 for further discussion

  4. added this to the milestone on Aug 9, 2017
  5. tkvw commented on Aug 22, 2017

    @tkvw
  6. bbakerman commented on Aug 24, 2017

    @bbakerman
    MemberAuthor

    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 ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions