Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queries refetch() with variables causing JS error #342

Closed
chrispage1 opened this issue Jul 27, 2018 · 5 comments
Closed

queries refetch() with variables causing JS error #342

chrispage1 opened this issue Jul 27, 2018 · 5 comments

Comments

@chrispage1
Copy link

chrispage1 commented Jul 27, 2018

Hi,

We are creating a pagination system that performs a refetch and passes variables along with the fetch. The GraphQL request works great but an error is thrown before anything can be done.

Our code -

        apollo: {
            getOrders: {
                manual: true,
                query: QUERY_GET_ORDERS,
                variables () { return { per_page: this.per_page, page: this.page } },

                result ({ data, loading }) {

                    // when loading is done, do this...
                    if(!loading) {
                        console.log(data);
                        this.data[this.page] = data.Order_Orders.data;
                    }

                    // set our total count & total pages
                    this.totalCount = data.Order_Orders.total;
                    this.total_pages = Math.round(this.totalCount / this.per_page);
                }
            }
        },

        methods: {

            onPageChange(page) {

                // change our page
                this.page = page;

                // check if we already have this page loaded?
                if(this.data.hasOwnProperty(this.page)) return;

                // refetch our orders with new page
                // ToDo: look into refetch error, bug?
                this.$apollo.queries.getOrders.refetch({ page: this.page });
            },

So when onPageChange() is triggered, it should refetch our getOrders with a new page number, and it does... However, we then get the following error -

[Vue warn]: Error in getter for watcher "function () {
          return _this.options.variables.call(_this.vm);
        }": "TypeError: _this.options.variables.call is not a function"

found in

---> <ManageOrders> at resources/assets/js/components/_Pages/ManageOrders/Index.vue
       <IndexPage> at resources/assets/js/components/IndexPage.vue
         <Root>
TypeError: _this.options.variables.call is not a function
    at VueComponent._watchers.push.vm.$watch.immediate (_postlude:4)
    at Watcher.get (_postlude:4)
    at Watcher.run (_postlude:4)
    at flushSchedulerQueue (_postlude:4)
    at Array.<anonymous> (_postlude:4)
    at MessagePort.flushCallbacks (_postlude:4)

Is this a bug in the plugin or something that we have done? At the minute I'm leaning towards a bug as this doesn't seem right? Then again the refetch() method isn't well documented.

Thanks!

@jpikora
Copy link
Contributor

jpikora commented Aug 26, 2018

Can't say much about that error, however it looks to me you are complicating things unnecessarily. You probably have your reasons, but apollo is already providing powerful mechanism for paging and vue-apollo brings reactivity into your queries by default.

It looks like you are trying to avoid apollo cache, but a lot of things from the code you are doing are already done by apollo/vue-apollo behind the scenes.

E.g. your getOrders query has reactive variables option (since it is a function), if variables are changed the query is automatically re-fetched, so once you change the page you don't need to refetch it manually (currently there might be 2 same requests on page change, if there is no deduplication).

Another thing is that once you have the getOrders result it would be much cleaner if you define computed props to derive any additional data out of it, so you don't need to handle the query manually in result callback.

@chrispage1
Copy link
Author

@jpikora thank you for your feedback. I have been able to refactor and tidy up my orders page massively based on your feedback. Appreciate that and will mark this issue as closed as its overcome my issue through refactoring. Would still say there is an issue with refetch when passing variables though!

@escapedcat
Copy link
Contributor

Hey @chrispage1 , would you mind sharing a bit of your solution?
It looks we're running into the same issue while using vuetifys data-tables.

This is our query:

apollo: {
    apples: {
      query: applesQuery,
      variables() {
        const {
          page,
          rowsPerPage,
        } = this.pagination

        return {
          perPage: rowsPerPage,
          page,
        }
      },
      update({ apples }) {
        return apples
      },
    },
  },

The data-table gets apples.items and the total is handed in via a computed like this:

totalItems() {
  return this.apples.total
},

Still getting a somewhat similar error:

[Vue warn]: Error in callback for watcher "function () {
  return _this.options.variables.call(_this.vm);
}": "TypeError: Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object>"

Not sure if this is related to your described issue though.

@NicoAiko
Copy link

@escapedcat I get that error too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants