How to implement graphql subscription using apollo ios client
Asked Answered
W

3

7

I am trying to implement graphql subscription using apollo ios client. But not able to figure it out as lack of documentation examples. Example given on apollo documentation is:

let apollo: ApolloClient = {
  let configuration = URLSessionConfiguration.default
  // Add additional headers as needed
  configuration.httpAdditionalHeaders = ["Authorization": "Bearer <token>"] // Replace `<token>`

  let url = URL(string: "http://localhost:8080/graphql")!

  return ApolloClient(networkTransport: HTTPNetworkTransport(url: url, configuration: configuration))
}()

APOLLO IOS GUIDE: Creating a client

Washin answered 7/8, 2018 at 6:41 Comment(0)
W
7

Implement the subscription in apollo ios graphql client by following below steps.

  1. Using cocoapods:

    1. pod 'Apollo'
    2. pod 'Apollo/WebSocket'
    3. pod install
  2. To create client to support subscription and authentication. Add below code in AppDelegate.swift:

    1. Websocket - we have to use WebSocketTransport and URLRequest
    2. Authentication - we have to pass auth parameters in connection params connectingPayload to server. And for http we are passing it in headers as mentioned in question snippet.
    3. SplitNetworkTransport- To combine both http and websocket to create client. we have to use httpNetworkTransport and webSocketNetworkTransport


lazy var apollo: ApolloClient = {
    let authPayloads = [
        "Authorization": "Bearer "
    ]
    let configuration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = authPayloads

let map: GraphQLMap = authPayloads 
let wsEndpointURL = URL(string: "ws://localhost:8080/subscriptions")!
let endpointURL = URL(string: "http://localhost:8080/api")!
let websocket = WebSocketTransport(request: URLRequest(url: wsEndpointURL), connectingPayload: map)
let splitNetworkTransport = SplitNetworkTransport(
    httpNetworkTransport: HTTPNetworkTransport(
        url: endpointURL,
        configuration: configuration
    ), 
    webSocketNetworkTransport: websocket
)
return ApolloClient(networkTransport: splitNetworkTransport)

}()

Washin answered 7/8, 2018 at 6:41 Comment(2)
from where I get wsEndpointURL? and what is the next step I have to do to update UI?Fleta
can you help me, pleaseFleta
A
2

I'm getting close. I was getting rejected for not having the correct headers in my Websocket upgrade. I ended up having to set them directly on the URLRequest object.

    var apollo: ApolloClient? {
        let authHeaders = ["X-Hasura-Access-Key": "<my_Key>", "Content-Type": "application/json"]

        let configuration = URLSessionConfiguration.default
        // Add additional headers as needed
        configuration.httpAdditionalHeaders = authHeaders

        //The string to my graph QL Server run by Hasure on AWS RDS.
        let graphQLEndpoint = "http://<my_host>/v1alpha1/graphql"
        let graphQLSubscriptionEndpoint = "ws://<my_host>/v1alpha1/graphql"
        //Take my Ec2 Server string and make a URL for the graph QL and subscriptions
        guard let httpURL = URL(string: graphQLEndpoint), let webSocketURL = URL(string: graphQLSubscriptionEndpoint) else {
            return nil
        }

        let httpTransport = HTTPNetworkTransport(url: httpURL, configuration: configuration, sendOperationIdentifiers: false)

        var request = URLRequest(url: webSocketURL)

        request.setValue("<my_key>", forHTTPHeaderField: "X-Hasura-Access-Key")

        request.setValue("application/json", forHTTPHeaderField: "Content-Type")

        let webSocketTransport = WebSocketTransport(request: request, sendOperationIdentifiers: false, connectingPayload: nil)

        let splitTransport = SplitNetworkTransport(httpNetworkTransport: httpTransport, webSocketNetworkTransport: webSocketTransport)

        //Initalize the APolloClient with that URL.
        return ApolloClient(networkTransport: splitTransport)
    }

The upgrade worked after that.

Artamas answered 17/9, 2018 at 23:14 Comment(0)
S
2

Here's my ApolloClient setup for just a web socket transport client:

let connectingPayload = ["authToken": accessToken]
let urlRequest = URLRequest(url: baseURL)
let webSocketTransport = WebSocketTransport(request: urlRequest, sendOperationIdentifiers: false, connectingPayload: connectingPayload)

let apollo = ApolloClient(networkTransport: webSocketTransport)
Saddlebag answered 19/9, 2018 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.