簡體   English   中英

GraphQL查詢/變異的響應

[英]Response of a GraphQL query/mutation

我有一個問題,在下列每種情況下,如何看待GraphQL查詢/變異的響應:

  1. 結果沒有錯誤
  2. 出了點問題,一個或多個錯誤
  3. 有結果和一些錯誤

我不確定后者是否可能,但我似乎記得在某個地方讀到它可能會發生。 例如,在多個突變的情況下,假設兩個,其中每個突變被順序處理。 我認為如果第一個突變很好,可能會發生上面的情況#3,但是在執行第二個突變期間會發生錯誤,但我不確定。

無論如何,回應應該如何? 像下面那些? (JSON中的示例,其中每個都與之前的案例相對應。)或者還有其他方式更慣用嗎? 也許Relay提供了一些關於它應該如何的指導方針? 我找不到任何好的資源。

1:

{
  "data": {
    ...
  }
}

2:

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

3:

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

謝謝。

是的,您的樣本回復看起來正確。 這是“案例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
        }
      ]
    }
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM