gpt4 book ai didi

ios - iOS 上的 Apollo 客户端和 Hasura 身份验证

转载 作者:行者123 更新时间:2023-11-28 05:52:19 27 4
gpt4 key购买 nike

我正在尝试在 iOS 上使用 apollo 客户端实现订阅,我已经研究了 this线程,但它对我不起作用。我仍然收到此 Authentication hook unauthorized this request, code: 1000Operation couldn't be completed Starscream.WSError error 1

我的代码是:

let apollo: ApolloClient = {
let authPayload = ["X-Hasura-Access-Key": "<my_key>",
"Content-Type": "application/json"]

let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = authPayload

let map: GraphQLMap = authPayload

let url = URL(string: "https://url")!
let wsUrl = URL(string: "wss://url")!

let httpNetworkTransport = HTTPNetworkTransport(url: url, configuration: configuration)
let wsNetworkTransport = WebSocketTransport(request: URLRequest(url: wsUrl), connectingPayload: map)

return ApolloClient(networkTransport: SplitNetworkTransport(httpNetworkTransport: httpNetworkTransport,
webSocketNetworkTransport: wsNetworkTransport))
}()

虽然它可以很好地处理查询,但不适用于 websocket 和订阅。我做错了什么?

UPD:这很有趣,但如果我删除 wsNetworkTransport 中的负载,错误仍然存​​在。此外,如果我通过 request.addValue 手动添加请求 header ,错误再次相同。

我这样调用客户端:

apollo.subscribe(subscription: OrdersSubscription(id: "123")) { (result, error) in
error == nil ? print(result!) : print(error!)
}

我还缺少任何其他设置吗?这东西真的缺少文档。

最佳答案

这段代码应该可以工作:

let authPayloads = [
"X-Hasura-Access-Key": "<key>"
]
let map: GraphQLMap = ["headers": authPayloads]
let websocket = WebSocketTransport(request: urlRequest, connectingPayload: map)

有效载荷映射的结构需要稍微不同。

关于ios - iOS 上的 Apollo 客户端和 Hasura 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52491265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com