updateQuery is ignored when page variable is reactive #1299
-
Given a component: import getProducts from '~/graphql/getProducts'
export default {
props: ['category'],
data() {
return {
products: [],
page: 1,
showMoreEnabled: true
}
},
apollo: {
products: {
query: getProducts,
variables() { return { locale: this.$i18n.locale, category: this.category, page: this.page } } // <<<<<-------- important
}
},
watch: {
products(v) {
console.log('products updated', v)
}
},
methods: {
loadMore() {
this.page++;
this.$apollo.queries.products.fetchMore({
variables: {
page: this.page
},
updateQuery: (previousResult, { fetchMoreResult }) => {
console.log(previousResult)
const newProducts = fetchMoreResult.products
console.log(newProducts)
this.showMoreEnabled = newProducts.length > 0
console.log([...previousResult.products, ...newProducts])
return {products: [...previousResult.products, newProducts]}
},
})
}
}
} In this configuration if I run If I change the apollo prop to this (removing apollo: {
products: {
query: getProducts,
variables() { return { locale: this.$i18n.locale, category: this.category, page: 1 } } // <<<<<-------- important
}
} Then everything works fine. I understand that this is a conflict between reactive behaviour and manual resolution, but manual resolution, if present, should have higher priority, IMO Versions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Using You can further optimize the cache with keyArgs in your type policies so that coming back to this component will render everything already loaded (even beyond the 1st page): keyArgs: ['locale', 'category'] // exclude the page from the cache key |
Beta Was this translation helpful? Give feedback.
Using
page
in your reactive variables will re-fetch and update the whole query with the newpage
value. The correct way is only putting the initial page value in the query variables. See the docs.You can further optimize the cache with keyArgs in your type policies so that coming back to this component will render everything already loaded (even beyond the 1st page):