Created
May 4, 2019 14:57
-
-
Save malgamves/46615e97f14090849f8dcfb7ba116960 to your computer and use it in GitHub Desktop.
Main.js file for a Realtime Vue App using GraphQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ApolloClient } from 'apollo-client' | |
import { WebSocketLink } from 'apollo-link-ws'; | |
import { HttpLink } from 'apollo-link-http'; | |
import { split } from 'apollo-link'; | |
import { getMainDefinition } from 'apollo-utilities'; | |
import { InMemoryCache } from 'apollo-cache-inmemory'; | |
import Vue from 'vue' | |
import VueApollo from 'vue-apollo' | |
import App from './App' | |
Vue.config.productionTip = false | |
const httpLink = new HttpLink({ | |
// You should use an absolute URL here | |
uri: 'https://<your-app-name>/v1alpha1/graphql' | |
}); | |
const wsLink = new WebSocketLink({ | |
uri: "wss://<your-app-name>.herokuapp.com/v1alpha1/graphql", | |
options: { | |
reconnect: true | |
} | |
}); | |
const link = split( | |
// split based on operation type | |
({ query }) => { | |
const { kind, operation } = getMainDefinition(query); | |
return kind === 'OperationDefinition' && operation === 'subscription'; | |
}, | |
wsLink, | |
httpLink, | |
); | |
const apolloClient = new ApolloClient({ | |
link, | |
cache: new InMemoryCache(), | |
connectToDevTools: true | |
}) | |
Vue.use(VueApollo) | |
const apolloProvider = new VueApollo({ | |
defaultClient: apolloClient, | |
defaultOptions: { | |
$loadingKey: 'loading' | |
} | |
}) | |
/* eslint-disable no-new */ | |
new Vue({ | |
el: '#app', | |
apolloProvider, | |
render: h => h(App) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment