Apollo-module: best practice to use apollo inside of Vuex

Created on 26 Sep 2017  路  11Comments  路  Source: nuxt-community/apollo-module

@Akryum @Atinux do you guys have an idea what would be the best practice to attach $apollo or apolloProvider to Vuex and make it accessible inside of actions? Would be great to be able doing:

this.$store.dispatch('mutateGql',variables) 

// inside of store.js
mutateGql({apollo?},context){
// => how to get current apollo instance inside of the action?
}

Any thought how to do this with this module?

This feature request is available on Nuxt.js community (#c21)
enhancement question

Most helpful comment

You can access the apollo clients in Nuxt Vuex via this. For instance:

export default {
  actions: {
    foo (context) {
      let client = this.app.apolloProvider.defaultClient
    }
}

If you wanted to make an Apollo call via a page's asyncData, it'd look something like this:

asycData (context) {
  let client = context.app.apolloProvider.defaultClient
}

Notice, in asyncData, the app is assigned to context. That is not the case in the store. Worth noting, app _is_ assigned to context in nuxtServerInit. For example:

nuxtServerInit (store, context) {
  let client = context.app.apolloProvider.defaultClient
}

Lastly, to access Apollo clients in a component method (say, for browser-only functionality), you'd do it this way:

methods: {
  foo () {
    let client = this.$apollo.provider.defaultClient
  }
}

Those are probably the four likely ways you'll need to be accessing Apollo. Unfortunately, they're four _different_ ways, but they are somewhat justified. I would recommend this be documented somewhere, as it is kind of confusing, and takes some trial and error of logging to console and inspecting vars to figure this out.

All 11 comments

Would appreciate some guidance on this as well. Maybe create the store manually, creating modules in a function, and passing the store into that function? Alternatively, it'd be nice if:

import client from '~/apollo/clients/default'

or something similar pulled in that particular named client.

+1 for guidance / example for this one!
Can't figure out best approach here, but found that axios is accessible inside of actions and described here.
https://github.com/nuxt-community/axios-module

You can access the apollo clients in Nuxt Vuex via this. For instance:

export default {
  actions: {
    foo (context) {
      let client = this.app.apolloProvider.defaultClient
    }
}

If you wanted to make an Apollo call via a page's asyncData, it'd look something like this:

asycData (context) {
  let client = context.app.apolloProvider.defaultClient
}

Notice, in asyncData, the app is assigned to context. That is not the case in the store. Worth noting, app _is_ assigned to context in nuxtServerInit. For example:

nuxtServerInit (store, context) {
  let client = context.app.apolloProvider.defaultClient
}

Lastly, to access Apollo clients in a component method (say, for browser-only functionality), you'd do it this way:

methods: {
  foo () {
    let client = this.$apollo.provider.defaultClient
  }
}

Those are probably the four likely ways you'll need to be accessing Apollo. Unfortunately, they're four _different_ ways, but they are somewhat justified. I would recommend this be documented somewhere, as it is kind of confusing, and takes some trial and error of logging to console and inspecting vars to figure this out.

@bjunc thanks for this, I will append these examples to the README file!

I added the information to cover most usecases

Happy to help :)

@bjunc what is the current way to do this? I can't seem to get it working: Cannot read property 'app' of undefined

My store:

// store/index.js

// ...

export const actions = {
    callApollo: async (context) => {
        const response = await this.app.apolloProvider.defaultClient.query({
            // ...
        })
    }
}

@georgeboot, I believe you lost your scope. Try this:

export const actions = {
  async callApollo (context) {
    const response = await this.app.apolloProvider.defaultClient.query({ ... })
  }
} 

@bjunc ,, user.ts?7a93:38 Uncaught (in promise) TypeError: Cannot read property 'apolloProvider' of undefined .... when using with vuex module decorator nuxt-app with typescript,,, i need to fix this issues ...????

@georgeboot, I believe you lost your scope. Try this:

export const actions = {
  async callApollo (context) {
    const response = await this.app.apolloProvider.defaultClient.query({ ... })
  }
} 

This fine, However, "this" is undefined when used inside "getters"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Billybobbonnet picture Billybobbonnet  路  4Comments

leviwheatcroft picture leviwheatcroft  路  5Comments

Sendoo picture Sendoo  路  4Comments

drewbaker picture drewbaker  路  6Comments

Coppola-Aleandro picture Coppola-Aleandro  路  3Comments