Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the lifecycle of vue apollo subscriptions?

Does vue-apollo automatically unsubscribe the query when the View changes?

For example, I have two views routed to /users and /orders. /users has a subscription to the users table and /orders has a subscription to the orders table.

If I were on the /user page, would the order subscription still be in effect? If yes, how would I be able to turn it off?

like image 752
zcaudate Avatar asked Feb 17 '20 02:02

zcaudate


People also ask

Is Vue 3 compatible with Vue Apollo?

Different versions of Vue Apollo are compatible with different versions of Vue: Vue Apollo v3 - Vue v2. Vue Apollo v4 - Vue v2 and Vue v3.

What is Apollo in Vue?

Vue is a modern JavaScript framework for building single-page applications. Apollo Client is a fully-fledged GraphQL client and state management library. Using Vue Apollo, we can combine them to substantially improve the developer experience involved in building complex UIs.


1 Answers

Since Apollo queries are bound to your component they will follow the lifecycle of your components, i.e. if your route changes (different components are renderd), your old components will be deleted and therefore your old queries will also be removed.

This is taken care of within Vue apollo by this mixin.

Take a look at the following part:

export function installMixin (Vue, vueVersion) {
  Vue.mixin({
    // Other irrelevant code for this question
    destroyed: destroy,
  })
}

This means it binds to the 'destroyed' event of each Vue component which will then trigger the destroy function (as defined by the Vue API reference):

function destroy () {
  if (this.$_apollo) {
    this.$_apollo.destroy()
  }
}

So this process ensures your queries are destroyed and not in effect anymore when your component is destroyed.

I hope this answers your question

like image 52
Sven Hakvoort Avatar answered Sep 24 '22 19:09

Sven Hakvoort