Search code examples
vue.jsvue-apollo

How to access '$apollo' outside vue component?


How to make apollo accessible outside vue component.

I am verifying if the user exist and then allow the route to proceed further.

{
        path: '/:username',
        name: 'userProfilePage',
        component: userProfilePage,
        beforeEnter(routeTo, routeFrom, next) {
            userExist(routeTo.params.username)
            next()
        }

Passing the username as a parameter to the userExist function.

import gql from "graphql-tag"

export default function userExist(username) {
    this.$apollo
        .query({
            query: gql`
        query($username: String!) {
            login(username: $username) {
                username
                email
            }
        }
    `,
            variables: {
                username: username
            }
        })
        .then(res => {
            console.log(res);
            return res
        })
        .catch(err => {
            console.log(err);
            return err
        });
}

But it is outputting the error:

enter image description here

Apollo client code

import Vue from 'vue'
import App from './App.vue'
import VueApollo from 'vue-apollo';
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import router from './routes.js'

Vue.config.productionTip = false

const httpLink = new HttpLink({
    uri: process.env.VUE_APP_DB_URL,
})

const cache = new InMemoryCache()

const apolloClient = new ApolloClient({
    link: httpLink,
    cache
})

Vue.use(VueApollo)

const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
})

new Vue({
    render: h => h(App),
    router,
    apolloProvider
}).$mount('#app')

Solution

  • So instead of initializing the apollo client in the App.vue file, initialize it in another file. Something like clients.js, and export that client:

    const httpLink = new HttpLink({
        uri: process.env.VUE_APP_DB_URL,
    })
    
    const cache = new InMemoryCache()
    
    export const apolloClient = new ApolloClient({
        link: httpLink,
        cache
    })
    

    Once done, import that in App.vue file like this:

    import { apolloClient } from './clients.js';
    
    Vue.use(VueApollo)
    
    const apolloProvider = new VueApollo({
        defaultClient: apolloClient,
    })
    
    new Vue({
        render: h => h(App),
        router,
        apolloProvider
    }).$mount('#app')
    

    Once done, import that client in any other file you want:

    import { apolloClient } from './client.js';
    import gql from "graphql-tag"
    
    export default function userExist(username) {
    apolloClient
        .query({
            query: gql`
        query($username: String!) {
            login(username: $username) {
                username
                email
            }
        }
    `,
            variables: {
                username: username
            }
        })
        .then(res => {
            console.log(res);
            return res
        })
        .catch(err => {
            console.log(err);
            return err
        });
    }