Sign up for Hasura Newsletter
Loading...

Mutation and update cache

Now let's do the integration part. Open TaskFragment.kt and add the following code to define the delete mutation

githubTaskFragment.kt
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 -> 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
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin