Search code examples
vue.jsvuexvue-apollo

How to initialize vuex store before apollo request


When created my base component I dispatch store but apollo query init queries before store and I get not correct query.

Code of my store:

import Vuex from 'vuex';

import reportFilter from '../../report-filter/store';
import account from './account-store';

const store = new Vuex.Store({
    strict: process.env.NODE_ENV === 'development',
    state: {
        role: null,
        id: null,        
    },
    mutations: {
        setAccountData(state, { accountData }) {            
            state.role = accountData.role;
            state.id = accountData.id;            
        },
    },

    actions: {
        initialize({ commit }) {
            // TODO: rewrite after GraphQL backend will be released
            commit('setAccountData', { accountData: document.accountData 
          });
        },
    },
});

export default store;

My apollo client provided in main component.

new Vue({
    router,
    store,
    svgxuse,
    provide: apolloProvider.provide(),
    render: h => h(RootLayout),    
    mounted() {
        redirectIfNeeded(this);
    },
}).$mount('#app');

BaseLayout component:

import YNavigation from '../../navigation/Navigation.vue';
import YHeader from '../../header/Header.vue';
import YFooter from '../../footer/Footer.vue';

export default {
    data() {
        return {};
    },

    created() {
        this.$store.dispatch('account/initialize');
    },

    components: { YNavigation, YHeader, YFooter },
};

I tried to dispatch store in main vue component before create but it not helped.

apollo: {
        data: {
            query: gql`
                query GetDataById($id: ID!) {
                  ${this.$store.state.account.role} {
                    getDataById(id: $id) {
                      id
                      someId
                      name                                           
                     }
                  }
                }
            `,
            update: data => cloneDeep(data.user.getDataById),
            variables() {
                return {
                    id: this.actionData.dataId
                };
            },
            skip() {
                return this.actionData.dataId === undefined;
            },
        },
}

I expect get data by some specific role but get error because ${this.$store.state.account.role} return null in query string.


Solution

  • I solve that problem by init store as plugin function

    import Vuex from 'vuex';
    
    import reportFilter from '../../report-filter/store';
    import account from './account-store';
    import user from './user-store';
    
    // called when the store is initialized
    const initPlugin = store => {
        store.dispatch('account/initialize');
        store.dispatch('user/initialize');
    };
    
    const store = new Vuex.Store({
        strict: process.env.NODE_ENV === 'development',
        state: {
            // Global store
            // This is place for current user data or other global info
        },
        modules: {
            reportFilter,
            account,
            user,
        },
        plugins: [initPlugin],
    });
    
    export default store;