apollo-client: Unable to get apollo client response headers
I am able to see the response headers in the network but unable to get those response headers.
Please follow the below screenshots.
Network Response Headers:
Console Response Data(Empty headers):
Actual tried Code:
const httpLink = new HttpLink({ uri: URL_SERVER_GRAPHQL })
// Setup the header for the request
const middlewareAuthLink = new ApolloLink((operation, forward) => {
const token = localStorage.getItem(AUTH_TOKEN)
const authorizationHeader = token ? `Bearer ${token}` : null
operation.setContext({
headers: {
authorization: authorizationHeader
}
})
return forward(operation)
})
//After the backend responds, we take the refreshToken from headers if it exists, and save it in the cookie.
const afterwareLink = new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
const context = operation.getContext()
const {
response: { headers }
} = context
if (headers) {
const refreshToken = headers.get('refreshToken')
if (refreshToken) {
localStorage.setItem(AUTH_TOKEN, refreshToken)
}
}
return response
})
})
const client = new ApolloClient({
link: from([middlewareAuthLink, afterwareLink, httpLink]),
cache: new InMemoryCache()
})
Please help with this issue.
Thanks in advance.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 21 (5 by maintainers)
Hi @brainkim, Any update regarding this issue…?
Hi @brainkim, Any update regarding this issue…? let me know if anything is required.
@Suribabu-Balam from your client side code example above, in your
afterwareLink
, you’re calling the following:I don’t see
refreshToken
being set anywhere in the server reproduction you provided. Headers are working though; if you want to verify this, in your server reproduction, add the following 2 lines to the top of yourindex.js
context
function:Then back in your client app, adjust your
afterwareLink
to add the following:If you run that, you’ll see that the
some-header
value is logged properly on the client side.All of the above being said, this isn’t a bug with Apollo Client, so I’ll close this off. If you would like to continue this discussion, head over to our community forums: https://community.apollographql.com - thanks!
Are you ensuring that you’re specifying
Access-Control-Expose-Headers
in the response headers from the server?Hi @brainkim,
Here is the GitHub repository URL: https://github.com/Suribabu-Balam/apollo-headers
Please check and let me know if anything is required.
Ok, I will provide the git hub repository URL.