Mutation and update cache
Now let's do the integration part. Open TaskFragment.kt
and add the following code to define the delete mutation
private fun removeTodoMutationCloud(todoId: Int){// Init QueryremoveTodoMutation = RemoveTodoMutation.builder().id(todoId).build()// Apollo runs query on background threadNetwork.apolloClient.mutate(removeTodoMutation)?.enqueue(object : ApolloCall.Callback<RemoveTodoMutation.Data>() {override fun onFailure(error: ApolloException) {Log.d("Todo", error.toString() )}override fun onResponse(@NotNull response: Response<RemoveTodoMutation.Data>) {// get data from local cache and update the listval index = listItems?.indexOfFirst { todo -> todo.id() == todoId}var todos = (listItems?.toMutableList())?.removeAt(index!!)Network.apolloClient.apolloStore().write(GetMyTodosQuery(), GetMyTodosQuery.Data(mutableListOf(todos))).execute()getMyTodosQueryLocal()}})}
We have a function defined to handle the onClick of delete icon to remove a todo. Let's update the function.
override fun delete(taskId: Int) {- // Todo : Method for deleting a task+ removeTodoMutationCloud(taskId)}
Did you find this page helpful?
Start with GraphQL on Hasura for Free
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs