How to implement graphql subscription using apollo ios client

后端 未结 3 500
故里飘歌
故里飘歌 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 23:00

    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": "", "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:///v1alpha1/graphql"
            let graphQLSubscriptionEndpoint = "ws:///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("", 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.

提交回复
热议问题