如何从iOS中的hasura graphql查询获得错误响应

问题描述

我正在使用 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)。

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

提前致谢。

解决方法

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.