1

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.

Max Kraev
  • 740
  • 12
  • 31
  • check your Access-Key , this error apparent because you don't have Authentication – a.masri Sep 25 '18 at 05:49
  • @a.masri I double checked it, it authenticates queries, but not the subscriptions – Max Kraev Sep 25 '18 at 05:50
  • base the apollo client doc , the auth type Bearer not access- key , check this doc https://www.apollographql.com/docs/ios/initialization.html – a.masri Sep 25 '18 at 06:23
  • WebSocketTransport seems to be unresponsive to header changes, that's weird, I tried to change the headers, but it didn't work - the same error – Max Kraev Sep 25 '18 at 11:02

1 Answers1

3

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.

iamnat
  • 4,056
  • 1
  • 23
  • 36
  • Yes, I just contacted the Hasura support and they really helped. https://github.com/hasura/graphql-engine/issues/503. 100% works for me. Oh, you're from Hasura :) Thanks again! – Max Kraev Sep 25 '18 at 16:32
  • Hallelujah, Praise t̶h̶e̶ ̶L̶o̶r̶d̶ iamnat !! I've been looking all over to figure this out. – Tycho Pandelaar Apr 01 '20 at 14:31