Refactor Apollo Client handling
This commit is contained in:
88
helpers/apollo.ts
Normal file
88
helpers/apollo.ts
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import { ApolloClient, HttpLink, InMemoryCache, split } from "@apollo/client/core";
|
||||||
|
import { WebSocketLink } from "@apollo/client/link/ws";
|
||||||
|
import { setContext } from '@apollo/client/link/context';
|
||||||
|
import { fb } from "./fb";
|
||||||
|
import { getMainDefinition } from "@apollo/client/utilities";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the current authentication token
|
||||||
|
*
|
||||||
|
* The token stored here is the Firebase Auth token.
|
||||||
|
* If null, no token is passed (no user signed in)
|
||||||
|
*/
|
||||||
|
let authToken: string | null = null;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Updates the token value on Firebase ID Token changes
|
||||||
|
*/
|
||||||
|
fb.idToken$.subscribe(newToken => {
|
||||||
|
authToken = newToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Injects auth token if available
|
||||||
|
*/
|
||||||
|
const authLink = setContext((_, { headers }) => {
|
||||||
|
if (authToken) {
|
||||||
|
return {
|
||||||
|
headers: {
|
||||||
|
...headers,
|
||||||
|
authorization: `Bearer ${authToken}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
headers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
const httpLink = new HttpLink({
|
||||||
|
uri: process.env.CONTEXT === "production"
|
||||||
|
? "https://api.hoppscotch.io/graphql"
|
||||||
|
: "https://api.hoppscotch.io/graphql",
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
const wsLink = new WebSocketLink({
|
||||||
|
uri: process.env.CONTEXT === "production"
|
||||||
|
? "wss://api.hoppscotch.io/graphql"
|
||||||
|
: "wss://api.hoppscotch.io/graphql",
|
||||||
|
options: {
|
||||||
|
connectionParams: () => {
|
||||||
|
return {
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${authToken}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const splitLink = split(
|
||||||
|
({ query }) => {
|
||||||
|
const definition = getMainDefinition(query);
|
||||||
|
return (
|
||||||
|
definition.kind === 'OperationDefinition' &&
|
||||||
|
definition.operation === 'subscription'
|
||||||
|
)
|
||||||
|
},
|
||||||
|
wsLink,
|
||||||
|
httpLink
|
||||||
|
);
|
||||||
|
|
||||||
|
export const apolloClient = new ApolloClient({
|
||||||
|
link: authLink.concat(splitLink),
|
||||||
|
cache: new InMemoryCache(),
|
||||||
|
defaultOptions: {
|
||||||
|
query: {
|
||||||
|
fetchPolicy: 'network-only',
|
||||||
|
errorPolicy: 'ignore'
|
||||||
|
},
|
||||||
|
watchQuery: {
|
||||||
|
fetchPolicy: 'network-only',
|
||||||
|
errorPolicy: 'ignore'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -39,16 +39,6 @@ export default {
|
|||||||
"background-color:black;padding:4px 8px;border-radius:8px;font-size:16px;color:white;"
|
"background-color:black;padding:4px 8px;border-radius:8px;font-size:16px;color:white;"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Update GraphQL Token on firebase ID Token changes
|
|
||||||
fb.idToken$.subscribe((token) => {
|
|
||||||
if (token) {
|
|
||||||
console.log(token)
|
|
||||||
this.$apolloHelpers.onLogin(token)
|
|
||||||
} else {
|
|
||||||
this.$apolloHelpers.onLogout()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const workbox = await window.$workbox
|
const workbox = await window.$workbox
|
||||||
if (workbox) {
|
if (workbox) {
|
||||||
workbox.addEventListener("installed", (event) => {
|
workbox.addEventListener("installed", (event) => {
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ export default {
|
|||||||
"~/plugins/vuex-persist",
|
"~/plugins/vuex-persist",
|
||||||
"~/plugins/v-tooltip",
|
"~/plugins/v-tooltip",
|
||||||
"~/plugins/vue-rx",
|
"~/plugins/vue-rx",
|
||||||
|
"~/plugins/vue-apollo",
|
||||||
{ src: "~/plugins/web-worker", ssr: false },
|
{ src: "~/plugins/web-worker", ssr: false },
|
||||||
],
|
],
|
||||||
|
|
||||||
@@ -129,16 +130,7 @@ export default {
|
|||||||
"@nuxtjs/robots",
|
"@nuxtjs/robots",
|
||||||
// https://github.com/nuxt-community/sitemap-module
|
// https://github.com/nuxt-community/sitemap-module
|
||||||
"@nuxtjs/sitemap",
|
"@nuxtjs/sitemap",
|
||||||
// https://github.com/nuxt-community/apollo-module
|
|
||||||
"@nuxtjs/apollo",
|
|
||||||
],
|
],
|
||||||
|
|
||||||
apollo: {
|
|
||||||
clientConfigs: {
|
|
||||||
default: "~/plugins/apollo.js",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// PWA module configuration (https://pwa.nuxtjs.org/setup)
|
// PWA module configuration (https://pwa.nuxtjs.org/setup)
|
||||||
pwa: {
|
pwa: {
|
||||||
meta: {
|
meta: {
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
export default () => {
|
|
||||||
return {
|
|
||||||
httpEndpoint:
|
|
||||||
process.env.CONTEXT === "production"
|
|
||||||
? "https://api.hoppscotch.io/graphql"
|
|
||||||
: "https://api.hoppscotch.io/graphql",
|
|
||||||
wsEndpoint:
|
|
||||||
process.env.CONTEXT === "production"
|
|
||||||
? "wss://api.hoppscotch.io/graphql"
|
|
||||||
: "wss://api.hoppscotch.io/graphql",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
15
plugins/vue-apollo.ts
Normal file
15
plugins/vue-apollo.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import Vue from "vue"
|
||||||
|
import VueApollo from "vue-apollo"
|
||||||
|
import { apolloClient } from "~/helpers/apollo";
|
||||||
|
|
||||||
|
const vueApolloProvider = new VueApollo({
|
||||||
|
defaultClient: apolloClient as any
|
||||||
|
});
|
||||||
|
|
||||||
|
Vue.use(VueApollo);
|
||||||
|
|
||||||
|
export default (ctx: any) => {
|
||||||
|
const { app } = ctx
|
||||||
|
|
||||||
|
app.apolloProvider = vueApolloProvider
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user