Mutation and update cache

Open src/app/Todo/TodoPrivateList.ts and update the clearCompleted function as below

githubsrc/app/Todo/TodoPrivateList.ts
clearCompleted() {
+ // Remove all the todos that are completed
+ const CLEAR_COMPLETED = gql`
+ mutation clearCompleted {
+ delete_todos(where: {is_completed: {_eq: true}, is_public: {_eq: false}}) {
+ affected_rows
+ }
+ }
+ `;
+
+ this.apollo.mutate({
+ mutation: CLEAR_COMPLETED,
+ optimisticResponse: {},
+ update: (cache, { data }) => {
+ const existingTodos : any = cache.readQuery({ query: GET_MY_TODOS });
+ const newTodos = existingTodos.todos.filter(t => (!t.is_completed));
+ cache.writeQuery({query:GET_MY_TODOS, data: {todos: newTodos}});
+ },
+ })
+ .subscribe(
+ ({ data }) => {
+ console.log('got data ', data);
+ },
+ (error) => {
+ console.log('there was an error sending the query', error);
+ }
+ );
}

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