Update the apollo cache

This commit is contained in:
Matt Rider 2019-08-26 18:54:21 +02:00
parent f11091c4ce
commit ba1e62da21
2 changed files with 34 additions and 17 deletions

View File

@ -62,12 +62,13 @@
</template> </template>
<script> <script>
import gql from 'graphql-tag'
import { mapGetters, mapMutations } from 'vuex' import { mapGetters, mapMutations } from 'vuex'
import HcUser from '~/components/User' import HcUser from '~/components/User'
import ContentMenu from '~/components/ContentMenu' import ContentMenu from '~/components/ContentMenu'
import ContentViewer from '~/components/Editor/ContentViewer' import ContentViewer from '~/components/Editor/ContentViewer'
import HcEditCommentForm from '~/components/EditCommentForm/EditCommentForm' import HcEditCommentForm from '~/components/EditCommentForm/EditCommentForm'
import CommentMutations from '~/graphql/CommentMutations'
import PostQuery from '~/graphql/PostQuery'
export default { export default {
data: function() { data: function() {
@ -140,24 +141,33 @@ export default {
this.openEditCommentMenu = showMenu this.openEditCommentMenu = showMenu
this.setEditPending(showMenu) this.setEditPending(showMenu)
}, },
async deleteCommentCallback() { deleteCommentCallback() {
try { this.$apollo
var gqlMutation = gql` .mutate({
mutation($id: ID!) { mutation: CommentMutations(this.$i18n).DeleteComment,
DeleteComment(id: $id) {
id
}
}
`
await this.$apollo.mutate({
mutation: gqlMutation,
variables: { id: this.comment.id }, variables: { id: this.comment.id },
update: store => {
const data = store.readQuery({
query: PostQuery(this.$i18n),
variables: { id: this.post.id },
})
const index = data.Post[0].comments.findIndex(
deletedComment => deletedComment.id === this.comment.id,
)
if (index !== -1) {
data.Post[0].comments.splice(index, 1)
}
store.writeQuery({ query: PostQuery(this.$i18n), data })
},
})
.then(res => {
this.$toast.success(this.$t(`delete.comment.success`))
this.$emit('deleteComment')
})
.catch(err => {
this.$toast.error(err.message)
}) })
this.$toast.success(this.$t(`delete.comment.success`))
this.$emit('deleteComment')
} catch (err) {
this.$toast.error(err.message)
}
}, },
}, },
} }

View File

@ -51,5 +51,12 @@ export default i18n => {
} }
} }
`, `,
DeleteComment: gql`
mutation($id: ID!) {
DeleteComment(id: $id) {
id
}
}
`,
} }
} }