I'm trying to change the default header of my Apollo Graph QL Client INSIDE a react component and I simply cant manage to find anything in the docs. So in my index.js I create and link my client as described and add a JWT Token header. But what if I want to change the headers inside my React Component. For example, I want to replace the Token on reauthenticate.
In Axios you can simply do something with this.
axios.defaults.headers.common['Auth-Token'] = 'foo bar';
How can I do something like that with React Apollo? Here is he important part of my index.js
const httpLink = createHttpLink({
uri: 'https://somesecureapi',
});
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token');
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
I tried to export the client and then import it to the component create a new Link and changethe old one but this didn't work.
There must be some easier way for this. Any suggestions