【问题标题】:Response of a GraphQL query/mutationGraphQL 查询/突变的响应
【发布时间】:2016-01-14 10:06:05
【问题描述】:

我有一个关于 GraphQL 查询/突变的响应在以下每种情况下应该是什么样子的问题:

  1. 有结果,没有错误
  2. 出了点问题,一个或多个错误
  3. 既有结果又有一些错误

我不确定后者是否可能,但我似乎记得在某处读过它可能会发生。例如。在多个突变的情况下,假设有两个,其中每个突变都是按顺序处理的。如果第一个突变很好,我认为上面的案例 #3 可能会发生,但是在第二个突变的执行过程中会发生错误,但我不确定。

无论如何,响应应该是什么样子?像下面的那些? (JSON 中的示例,每个都对应于以前的案例。)或者还有其他更惯用的方法吗?也许 Relay 提供了一些关于它应该是什么样子的指导方针?我找不到任何好的资源。

1:

{
  "data": {
    ...
  }
}

2:

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

3:

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

谢谢。

【问题讨论】:

    标签: graphql relayjs


    【解决方案1】:

    是的,您的示例回复在我看来是正确的。这是“案例3”的更详细示例。

    其中一个字段出错的示例查询

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

    示例响应

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

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 2021-09-09
      • 2018-09-17
      • 2022-01-13
      • 2018-11-10
      • 2020-01-17
      • 2019-11-18
      • 2018-06-18
      • 2017-10-18
      相关资源
      最近更新 更多