Skip to content

Instantly share code, notes, and snippets.

@joshmenden
Created September 3, 2022 01:32
Show Gist options
  • Save joshmenden/b03d34b110e1f3ece59c0b387581a314 to your computer and use it in GitHub Desktop.
Save joshmenden/b03d34b110e1f3ece59c0b387581a314 to your computer and use it in GitHub Desktop.
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client/core'
async function getHeaders() {
const headers = {}
const jwt = // whatever logic you need to fetch your JWT, possible from pinia etc
if (jwt) {
headers['Authorization'] = `Bearer ${jwt}`
}
headers['Content-Type'] = 'application/json'
return headers
}
const defaultOptions = {
query: {
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all',
},
}
const defaultOptions = {
query: {
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all',
},
}
function createClient(url) {
const uri = url
const httpLink = new HttpLink({
uri,
fetch: async (uri, options) => {
// where we inject the headers
options.headers = await getHeaders()
return fetch(uri, options)
},
})
return new ApolloClient({
// where we set an InMemoryCache
cache: new InMemoryCache(),
link: httpLink,
defaultOptions
})
}
export default function (URL1, URL2) {
return {
default: createClient(URL1),
otherClient: createClient(URL2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment