Skip to content

Instantly share code, notes, and snippets.

@Lavanyagaur22
Last active December 23, 2019 12:43
Show Gist options
  • Save Lavanyagaur22/494097a29c66a2c0b6035aec911b71be to your computer and use it in GitHub Desktop.
Save Lavanyagaur22/494097a29c66a2c0b6035aec911b71be to your computer and use it in GitHub Desktop.
object Utils {
//To run on emulator use http://10.0.2.2:4000/graphql
const val BASE_URL = "http://10.0.2.2:4000/graphql"
private var apClient: ApolloClient? = null
private var httpClient: OkHttpClient? = null
@JvmStatic
fun getApolloClient(context: Context): ApolloClient? {
//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)!!)
.serverUrl(BASE_URL)
.build()
}
return apClient
}
private fun getOkhttpClient(context: Context): OkHttpClient? {
httpClient?.let {
return it
} ?: kotlin.run {
httpClient = OkHttpClient.Builder()
//Adding HttpLoggingInterceptor() to see the response body and the results.
.addInterceptor(LoggingInterceptor())
.build()
}
return httpClient
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment