Files
dancefinder-app/plugins/apollo.js
T

62 lines
1.4 KiB
JavaScript
Raw Normal View History

import { ApolloClient } from 'apollo-client'
2020-06-21 19:06:41 +02:00
import {
InMemoryCache,
IntrospectionFragmentMatcher
} from 'apollo-cache-inmemory'
import { createHttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
2020-06-21 17:59:14 +02:00
import { provide } from '@vue/composition-api'
import { onGlobalSetup } from 'nuxt-composition-api'
import { DefaultApolloClient } from '@vue/apollo-composable'
import Vue from 'vue'
2020-06-21 19:06:41 +02:00
import introspectionQueryResultData from '../fragmentTypes.json'
import { useAuth } from './auth'
2020-06-21 17:59:14 +02:00
const apiUrl = process.env.graphqlApi || '/query'
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData
})
const cache = new InMemoryCache({ fragmentMatcher })
const httpLink = createHttpLink({
uri: apiUrl
})
2020-06-21 19:06:41 +02:00
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'
}
}
})
2020-06-21 17:59:14 +02:00
Vue.use(() => {
onGlobalSetup(() => {
provide(DefaultApolloClient, instance)
})
2020-06-21 17:59:14 +02:00
})