簡體   English   中英

無法檢索 flutter 項目中 Graphql 突變中發生的錯誤

[英]Unable to retrieve errors occured in Graphql mutation in flutter project

我在我的 flutter 應用程序中使用 package graphql_flutter進行 GraphQL 操作。 查詢和突變進展順利,但我無法按照他們文檔中提到的方式檢索錯誤。 每次我收到一般錯誤消息時,

ClientException: Failed to connect to http://127.0.0.1:3006/graphql: 

我做得到,

print(result.exception.toString());

我的突變看起來像,

final MutationOptions mutationOptions = MutationOptions(
  documentNode: gql(mutationString),
  variables: vars
);

final QueryResult result = await _instance._client.mutate(mutationOptions);

if (result.hasException) {
  // none of the following prints the expected error.
  print(result.exception.clientException.message);
  print(result.exception.graphqlErrors);
  print(result.exception.toString());
}

print(result.data);

return result.data;

而在阿波羅客戶端,我的錯誤是:

{
  "errors": [
    {
      "message": "Invalid Phone number provided",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "otp"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
         ....

但我什么都沒有。

注意:成功響應如期而至。 我想知道如何獲得 graphql 錯誤。

我發現了問題。 這是因為 android 無法從模擬器連接到127.0.0.1localhost 我用我的本地 IP 地址替換了主機,現在它工作正常。

把它放在一個 try/catch 塊中,看看它是否可以捕獲任何異常

final QueryResult result = await _instance._client.mutate(mutationOptions);

最初的問題是如何獲得 graphql 錯誤。 我正在使用 graphql: ^5.0.0

我檢查了文檔並找到了這個例子:

if (result.hasException) {
  if (result.exception.linkException is NetworkException) {
     // handle network issues, maybe
    }
   return Text(result.exception.toString())
 }

但這只是給了我異常,而不是錯誤,我將結果異常轉換為錯誤類型並能夠得到消息:

if (result.hasException) {
    if (result.exception!.linkException is ServerException) {
      ServerException exception =
          result.exception!.linkException as ServerException;
      var errorMessage = exception.parsedResponse!.errors![0].message;
      print(errorMessage);
      throw Exception(errorMessage);
    }
  } 

對於一個簡單的消息來說,這似乎需要做很多工作,我想知道是否還有另一種更簡單的內置方法可以做到這一點

暫無
暫無

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

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