question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

empty headers in in afterware link

See original GitHub issue

Intended outcome:

I’m setting a custom header on the response I’m supposed being able to access in the afterware link

Actual outcome:

The headers object is empty

How to reproduce the issue:

Some context: I have a custom header in response visible in network tab but not in the response object.

I know that the custom header is stripped away by the browser as this is a CORS request so I put it into Access-Control-Expose-Headers.

Now the browser exposes the custom header just fine as you may see in the screenshot and I should be able to access it within the afterwareLink. Though that’s not the case.

headers

The headers object is always empty though getContext yields forceFetch and cache fields

console

I’m using: CLIENT: “apollo”: “0.2.2”, “apollo-cache-inmemory”: “1.1.0”, “apollo-client”: “2.0.2”, “apollo-link”: “1.0.0”, “apollo-link-context”: “1.0.0”, “apollo-link-error”: “1.0.0”, “apollo-link-http”: “1.1.0”, “graphql”: “0.11.7”, “graphql-tag”: “2.5.0”, “vue”: “2.5.3”, “vue-apollo”: “3.0.0-alpha.2”,

SERVER: “apollo-server-express”: “1.2.0”, “body-parser”: “^1.17.1”, “cors”: “^2.8.3”, “express”: “^4.15.2”, “graphql-tools”: “^1.2.3”,

Afterware code:

const afterwareLink = new ApolloLink((operation, forward) => {
  const { headers } = operation.getContext();
  console.log(operation.getContext());
  if (headers) {
    const refreshToken = headers.get('x-refresh-token');
    console.log('refreshToken', refreshToken);
  }
  return forward(operation);
}

Version

  • apollo-client@2.0.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
ecerronicommented, Nov 16, 2017

@Knaackee That’s ok I guess as it is the same thing I see in mine.

However headers.get('x-refresh-token') correctly retrieves the token and I can both console.log it and set it in the localStorage in the afterware code.

Do you mean it is not working for you? I mean retrieving the header using headers.get('x-refresh-token')

A complete example:

import { ApolloLink } from 'apollo-link';
import { createHttpLink } from 'apollo-link-http';

const httpLink = createHttpLink({ uri: 'http://localhost:3001/graphql' });

const afterwareLink = new ApolloLink((operation, forward) => {
  return forward(operation).map(response => {
    const context = operation.getContext();
    const { response: { headers } } = context;

    if (headers) {
      const token = headers.get("x-refresh-token");
      
      if (token) {
        localStorage.setItem("token", token);
      }

    }
    return response;
  });
});

// use with apollo-client
const link = afterwareLink.concat(httpLink);
18reactions
alamothecommented, May 14, 2019

Wasted a couple of hours too 😃

Looks like if you console.log headers, it will appear as empty. Iterating over it doesn’t work either.

Whereas getting the header by name: context.response.headers.get('...') magically works. Now only to find the Chrome engineer who is responsible for this behavior…

Read more comments on GitHub >

github_iconTop Results From Across the Web

apollo-client has no headers in response
The server answers with a new JWT-Token in every response header, which i need. So the solution should be simple .. a afterware...
Read more >
Fixing Apollo Client Afterware - YouTube
Learn how to correctly get Apollo Client afterware to work. Allowing you to read the headers from the response.
Read more >
Headers
Header Layouts · Hover over the header and click Header. · On the Design tab, select a new header layout. Switching to one...
Read more >
Advanced HTTP networking - Apollo GraphQL Docs
Customizing request logic. The following example demonstrates adding a custom link to Apollo Client. This link adds an Authorization header to every HTTP ......
Read more >
How to use the apollo-link.from function in apollo-link
To help you get started, we've selected a few apollo-link.from examples, ... 缓存 export default new ApolloClient({ link: from([Middleware, Afterware, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found