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 Query
removeTodoMutation = RemoveTodoMutation.builder().id(todoId).build()
// Apollo runs query on background thread
Network.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 list
val index = listItems?.indexOfFirst { todo -> == todoId}
var todos = (listItems?.toMutableList())?.removeAt(index!!)
.write(GetMyTodosQuery(), GetMyTodosQuery.Data(mutableListOf(todos))).execute()

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)