Utilizing vuex for Apollo pagination

Trying to incorporate vuetify's pagination component with the nuxtjs@apollo module has been quite a challenge for me.

I'm facing difficulties making it work seamlessly with my vuex store.

To avoid overwhelming you, I'll skim through most of the code which primarily consists of boilerplate.

To begin with, in order to populate my initial state, I send a graphql query to my backend and then commit these results to my state.

const client = context.app.apolloProvider.defaultClient

const response = await client.query({
  query: productsGQL,
  variables: {
    first: 5,
    page: 1
  }
})

commit('setProducts', response.data.products.data)
commit('setPagination', response.data.products.paginatorInfo)
commit('setPage', response.data.products.paginatorInfo.currentPage)

This process works smoothly as all necessary data such as products, pagination, and page are set with initial values.

Now, I have two components - a CardComponent that houses all the products:

<script>
export default {
  name: 'CardComponent',

  computed: {
    products() {
      return this.$store.getters.getProducts
    }
  }
}
</script>

And a PaginationComponent:

<template>
  <div>
    <v-pagination
      v-model='page'
      circle
      :length='total_pages'
    />
  </div>
</template>

<script>
export default {
  name: 'PaginationComponent',

  data() {
    return {
      total_pages: Math.ceil(this.$store.getters.getPagination.total / this.$store.getters.getPagination.perPage)
    }
  },

  computed: {
    page: {
      get() {
        return this.$store.getters.getPage
      },
      set(value) {
        return this.$store.commit('setPage', value)
      }
    }
  }
}
</script>

The initial value of Page is set at 1. When I click on the second page, the Page variable updates to 2, and this change is reflected in my state as well.

My current dilemma lies in figuring out how to trigger a requery to update the database. While the page number changes successfully, the displayed products remain the same, leaving me uncertain about my next steps.

If you require more code from the store, please let me know so I can include it here.

Answer №1

After implementing the refresh() method, the problem was successfully resolved:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The integration of Quill.js is problematic within a Vuetify v-dialog component

As I attempt to integrate a Quill.js editor within a Vuetify v-dialog, I'm encountering an issue where the toolbar dropdowns fail to close when the user clicks outside of the currently open dropdown. You can view my implementation on JSFiddle: https: ...

Creating multi-dimensional arrays using array lists with Axios and Vue.js

My knowledge of axios and JavaScript is limited, and I find myself struggling with creating a multi-dimensional array. 1. This is how I want my data to be structured : https://i.stack.imgur.com/kboNU.png userList: [ { ...

VueJS: Toggling button visibility in a table

I am facing an issue with my code where clicking on button A should hide it and display button B, but nothing happens when I click on A. Here is the code snippet: <template> <table> <thead> <tr> <th> ...

Unexpected behavior occurs in Django routing with a VueJS single page application when the URL is missing a trailing slash

In my development setup, I am utilizing a Django backend with a VueJS frontend, serving a REST API through Django and using VueJS along with vue-router for the single page application. After reading up on this on Stack Overflow, I found a helpful suggesti ...

A guide to organizing the alphabet into groups and displaying them in a Vue template

In my Vue.js data, the structure looks like this: new Vue({ el: "#app", data() { return { alpha: [{ artist: "Aa" }, { artist: "Az" }, { artist: "Ab" }, { artist ...

What causes regular objects to become automatically reactive in Vue3?

There is an example discussed in this particular article. It mentions that a normal object is not 'reactive'. Upon testing in this codesandbox environment, it was observed that changes made to the normal object, including a plain string, can aut ...

What is the best way to emphasize a link to the current page in vue.js?

I am working on a sidebar feature that displays several dynamically generated links (similar to creating notes and then linking to them): https://i.stack.imgur.com/1lxcy.png Users can add a new note by clicking the 'Add stage' button. The list ...

One potential way to transfer information from a child component to the parent script section in Vue is by utilizing props

I need assistance creating an object or array to send to the RETS API from a parent component in Vue.js I tried using the $emit method, but it did not work as expected Ultimately, I aim to receive an array in the parent component structured like this: &a ...

Connect the v-model to a non-existent property (Array) in Vue JS

I am struggling with retrieving questions from the database and displaying them along with options on a webpage. Here is an example: <div v-for="(question,index) in questions"> <div class="interview__item-text ...

Vue messaging application fails to display data upon mounting

Recently, I've been experimenting with different Vue chat libraries and encountered this interesting piece of code: <template> <p>{{ this.users[0] }}</p> </template> <script> export default { data() { return ...

Encountering a 404 error when using Vue history mode in conjunction with an Express

I'm facing an issue with my Vue SPA hosted on an Express server. Whenever I use history mode and refresh the page, I encounter a 404 not found exception. I attempted to solve this problem by utilizing the connect-history-api-fallback package but unfor ...

Stopping the interval in Vue before the component is destroyed

I am currently facing an issue with a countdown timer implemented on a component. The timer functions properly, however, I want it to stop counting down once the user navigates away from the page where the component is located. I have attempted to use cl ...

accessing elements in a loop in vuejs

I am trying to create a rating system using Vue.js and the v-for directive. Here is the code I have: <div class="rating-container"> <input type="radio" name="star" value="5" id="star-5"> <label class="star-radio" for="star-5">5&l ...

Creating a parent component within a child component in Vue.js

My application features a key component: export default { name: 'category', props: { ref: Object, asLabel: Boolean }, components: { Products } } The template in the Products component includes: <category :asLabel="tr ...

Introducing an innovative Serverless Multi-user Blogging Platform utilizing Next.js, Tailwind, and AWS CLI for seamless content creation and management

Seeking assistance as I may have overlooked a step. Any guidance is appreciated! Recently, I managed to deploy a full-stack serverless nextJs application on AWS Amplify successfully. I made modifications by adding a new field while creating a new blog pos ...

What is the best way to send a VueJS 3 component to a Leaflet popup that can accept both strings and HTMLElements without losing its reactivity?

Currently, I am utilizing a Leaflet map without any VueJS specific wrapper libraries, just the basic leaflet. Additionally, I implement <script setup> for my VueJS components. My objective is to pass a Vue component as content for a Leaflet popup (t ...

Animating numerous elements simultaneously during Vue component rendering

Take a look at this simple case in the following jsfiddle: https://jsfiddle.net/hsak2rdu/ I attempted to swap and animate two elements, but it didn't work as expected. When you click the toggle button in the playground, the second element quickly mo ...

Guide on setting up a dynamic sidebar linked to the route in Nuxt.js

My pages folder is structured like this: pages/ --index/ ----page1/ ------_slug.vue ----page2/ ------_slug.vue ----page1.vue // contains different content ----page2.vue // contains different content --index.vue The routes can be accessed as follows: /i ...

Dealing with a mysterious Vuejs object that remains undefined within a watched property

Encountering an issue with Vue.js's watched property. Despite the list being defined, I am receiving the error: Cannot read property 'list' of undefined. This occurs after the if check at the line var index ..... watch: { selectedRole: () ...

Can fetch be used to retrieve multiple sets of data at once?

Can fetch retrieve multiple data at once? In this scenario, I am fetching the value of 'inputDest' (email) and 'a' (name). My objective is to obtain both values and send them via email. const inputDest = document.querySelector('i ...