Skip to content

Instantly share code, notes, and snippets.

View Lavanyagaur22's full-sized avatar
🚩

Lavanya gaur Lavanyagaur22

🚩
View GitHub Profile
mutation createTask ($title:String!, $description :String!) {
createTask(title: $title, description: $description) {
id
title
description
version
}
query allTasksQuery{
allTasks{
id
title
description
version
}
}
mutation updateCurrentTask($id: ID!, $title:String, $version: Int!) {
updateTask(id: $id, title: $title, version:$version) {
id
title
description
version
}
}
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.
private fun getOkhttpClient(context: Context): OkHttpClient? {
//Adding HttpLoggingInterceptor() to see the response body and the results.
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
httpClient?.let {
return it
} ?: kotlin.run {
private fun getConflictInterceptor(context: Context): Interceptor? {
conflictInterceptor?.let {
return it
} ?: kotlin.run {
conflictInterceptor = Interceptor {
val request = it.request()
//all the HTTP work happens, producing a response to satisfy the request.
val response = it.proceed(request)
private fun cacheResolver(): CacheKeyResolver {
return object : CacheKeyResolver() {
override fun fromFieldRecordSet(field: Field, recordSet: MutableMap<String, Any>): CacheKey {
Log.e("UtilClass", "fromFieldRecordSet ${(recordSet["id"] as String)}")
if (recordSet.containsKey("id")) {
val typeNameAndIDKey = recordSet["__typename"].toString() + "." + recordSet["id"]
return CacheKey.from(typeNameAndIDKey)
}
//Toast shown to the user displaying conflict detected.
private fun showToast(context: Context) {
(context as AppCompatActivity).runOnUiThread {
Toast.makeText(context, "Conflict Detected", Toast.LENGTH_SHORT).show()
}
}
fun getTasks() {
Log.e(TAG, "inside getTasks")
val client = Utils.getApolloClient(this)?.query(
AllTasksQuery.builder().build()
)
client?.enqueue(object : ApolloCall.Callback<AllTasksQuery.Data>() {
override fun onFailure(e: ApolloException) {
fun updateTask(id: String, title: String, version: Int) {
Log.e(TAG, "inside update task")
val client = Utils.getApolloClient(this)?.mutate(
UpdateCurrentTask.builder().id(id).title(title).version(version).build()
)
client?.enqueue(object : ApolloCall.Callback<UpdateCurrentTask.Data>() {
override fun onFailure(e: ApolloException) {
Log.e("onFailure" + "updateTask", e.toString())