How to implement graphql subscription using apollo ios client

后端 未结 3 497
故里飘歌
故里飘歌 2021-01-12 22:32

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

3条回答
  •  清酒与你
    2021-01-12 22:50

    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)
    

    }()

提交回复
热议问题