Raed
Raed

Reputation: 698

How to trigger an Apollo query in a Vue component event?

How to trigger an Apollo GraphQL query on a mounted event in a Vue.js component.

Here is what my code looks like:

<template>
</template>

<script>
import gql from 'graphql-tag';

const myQuery = gql`
  query someQuery {
    books{
      id
   }
}`

export default {
  data: () => ({
    books: []
  }),
  apollo: {
    books: {
      query: myQuery,
    },
  },
  mounted () {
  // run the appollo query here as well ?
  }
};
</script>

The query runs fine on the first load of the component, but how can I get it to run with various events?

Upvotes: 3

Views: 3801

Answers (1)

ceferrari
ceferrari

Reputation: 1677

this.$apollo.queries.books.refetch();

if you have variables and need to update them on each call, pass them as parameter of the method refetch:

this.$apollo.queries.books.refetch(variables);

Upvotes: 6

Related Questions