【问题标题】:How to get error response from hasura graphql query in iOS如何从 iOS 中的 hasura graphql 查询中获取错误响应
【发布时间】:2021-10-02 07:06:41
【问题描述】:

我正在使用 hasura 进行查询。

    ApolloNetworkClass.shared.apolloClient.fetch(query: GetCollectionsQuery(user_id: "120"), cachePolicy: .fetchIgnoringCacheData) { (result) in
        switch result
        {
        case .success(let result) :
            if let array = result.data?.nftCollections.map({$0.fragments.nftCollectionsModel})
            {
                self.collectionList = array
                
                self.tblCollections.reloadData()
            }
            else if let error = result.errors {
                if let errorResponse = error.first {
                    print(errorResponse)
                } else {
                    print("[Error] Got errors when call request: \(error)")
                }
            }
            break
        case .failure(let error) :
            Utility.ShowToast(message: error.localizedDescription, position: .top)
            print(error)
            break
        }
    }

这是我的查询,我只想获取错误代码或响应代码(如 401)。

我已经尝试了所有方法,但我无法获得解决方案。

提前致谢。

【问题讨论】:

    标签: ios swift graphql apollo hasura


    【解决方案1】:

    GraphQL 错误响应仍将返回状态代码 200,这与 REST API 模式不同,如果出现问题,您将收到 4xx。

    为了解析错误消息,您需要查看响应对象,其格式为:

    {
      "data": ....,
      "errors": []
    }
    
    You can then expand on the errors object which will contain information like the error code, location of the error via some path etc.
    

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 1970-01-01
      • 2021-01-11
      • 2021-08-19
      • 2020-08-13
      • 2021-11-18
      • 2021-12-20
      • 2018-08-04
      • 2016-11-12
      相关资源
      最近更新 更多