Skip to content

v0.10.0

Pre-release
Pre-release

Choose a tag to compare

@Akryum Akryum released this 22 May 13:42

Breaking changes

  • The Apollo client and a customizable configuration in now provided directly by vue-cli-plugin-apollo. The genrator will no longer add a apollo.js file in your project. From now on, upgrading vue-cli-plugin-apollo will also upgrade Apollo and the configuration if it needed a change, without requiring you to do anything!

If you already have an existing project, remove apollo.js and modify vue-apollo:

import Vue from 'vue'
import VueApollo from 'vue-apollo'
import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client'

// Install the vue plugin
Vue.use(VueApollo)

// Name of the localStorage item
const AUTH_TOKEN = 'apollo-token'

// Config
const defaultOptions = {
  // You can use `https` for secure connection (recommended in production)
  httpEndpoint: process.env.VUE_APP_GRAPHQL_HTTP || 'http://localhost:4000/graphql',
  // You can use `wss` for secure connection (recommended in production)
  // Use `null` to disable subscriptions
  wsEndpoint: process.env.VUE_APP_GRAPHQL_HTTP || 'ws://localhost:4000/graphql',
  // LocalStorage token
  tokenName: AUTH_TOKEN,
  // Enable Automatic Query persisting with Apollo Engine
  persisting: true,
  // Use websockets for everything (no HTTP)
  // You need to pass a `wsEndpoint` for this to work
  websocketsOnly: false,
  // Is being rendered on the server?
  ssr: false,
  // Override default http link
  // link: myLink,
  // Override default cache
  // cache: myCache,
  // Additional ApolloClient options
  // apollo: { ... }
}

// Call this in the Vue app file
export function createProvider (options = {}) {
  // Create apollo client
  const { apolloClient, wsClient } = createApolloClient({
    ...defaultOptions,
    ...options,
  })
  apolloClient.wsClient = wsClient

  // Create vue apollo provider
  const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
  })

  return apolloProvider
}

// Manually call this when user log in
export function onLogin (apolloClient, token) {
  localStorage.setItem(AUTH_TOKEN, token)
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
}

// Manually call this when user log out
export function onLogout (apolloClient) {
  localStorage.removeItem(AUTH_TOKEN)
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
  apolloClient.resetStore()
}

Change your main.js entry file too:

import Vue from 'vue'
import App from './App.vue'
// Changed
import { createProvider } from './vue-apollo'

// ...

new Vue({
  // Changed
  provide: createProvider().provide(),
  render: h => h(App)
}).$mount('#app')