Subscription

When we had initially set up Apollo, we used just the network transport. But subscriptions is an advanced use case which needs more configuration. So we have to use ApolloWebSocket.

Now we need to update our ApolloClient instance to point to the subscription server.

Open Network.kt and update the following:

githubNetwork.kt
+ private val GRAPHQL_WEBSOCKET_ENDPOINT: String = "wss://learn.hasura.io/graphql"
apolloClient = ApolloClient.builder()
.serverUrl(GRAPHQL_ENDPOINT)
.okHttpClient(okHttpClient)
.normalizedCache(normalizedCacheFactory, cacheKeyResolver)
+ .subscriptionTransportFactory(WebSocketSubscriptionTransport.Factory(GRAPHQL_WEBSOCKET_ENDPOINT, okHttpClient))
.addCustomTypeAdapter(CustomType.TIMESTAMPTZ, dateCustomTypeAdapter)
.build()

Note that we have added a new subscriptionTransportFactory with GRAPHQL_WEBSOCKET_ENDPOINT and hence all GraphQL subscription queries can go through a websocket connection.