简体   繁体   English

GraphQL查询/变异的响应

[英]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: 我有一个问题,在下列每种情况下,如何看待GraphQL查询/变异的响应:

  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. 我认为如果第一个突变很好,可能会发生上面的情况#3,但是在执行第二个突变期间会发生错误,但我不确定。

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? (JSON中的示例,其中每个都与之前的案例相对应。)或者还有其他方式更惯用吗? Perhaps Relay provide some guidelines as to how it should look like? 也许Relay提供了一些关于它应该如何的指导方针? I couldn't find any good resources for this. 我找不到任何好的资源。

1: 1:

{
  "data": {
    ...
  }
}

2: 2:

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

3: 3:

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

Thanks. 谢谢。

Yes, your sample responses look right to me. 是的,您的样本回复看起来正确。 Here's a more detailed example of "case 3". 这是“案例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
        }
      ]
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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