1
votes

I'm trying to implement a subscription using apollo client on iOS and I've looked into this thread but it doesn't work for me. I'm still getting this Authentication hook unauthorized this request, code: 1000 or Operation couldn't be completed Starscream.WSError error 1

My code is:

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))
}()

It's working fine with queries though, but not with websocket and subscriptions. What am I doing wrong?

UPD: It's interesting, but if I remove the payload in the wsNetworkTransport, the error stays the same. Also if I add the request headers manually through request.addValue, the error again the same.

I'm calling the client like so:

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

Is there any additional setup I'm missing? This thing really lack the docs.

1
check your Access-Key , this error apparent because you don't have Authenticationa.masri
@a.masri I double checked it, it authenticates queries, but not the subscriptionsMax Kraev
base the apollo client doc , the auth type Bearer not access- key , check this doc apollographql.com/docs/ios/initialization.htmla.masri
WebSocketTransport seems to be unresponsive to header changes, that's weird, I tried to change the headers, but it didn't work - the same errorMax Kraev

1 Answers

2
votes

This code snippet should work:

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

The payloads map needs to be structured a little differently.