简体   繁体   中英

Response of a GraphQL query/mutation

I have a question concering how the response of a GraphQL query/mutation should look like in each of the following cases:

  1. There is a result, no errors
  2. Something went wrong, one or more errors
  3. There is both a result and some errors

I'm not sure the latter is even possible, but I seem to remember reading somewhere that it could happen. Eg in the case of multiple mutations, let's say two, where each mutation is processed sequentially. I think case #3 from above could happen if the first mutation is fine, but an error occurs during the execution of the second, but I'm not sure.

Anyway, how should the responses look like? Like the ones below? (Examples in JSON, where each corrensponds to the cases from before.) Or are there other ways that are more idiomatic? Perhaps Relay provide some guidelines as to how it should look like? I couldn't find any good resources for this.

1:

{
  "data": {
    ...
  }
}

2:

{
  "errors": [
    {
      ...
    },
    ...
  ]
}

3:

{
  "data": {
    ...
  },
  "errors": [
    {
      ...
    },
    ...
  ]
}

Thanks.

Yes, your sample responses look right to me. Here's a more detailed example of "case 3".

Sample query with an error in one of the fields

query MyQuery {
  viewer {
    articles(first: 1) {
      edges {
        node {
          title
          tags # we'll introduce an error in the schema here
        }
      }
    }
  }
}

Sample response

{
  "data": {
    "viewer": {
      "articles": {
        "edges": [
          {
            "node": {
              "title": "Sample article title",
              "tags": null
            }
          }
        ]
      }
    }
  },
  "errors": [
    {
      "message": "Cannot read property 'bar' of undefined",
      "locations": [
        {
          "line": 7,
          "column": 11
        }
      ]
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM