Skip to content

Instantly share code, notes, and snippets.

@Lavanyagaur22
Last active April 6, 2019 07:40
Show Gist options
  • Save Lavanyagaur22/2ca4699609461b546579e3810638e563 to your computer and use it in GitHub Desktop.
Save Lavanyagaur22/2ca4699609461b546579e3810638e563 to your computer and use it in GitHub Desktop.
fun getApolloClient(context: Context): ApolloClient? {
val apolloSqlHelper = ApolloSqlHelper(context, SQL_CACHE_NAME)
//Used Normalized Disk Cache: Per node caching of responses in SQL.
//Persists normalized responses on disk so that they can used after process death.
val cacheFactory = LruNormalizedCacheFactory(
EvictionPolicy.NO_EVICTION, SqlNormalizedCacheFactory(apolloSqlHelper)
)
//If Apollo Client is not null, return it else make a new Apollo Client.
//Helps in singleton pattern.
apClient?.let {
return it
} ?: kotlin.run {
apClient = ApolloClient.builder()
.okHttpClient(getOkhttpClient(context)!!)
.normalizedCache(cacheFactory, cacheResolver())
.serverUrl(BASE_URL)
.build()
}
return apClient
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment