简体   繁体   English

如何在方法中使用的vue apollo查询中重新获取/更新结果

[英]How to refetch / update result in vue apollo query that used in a method

I want to update or refetch data in apollo query that used in a method (not apollo object). 我想更新或重新获取方法中使用的apollo查询中的数据(不是apollo对象)。 the thing is i wanted to query and update that query after specific event, so i couldn't use apollo object directly in in my code. 事情是我想在特定事件后查询和更新该查询,所以我无法在我的代码中直接使用apollo对象。

methods: {
    async fetchEvents() {
      const { data } = await this.$apollo.query({
        query: gql`
            query(
              $someThing: Int,
            ) {
                events (
                  someThing: $someThing,
                ) {
                    total
                    items {
                     ...
                    }
                }
            }
        `,
        variables() {
          return {
            ...
          };
        },
      });
      this.data = data;
    },
  },


  watch: {
    'items.view.organizerId': function callback() {
      this.fetchEvents();
    },
    eventsSearchInputVal: function callback() {
      this.fetchEvents();
    },
    'pagination.statusFilter': function callback() {
      this.fetchEvents();
    },
  },

in conclusion when pagination.statusFilter or eventsSearchInputVal or items.view.organizerId in watch got changed the query should refetch. 总之,当pagination.statusFiltereventsSearchInputValitems.view.organizerIdwatch得到了改变查询应重新提取。 in this code nothing happen when those variables get changed. 在这段代码中,当这些变量发生变化时,什

i don't know why but I've changed variables() method to an object and it got fixed. 我不知道为什么,但我已经将variables()方法更改为一个对象并得到修复。 so this code is now working: 所以这段代码现在正在运行:

methods: {
    async fetchEvents() {
      const { data } = await this.$apollo.query({
        query: gql`
            query(
              $someThing: Int,
            ) {
                events (
                  someThing: $someThing,
                ) {
                    total
                    items {
                     ...
                    }
                }
            }
        `,
        variables: { // this is an object now
            ...
        },
      });
      this.data = data;
    },
  },


  watch: {
    'items.view.organizerId': function callback() {
      this.fetchEvents();
    },
    eventsSearchInputVal: function callback() {
      this.fetchEvents();
    },
    'pagination.statusFilter': function callback() {
      this.fetchEvents();
    },
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM