Mutation and update cache

Open src/components/TodoPrivateList.vue and update the clearCompleted function as below

githubsrc/components/TodoPrivateList.vue
clearCompleted: function() {
const isOk = window.confirm("Are you sure?");
if (isOk) {
// 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,
+ update: (cache, { data: { delete_todos } }) => {
+ if (delete_todos.affected_rows) {
+ const data = cache.readQuery({
+ query: GET_MY_TODOS,
+ });
+ data.todos = data.todos.filter((todo) => todo.is_completed !== true);
+ cache.writeQuery({
+ query: GET_MY_TODOS,
+ data
+ });
+ }
+ },
+ })
+ .catch(error => {
+ console.error(error);
+ });
}
}

We are defining a mutation which deletes all todos which matches the following conditions: is_completed: true and is_public: false.

Again we are making use of this.$apollo.mutate to execute the mutation.

That's a wrap of the todo app.

Close

Get Started with GraphQL Now

Hasura Cloud gives you a fully managed, production ready GraphQL API as a service to help you build modern apps faster.
Ready to get started?
Start for free on Hasura Cloud or you could contact our sales team for a detailed walk-through on how Hasura may benefit your business.
Stay in the know
Sign up for full access to our community highlights, new features, and occasional baby animal gifs! Oh, and we have a strict no-spam rule. ✌️