Skip to content

Instantly share code, notes, and snippets.

@Akryum
Created September 22, 2016 08:21
Show Gist options
  • Select an option

  • Save Akryum/4517947bcf71bd8b3c0960b84060c64a to your computer and use it in GitHub Desktop.

Select an option

Save Akryum/4517947bcf71bd8b3c0960b84060c64a to your computer and use it in GitHub Desktop.
Apollo mutation example in a Vue component
<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>
@rimiti
Copy link
Copy Markdown

rimiti commented Aug 24, 2019

Thanks for the sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment