Created
September 22, 2016 08:21
-
-
Save Akryum/4517947bcf71bd8b3c0960b84060c64a to your computer and use it in GitHub Desktop.
Apollo mutation example in a Vue component
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
<script> | |
import gql from 'graphql-tag'; | |
// GraphQL Mutation with one parameter | |
const upvoteMutation = gql` | |
mutation upvotePost($postId: Int!) { | |
upvotePost(postId: $postId) { | |
id | |
votes | |
} | |
} | |
`; | |
export default { | |
// Attribute | |
props: { | |
// Post id passed down to this component | |
postId: { | |
type: Number, | |
required: true, | |
}, | |
}, | |
methods: { | |
upvote() { | |
// Mutation | |
this.$apollo.mutate({ | |
mutation: upvoteMutation, | |
variables: { | |
postId: this.postId, | |
}, | |
}).then(data => { | |
console.log('Done upvoting.'); | |
}); | |
}, | |
}, | |
}; | |
</script> | |
<template> | |
<button @click="upvote">Upvote</button> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the sharing!