Skip to content

Instantly share code, notes, and snippets.

@smolinari
Created April 5, 2020 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smolinari/4d982a6798a3dea936fc7878ba44f44c to your computer and use it in GitHub Desktop.
Save smolinari/4d982a6798a3dea936fc7878ba44f44c to your computer and use it in GitHub Desktop.
<template>
<div>
<q-checkbox
v-model="todoToggle"
@input="setToggle"
/>
<p v-if="error">There has been an error<br>{{error}}</p>
</div>
</template>
<script>
import { queries, mutations } from 'src/graphql/Todos'
export default {
name: 'TodoToggle',
props: {
id: String,
completed: Boolean
},
data () {
return {
error: '',
todoToggle: this.completed
}
},
methods: {
setToggle () {
this.$apollo.mutate({
mutation: mutations.toggleTodo,
variables: {
id: this.id
},
update: (store, { data: { toggleTodo } }) => {
const data = store.readQuery({ query: queries.getTodos })
data.todos.find((todo) => {
if (todo.id === this.id) {
todo.completed = !todo.completed
}
})
store.writeQuery({ query: queries.getTodos, data })
}
}).catch((error) => {
this.error = error
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment