Response of a GraphQL query/mutation

落爺英雄遲暮 提交于 2019-12-10 16:00:08

问题


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


回答1:


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
        }
      ]
    }
  ]
}


来源:https://stackoverflow.com/questions/33157168/response-of-a-graphql-query-mutation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!