Mutation and update cache
Open src/components/TodoPrivateList.vue
and update the clearCompleted
function as below
<script setup lang="ts">import { computed, reactive } from "vue"import TodoItem from "../components/TodoItem.vue"import { useMutation, useQuery, useResult } from "@vue/apollo-composable"+ import { DELETE_TODOS, SELECT_TODOS } from "../graphql-operations"// Used in both query and mutation (refetch query variables)const selectTodosVariables = {where: {is_public: { _eq: false },},order_by: {created_at: "desc",},}const privateTodosQuery = useQuery(SELECT_TODOS, selectTodosVariables)const privateTodos = useResult(privateTodosQuery.result, [], (data) => data?.todos)+ const deleteCompletedTodosMutation = useMutation(DELETE_TODOS, {+ variables: {+ where: {+ is_completed: { _eq: true },+ is_public: { _eq: false },+ },+ },+ refetchQueries: [+ {+ query: SELECT_TODOS,+ variables: selectTodosVariables,+ },+ ],+ })const state = reactive({type: "private",filterType: "all",filteredTodos: computed(() => {return privateTodos.value.filter((todo) => {switch (state.filterType) {case "completed":return todo.is_completedcase "active":return !todo.is_completeddefault:return true}})}),activeTodos: computed(() => privateTodos.value.filter((todo) => !todo.is_completed)),remainingTodos: computed(() => state.activeTodos.length),})function filterResults(type: string) {switch (type) {case "active":state.filterType = "active"breakcase "completed":state.filterType = "completed"breakdefault:state.filterType = "all"break}}async function clearCompleted() {const isOk = window.confirm("Are you sure?")if (!isOk) return+ const result = await deleteCompletedTodosMutation.mutate()+ console.log("clear completed result", result)++ if (deleteCompletedTodosMutation.error.value) {+ console.error(deleteCompletedTodosMutation.error.value)+ }}</script>
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 useMutation()
to execute the mutation.
That's a wrap of the todo app.
Did you find this page helpful?
Start with GraphQL on Hasura for Free
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs