Files
dancefinder-app/plugins/apollo.js
T

49 lines
1001 B
JavaScript
Raw Normal View History

2022-08-17 08:42:25 +02:00
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core'
import { setContext } from '@apollo/client/link/context'
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 = (options) => {
const { getTokenSilently } = useAuth()
return getTokenSilently(options)
2020-01-25 14:59:14 +01:00
}
const authLink = setContext((_, { headers }) => {
2022-08-17 08:42:25 +02:00
return getToken()
.then(token => ({
2022-08-17 08:42:25 +02:00
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
})).catch(() => {
return {}
})
})
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 (_, inject) {
inject('apollo', instance)
2021-11-19 20:20:04 +01:00
}