Skip to content

Commit ec43d10

Browse files
committed
Install Vue Apollo plugin
1 parent 6732878 commit ec43d10

File tree

4 files changed

+1147
-27
lines changed

4 files changed

+1147
-27
lines changed

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@
1717
"@avalanche/setting-spacings": "^4.0.0-alpha.6",
1818
"@vue/cli-plugin-babel": "^3.3.0",
1919
"@vue/cli-service": "^3.3.0",
20+
"apollo-server-lambda": "^2.3.1",
21+
"graphql": "^14.1.1",
22+
"graphql-tag": "^2.9.0",
23+
"graphql-type-json": "^0.2.1",
2024
"node-sass": "^4.11.0",
2125
"node-sass-magic-importer": "^5.3.0",
2226
"reset-css": "^4.0.1",
2327
"sass-loader": "^7.1.0",
2428
"vue": "^2.5.22",
29+
"vue-apollo": "^3.0.0-beta.11",
30+
"vue-cli-plugin-apollo": "^0.19.1",
2531
"vue-template-compiler": "^2.5.22"
2632
},
2733
"devDependencies": {
2834
"@avalanche/eslint-config": "^3.0.0",
2935
"@vue/cli-plugin-eslint": "^3.3.0",
36+
"eslint-plugin-graphql": "^2.1.1",
3037
"eslint-plugin-import": "^2.14.0",
3138
"eslint-plugin-vue": "^5.1.0"
3239
},

src/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Vue from 'vue';
22

3+
import { createProvider } from './vue-apollo';
34
import App from './App.vue';
45

56
Vue.config.productionTip = false;
67

78
new Vue({
9+
apolloProvider: createProvider(),
810
render: h => h(App),
911
}).$mount(`#app`);

src/vue-apollo.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createApolloClient } from 'vue-cli-plugin-apollo/graphql-client';
2+
import Vue from 'vue';
3+
import VueApollo from 'vue-apollo';
4+
5+
Vue.use(VueApollo);
6+
7+
const AUTH_TOKEN = `apollo-token`;
8+
9+
const httpEndpoint = process.env.VUE_APP_GRAPHQL_HTTP || `/.netlify/functions/graphql`;
10+
const defaultOptions = {
11+
httpEndpoint,
12+
tokenName: AUTH_TOKEN,
13+
persisting: false,
14+
websocketsOnly: false,
15+
ssr: false,
16+
};
17+
18+
export function createProvider(options = {}) {
19+
const { apolloClient } = createApolloClient({
20+
...defaultOptions,
21+
...options,
22+
});
23+
24+
const apolloProvider = new VueApollo({
25+
defaultClient: apolloClient,
26+
defaultOptions: {
27+
$query: {},
28+
},
29+
errorHandler(error) {
30+
// eslint-disable-next-line no-console
31+
console.log(`%cError`, `background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;`, error.message);
32+
},
33+
});
34+
35+
return apolloProvider;
36+
}
37+
38+
export async function onLogin(apolloClient, token) {
39+
if (typeof localStorage !== `undefined` && token) {
40+
localStorage.setItem(AUTH_TOKEN, token);
41+
}
42+
try {
43+
await apolloClient.resetStore();
44+
} catch (e) {
45+
// eslint-disable-next-line no-console
46+
console.log(`%cError on cache reset (login)`, `color: orange;`, e.message);
47+
}
48+
}
49+
50+
export async function onLogout(apolloClient) {
51+
if (typeof localStorage !== `undefined`) {
52+
localStorage.removeItem(AUTH_TOKEN);
53+
}
54+
try {
55+
await apolloClient.resetStore();
56+
} catch (e) {
57+
// eslint-disable-next-line no-console
58+
console.log(`%cError on cache reset (logout)`, `color: orange;`, e.message);
59+
}
60+
}

0 commit comments

Comments
 (0)