DEV Community

Abhishek
Abhishek

Posted on • Edited on

2 2

Setting up a Vue CLI project with Vue Apollo

Today we’ll learn how to integrate apollo-client 2 with Vue.js using vue-apollo.

Installation

apollo-client has numbers of dependencies that need to be setup in your current Vue CLI project.

npm install --save vue-apollo graphql apollo-client apollo-link apollo-link-http apollo-cache-inmemory graphql-tag

Usage

  • Set up an ApolloClient in our main.js.
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'

// HTTP connection to the API
const httpLink = createHttpLink({
  // You should use an absolute URL here
  uri: 'http://localhost:3020/graphql',
})

// Cache implementation
const cache = new InMemoryCache()

// Create the apollo client
const apolloClient = new ApolloClient({
  link: httpLink,
  cache,
})
  • Install the VueApollo plugin into Vue
import Vue from 'vue'
import VueApollo from 'vue-apollo'

Vue.use(VueApollo)
  • The ApolloProvider holds the Apollo client instances that can then be used by all the child components.
const apolloProvider = new VueApollo({
  defaultClient: apolloClient,
})

The final step

Extracting the apollo component to a single file, create a file called apollo.js in src/graphql/apollo.js and add

//apollo.js 
// This is everything we need to work with Apollo 2.0.
import Vue from "vue";
import { ApolloClient } from "apollo-client";
import { HttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import VueApollo from "vue-apollo";
// Register the VueApollo plugin with Vue.
Vue.use(VueApollo);

// Create a new HttpLink to connect to your GraphQL API.
// According to the Apollo docs, this should be an absolute URI.
const httpLink = new HttpLink({
    uri: `http://localhost:3020/graphql`
});

// I'm creating another variable here just because it makes it easier to add more links in the future.
const link = httpLink;

// Create the apollo client
const apolloClient = new ApolloClient({
    // Tells Apollo to use the link chain with the http link we set up.
    link,
    // Handles caching of results and mutations.
    cache: new InMemoryCache(),
    // Useful if you have the Apollo DevTools installed in your browser.
    connectToDevTools: true
});

const apolloProvider = new VueApollo({
    // Apollo 2.0 allows multiple clients to be enabled at once.
    // Here we select the default (and only) client.
    defaultClient: apolloClient
});

export default apolloProvider;

Add it to your app with the apolloProvider option, Now our main.js will look like:

//main.js
import Vue from "vue";
import App from "./App.vue";
import apolloProvider from "./graphql/apollo";

new Vue({
  el: '#app',
  // inject apolloProvider here like vue-router or vuex
  apolloProvider,
  render: h => h(App),
})

You are now ready to use Apollo in your components!
For more details, see the documentation on apollo-vue and Apollo.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️