Mutation and update cache

Open TaskFragment.kt and add the following function to delete all the completed todos from the cloud.

githubTaskFragment.kt
private fun removeAllCompletedCloud(){
// Init Query
clearCompletedMutation = ClearCompletedMutation.builder().build()
// Apollo runs query on background thread
Network.apolloClient.mutate(clearCompletedMutation)?.enqueue(object : ApolloCall.Callback<ClearCompletedMutation.Data>() {
override fun onFailure(error: ApolloException) {
Log.d("Todo", error.toString() )
}
override fun onResponse(@NotNull response: Response<ClearCompletedMutation.Data>) {
// get data from local cache and update the list
val todos = listItems?.filter { task -> task.is_completed }
Network.apolloClient
.apolloStore()
.write(GetMyTodosQuery(), GetMyTodosQuery.Data(todos!!)).execute()
getMyTodosQueryLocal()
}
})
}

And finally update the removeAllCompleted function as below

private fun removeAllCompleted() {
- // Todo : Method for clearing all completed task at once
+ removeAllCompletedCloud()
}

That's a wrap of the todo app.

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
graphql-handbook