Search code examples
nativescript-vuevue-apollo

How to integrate Apollo with NativeScript vue?


How to consume GraphQL API from a Nativescript-vue application?

How to modify it for nativescript-vue?

Should vue-apollo package work directly? There are even some examples of "apollo with angular". But unfortunately I couldn't find instruction for nativescript-vue. @khalifa-gad said it works fine with nativescript core. But does it work also with nativescript-vue?

There is an complete implementation with angular.

Angular answer is:

import { NativeScriptModule } from 'nativescript-angular/nativescript.module';
import { NativeScriptHttpClientModule } from 'nativescript-angular/http-client';
import { ApolloModule, Apollo } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';

@NgModule({
  imports: [
    NativeScriptModule,
    NativeScriptHttpClientModule, // this provides HttpClient
    ApolloModule,
    HttpLinkModule
  ]
})
export class AppModule {
  constructor(
    apollo: Apollo,
    httpLink: HttpLink
  ) {
    // create Apollo
    apollo.create({
      link: httpLink.create({ uri }),
      // other options like cache
    });
  }
}


Solution

  • I have been using Vue-apollo with nativescript-vue last 2 months.

    Step by step apollo installation with JWT

    1. Use vue-apollo manual installation yarn add vue-apollo graphql apollo-boost
    2. Create apollo.config.jsfile
    3. add apollo code to your main.ts file
    import Vue from 'nativescript-vue'
    import List from './components/List.vue'
    import Login from './components/Login.vue'
    import * as ApplicationSettings from "tns-core-modules/application-settings";
    import VueDevtools from 'nativescript-vue-devtools'
    import VueApollo from "vue-apollo";
    import {
        ApolloClient,
        InMemoryCache,
        HttpLink,
        ApolloLink
    } from "apollo-boost";
    import { onError } from "apollo-link-error";
    import { setContext } from "apollo-link-context";
    /////////////// APOLLO
    Vue.use(VueApollo);
    const errorLink = onError(({ graphQLErrors }) => {
        if (graphQLErrors) graphQLErrors.map(({ message }) => console.log(message));
    });
    const httpLink = new HttpLink({
        uri: "https://polar-badlands-01357.herokuapp.com/graphql"
    });
    const authLink = setContext((_, { headers }) => {
        // get the authentication token from ApplicationSettings if it exists
        var tokenInAppSettings = ApplicationSettings.getString("token");
        // return the headers to the context so HTTP link can read them
        return {
            headers: {
                ...headers,
                authorization: tokenInAppSettings
                    ? `Bearer ${tokenInAppSettings}`
                    : null
            }
        };
    });
    export const apolloClient = new ApolloClient({
        link: ApolloLink.from([errorLink, authLink, httpLink]),
        cache: new InMemoryCache()
    });
    
    const apolloProvider = new VueApollo({
        defaultClient: apolloClient
    });
    
    
    if(TNS_ENV !== 'production') {
      Vue.use(VueDevtools)
    }
    import store from './store'
    
    // Prints Vue logs when --env.production is *NOT* set while building
    Vue.config.silent = true
    
    new Vue({
      store,
      apolloProvider,
      render: h => h("frame", [h(ApplicationSettings.hasKey("token")? List : Login)])
    }).$start()
    
    

    If someone is interested please check this complete Github repo with login and mutation examples.

    And for a complete tested nativescript-vue plugin database check out plug-in page