When I initialize my Apollo client, I make an Apollo Link with the header hello: "world"
. Is there a way to override the hello
header value from a component using hooks? I thought this would work but it still uses the Client header:
useQuery(<QUERY>,{
context:{
headers:{
hello: "Canada"
}
}
})
Your code seems correct. After doing some testing it appears that it is not possible to overwrite existing headers.
const { data, loading, error } = useQuery(ALL_GAMES, {
context: { headers: { authentication: 'some value', test: 'some value' } }
});
Perhaps if you pass in a whole new client to useQuery - you could create the client using a factory function
I ran into the same issue when trying to override the authorization header of a request. I solved it by spreading operation.getContext().headers
when I'm setting the headers in my link. Code:
const authLink = new ApolloLink((operation, forward) => {
operation.setContext({
headers: {
authorization: authToken,
...operation.getContext().headers,
},
});
return forward(operation);
});
Then the code you included should work as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With