2018-11-06 10:30:27 +01:00
|
|
|
import Vue from 'vue';
|
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
import { ApolloLink } from 'apollo-link';
|
|
|
|
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
|
|
|
|
import { createLink } from 'apollo-absinthe-upload-link';
|
|
|
|
import { AUTH_TOKEN } from './constants';
|
2018-11-15 17:35:47 +01:00
|
|
|
import { GRAPHQL_API_ENDPOINT, GRAPHQL_API_FULL_PATH } from './api/_entrypoint';
|
2019-01-18 14:47:10 +01:00
|
|
|
import { withClientState } from 'apollo-link-state';
|
|
|
|
import { currentUser } from '@/apollo/user';
|
|
|
|
import merge from 'lodash/merge';
|
|
|
|
import { ApolloClient } from 'apollo-client';
|
|
|
|
import { DollarApollo } from 'vue-apollo/types/vue-apollo';
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
// Install the vue plugin
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
|
|
|
// Http endpoint
|
2018-11-15 17:35:47 +01:00
|
|
|
const httpServer = GRAPHQL_API_ENDPOINT || 'http://localhost:4000';
|
|
|
|
const httpEndpoint = GRAPHQL_API_FULL_PATH || `${httpServer}/api`;
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
const fragmentMatcher = new IntrospectionFragmentMatcher({
|
|
|
|
introspectionQueryResultData: {
|
|
|
|
__schema: {
|
|
|
|
types: [
|
|
|
|
{
|
|
|
|
kind: 'UNION',
|
|
|
|
name: 'SearchResult',
|
|
|
|
possibleTypes: [
|
|
|
|
{ name: 'Event' },
|
|
|
|
{ name: 'Actor' },
|
|
|
|
],
|
|
|
|
}, // this is an example, put your INTERFACE and UNION kinds here!
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const cache = new InMemoryCache({ fragmentMatcher });
|
|
|
|
|
|
|
|
const authMiddleware = new ApolloLink((operation, forward) => {
|
|
|
|
// add the authorization to the headers
|
|
|
|
const token = localStorage.getItem(AUTH_TOKEN);
|
|
|
|
operation.setContext({
|
|
|
|
headers: {
|
|
|
|
authorization: token ? `Bearer ${token}` : null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-12-28 15:41:32 +01:00
|
|
|
if (forward) return forward(operation);
|
2018-12-21 15:41:34 +01:00
|
|
|
|
|
|
|
return null;
|
2018-11-06 10:30:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const uploadLink = createLink({
|
|
|
|
uri: httpEndpoint,
|
|
|
|
});
|
|
|
|
|
2019-01-18 14:47:10 +01:00
|
|
|
const stateLink = withClientState({
|
|
|
|
...merge(currentUser),
|
|
|
|
cache,
|
|
|
|
});
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-01-18 14:47:10 +01:00
|
|
|
const link = stateLink.concat(authMiddleware).concat(uploadLink);
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-01-18 14:47:10 +01:00
|
|
|
const apolloClient = new ApolloClient({
|
2018-12-21 15:41:34 +01:00
|
|
|
cache,
|
|
|
|
link,
|
|
|
|
connectToDevTools: true,
|
2019-01-18 14:47:10 +01:00
|
|
|
});
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-01-18 14:47:10 +01:00
|
|
|
apolloClient.onResetStore(stateLink.writeDefaults as any);
|
|
|
|
|
|
|
|
export const apolloProvider = new VueApollo({
|
|
|
|
defaultClient: apolloClient,
|
|
|
|
errorHandler(error) {
|
2018-11-06 10:30:27 +01:00
|
|
|
// eslint-disable-next-line no-console
|
2019-01-18 14:47:10 +01:00
|
|
|
console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Manually call this when user log in
|
|
|
|
export function onLogin(apolloClient) {
|
|
|
|
// if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient);
|
2018-11-06 10:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Manually call this when user log out
|
2019-01-18 14:47:10 +01:00
|
|
|
export async function onLogout(apolloClient: DollarApollo<any>) {
|
|
|
|
// if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient);
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
try {
|
2019-01-18 14:47:10 +01:00
|
|
|
await apolloClient.provider.defaultClient.resetStore();
|
2018-11-06 10:30:27 +01:00
|
|
|
} catch (e) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('%cError on cache reset (logout)', 'color: orange;', e.message);
|
|
|
|
}
|
|
|
|
}
|