So I am trying to pass an authorization header to Apollo Client 3 to access a database. The recommended way to do this in the current docs is to create a HttpLink object
const httpLink = new HttpLink({
uri: "************************",
fetch
});
and then use the setContext method (from 'http-link-context' I think):
const authLink = setContext((_, { headers, ...context }) => {
const token = localStorage.getItem("userToken");
return {
headers: {
...headers,
...(token
? { Authorization: `Bearer ${token}` }
: `Bearer *************************`)
},
...context
};
});
then graft the objects together and pass them as a "link" object to the new ApolloClient :
const client = new ApolloClient({
cache: new InMemoryCache(),
link: authLink.concat(httpLink)
});
Unfortunately, though, when I do this I get an error message
Uncaught (in promise) Error: GraphQL error: Missing authorization header.
And when I inspect my request headers I cannot see an authorization header.
Has anyone else been able to get this up and running successfully?