1

我正在尝试在 iOS 上使用 apollo 客户端实现订阅,我已经查看了这个线程,但它对我不起作用。我仍然得到这个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,错误再次相同。

我这样称呼客户:

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

我缺少任何其他设置吗?这件事真的缺乏文档。

4

1 回答 1

3

此代码段应该可以工作:

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

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

于 2018-09-25T16:32:08.880 回答