Created
April 5, 2020 14:40
-
-
Save smolinari/4d982a6798a3dea936fc7878ba44f44c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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