mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.1 KiB
53 lines
1.1 KiB
import { boot } from 'quasar/wrappers'
|
|
import { ApolloClient, InMemoryCache } from '@apollo/client/core'
|
|
import { setContext } from '@apollo/client/link/context'
|
|
import { createUploadLink } from 'apollo-upload-client'
|
|
|
|
import { useUserStore } from 'src/stores/user'
|
|
|
|
export default boot(({ app }) => {
|
|
const userStore = useUserStore()
|
|
|
|
// Authentication Link
|
|
const authLink = setContext(async (req, { headers }) => {
|
|
const token = userStore.token
|
|
return {
|
|
headers: {
|
|
...headers,
|
|
Authorization: token ? `Bearer ${token}` : ''
|
|
}
|
|
}
|
|
})
|
|
|
|
// Upload / HTTP Link
|
|
const uploadLink = createUploadLink({
|
|
uri () {
|
|
return '/_graphql'
|
|
}
|
|
})
|
|
|
|
// Cache
|
|
const cache = new InMemoryCache()
|
|
|
|
if (typeof window !== 'undefined') {
|
|
const state = window.__APOLLO_STATE__
|
|
if (state) {
|
|
cache.restore(state.defaultClient)
|
|
}
|
|
}
|
|
|
|
// Client
|
|
const client = new ApolloClient({
|
|
cache,
|
|
link: authLink.concat(uploadLink),
|
|
credentials: 'omit',
|
|
ssrForceFetchDelay: 100
|
|
})
|
|
|
|
if (import.meta.env.SSR) {
|
|
global.APOLLO_CLIENT = client
|
|
} else {
|
|
window.APOLLO_CLIENT = client
|
|
}
|
|
})
|