Last active
December 23, 2019 12:43
-
-
Save Lavanyagaur22/494097a29c66a2c0b6035aec911b71be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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