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://hasura.io/learn/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.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin
graphql-handbook