Files
dancefinder-app/plugins/apollo.js
T

50 lines
1.1 KiB
JavaScript
Raw Normal View History

import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client/core'
import { setContext } from '@apollo/client/link/context'
2020-06-21 17:59:14 +02:00
import { provide } from '@vue/composition-api'
import { DefaultApolloClient } from '@vue/apollo-composable'
2020-06-21 19:06:41 +02:00
import { useAuth } from './auth'
2020-06-21 17:59:14 +02:00
const apiUrl = process.env.graphqlApi || '/query'
const cache = new InMemoryCache()
const httpLink = createHttpLink({
uri: apiUrl
})
const getToken = async (options) => {
const { getTokenSilently } = useAuth()
return getTokenSilently.value(options)
2020-01-25 14:59:14 +01:00
}
const authLink = setContext(async (_, { headers }) => {
return getToken().then((token) => ({
headers: {
...headers,
2020-01-25 14:59:14 +01:00
authorization: token ? `Bearer ${token}` : ''
}
}))
})
2020-06-21 17:59:14 +02:00
const link = authLink.concat(httpLink)
const instance = new ApolloClient({
connectToDevTools: true,
link,
cache,
defaultOptions: {
2020-06-21 17:59:14 +02:00
query: {
fetchPolicy: 'cache-and-network'
},
watchQuery: {
2020-01-25 14:59:14 +01:00
fetchPolicy: 'cache-and-network'
}
}
})
export default function ({ app }) {
2021-11-19 20:20:04 +01:00
app.setup = () => {
2020-06-21 17:59:14 +02:00
provide(DefaultApolloClient, instance)
2021-11-19 20:20:04 +01:00
}
}