javascript - How to trigger an Apollo query in a Vue component event? - Stack Overflow

How to trigger an Apollo GraphQL query on a mounted event in a Vue.js ponent. Here is what my code look

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

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 ponent, but how can I get it to run with various events?

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

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 ponent, but how can I get it to run with various events?

Share Improve this question asked Mar 17, 2017 at 9:16 RaedRaed 6981 gold badge7 silver badges24 bronze badges 1
  • Make the query a method and call it when the events occur. – Bert Commented Mar 17, 2017 at 14:17
Add a ment  | 

1 Answer 1

Reset to default 6
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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745573780a4633839.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信