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.

Headers are not passed when using cross-fetch

See original GitHub issue

Intended outcome: Passing custom headers along with my requests when using ApolloClient.

Actual outcome: When using cross-fetch the headers are not passed as expected.

How to reproduce the issue:

Consider a very simple client running on node.js:

const {
  ApolloClient,
  HttpLink,
  InMemoryCache,
  gql,
} = require("@apollo/client/core");
const { fetch } = require("cross-fetch");

const client = new ApolloClient({
  link: new HttpLink({ uri: "https://hookb.in/xYjbyaxeOLh0nNxxnWpY", fetch }),
  cache: new InMemoryCache(),
  headers: { hello: "world" },
});

client
  .query({
    query: gql`
      query {
        fart
      }
    `,
  })
  .then(console.log);

It’s set up to hit a temporary hookbin endpoint and pass along a hello HTTP header with value world. Hookbin receives the request, but there’s no custom headers received at all: Screen Shot 2021-10-24 at 6 17 22 PM

Versions

  System:
    OS: macOS 11.6
  Binaries:
    Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 7.7.6 - ~/.nvm/versions/node/v15.14.0/bin/npm
  Browsers:
    Chrome: 95.0.4638.54
    Firefox: 93.0
    Safari: 15.0
  npmPackages:
    @apollo/client: ^3.4.16 => 3.4.16 

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
david-keefecommented, Nov 18, 2022

The above solution is what we used and it works as expected.

0reactions
jerelmillercommented, Nov 17, 2022

@samuela I’m curious, if you move the headers into the HttpLink options, does it work? It looks like we only do something with that headers value passed directly to ApolloClient when you provide no link. This would explain why you’re not seeing headers. That headers value passed directly to the client is mostly a convenience if you don’t pass a link to it.

Try this and see if you have better luck 🤞 :

const client = new ApolloClient({
  link: new HttpLink({ 
    uri: "https://hookb.in/xYjbyaxeOLh0nNxxnWpY", 
    headers: { hello: "world" },
    fetch 
  }),
  cache: new InMemoryCache(),
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

fetch() does not send headers? - Stack Overflow
To send a cross-origin request with headers like Authorization and X-My-Custom-Header , you have to drop the no-cors mode and support ...
Read more >
Using the Fetch API - MDN Web Docs
Instead, as soon as the server responds with headers, the Promise will resolve normally (with the ok property of the response set to...
Read more >
Fetch API - The Modern JavaScript Tutorial
If we send a fetch , then by default it always sends the Referer header with the full url of our page (except...
Read more >
node-fetch - npm
Start using node-fetch in your project by running `npm i node-fetch`. ... extracted and passed by manipulating request and response headers.
Read more >
How do I send requests using JavaScript Fetch API? - ReqBin
By default, a Fetch API request does not contain user credentials such as cookies and HTTP authentication headers such as a bearer token...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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