import { from, ApolloLink } from '@apollo/client/core'; import { onError } from '@apollo/client/link/error'; import { setContext } from '@apollo/client/link/context'; import { provideApolloClient } from '@vue/apollo-composable'; import createUploadLink from "apollo-upload-client/createUploadLink.mjs"; import { useAppConfig } from '~/composables/config'; export default defineNuxtPlugin((nuxtApp) => { const runtime = useRuntimeConfig(); const { $apollo } = nuxtApp as any; const errorLink = onError((err) => { nuxtApp.callHook('apollo:error', err); }); const authLink = setContext(async (_, { headers }) => { const localeCookie = useCookie(useAppConfig().COOKIES_LOCALE_KEY); const token = useCookie(useAppConfig().COOKIES_ACCESS_TOKEN_KEY); const hdrs: Record = { ...headers, 'Accept-Language': localeCookie.value || 'en-gb' }; if (token.value) { hdrs['X-SCHON-AUTH'] = `Bearer ${token.value}`; } return { headers: hdrs }; }); const customLink = new ApolloLink((operation, forward) => { return forward(operation).map((data) => { return data; }); }); const httpLink = createUploadLink({ uri: `https://api.${runtime.public.schonBaseDomain}/graphql/` }); $apollo.defaultClient.setLink(from([ errorLink, authLink, customLink, httpLink, ])); provideApolloClient($apollo.defaultClient); });