Skip to content

Instantly share code, notes, and snippets.

@denisrudnei
Created September 26, 2019 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denisrudnei/46aef7835bff74c7fc6d8ea060f80b75 to your computer and use it in GitHub Desktop.
Save denisrudnei/46aef7835bff74c7fc6d8ea060f80b75 to your computer and use it in GitHub Desktop.
import Vue from 'vue'
import VueApollo from 'vue-apollo'
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { split } from 'apollo-link'
import { WebSocketLink } from 'apollo-link-ws'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { getMainDefinition } from 'apollo-utilities'
const httpLink = createHttpLink({
uri: '/api/graphql'
})
const wsLink = new WebSocketLink({
uri: `ws://${window.location.host}/api/subscriptions`,
options: {
reconnect: true
}
})
const link = split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},
wsLink,
httpLink
)
const cache = new InMemoryCache()
const apolloClient = new ApolloClient({
link,
cache,
connectToDevTools: true
})
const apolloProvider = new VueApollo({
defaultClient: apolloClient
})
Vue.prototype.$apolloProvider = apolloProvider
Vue.use(VueApollo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment