This file contains 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
mutation createTask ($title:String!, $description :String!) { | |
createTask(title: $title, description: $description) { | |
id | |
title | |
description | |
version | |
} |
This file contains 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
query allTasksQuery{ | |
allTasks{ | |
id | |
title | |
description | |
version | |
} | |
} |
This file contains 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
mutation updateCurrentTask($id: ID!, $title:String, $version: Int!) { | |
updateTask(id: $id, title: $title, version:$version) { | |
id | |
title | |
description | |
version | |
} | |
} |
This file contains 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
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. |
This file contains 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
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 { |
This file contains 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
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) |
This file contains 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
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) | |
} |
This file contains 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
//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() | |
} | |
} |
This file contains 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
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) { |
This file contains 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
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()) |
OlderNewer