Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: Headers is not defined with 3.1.0 #206

Closed
CapsE opened this issue Sep 5, 2020 · 19 comments · Fixed by #218
Closed

ReferenceError: Headers is not defined with 3.1.0 #206

CapsE opened this issue Sep 5, 2020 · 19 comments · Fixed by #218

Comments

@CapsE
Copy link

CapsE commented Sep 5, 2020

Hi I just tried this library for the first time with your example code
`const { GraphQLClient, gql } = require('graphql-request');

async function main() {
const endpoint = 'https://graphql.contentful.com/content/v1/spaces/'

const graphQLClient = new GraphQLClient(endpoint, {
    headers: {
        Authorization: 'Bearer <TOKEN>',
    },

})

const query = gql`
  query page {
      pageCollection(preview: true) {
        items {
          route,
          contentCollection {
            items {
              type,
              data
            }
          }
        }
      }
}

`

const data = await graphQLClient.request(query)
console.log(JSON.stringify(data, undefined, 2))

}

main().catch((error) => console.error(error))`

Which resulted in the error "ReferenceError: Headers is not defined" the error was only displayed when the headers field was present if I removed the headers no error was given. Downgrading to version 3.0.0 fixed the issue

@jz222
Copy link

jz222 commented Sep 5, 2020

Hi,

I can confirm that. It worked like a charm before updating to the latest version.

const { GraphQLClient } = require("graphql-request");

const client = new GraphQLClient(url);

client.setHeaders({ authorization: `Bearer ${JWT}` });

return client;

@madikun
Copy link

madikun commented Sep 5, 2020

Hi, instead of headers: {} use Headers: {}, it helped me
example:

const client = new GraphQLClient(url, { Headers: { authorization: `Bearer ${JWT}` })

Hi,

I can confirm that. It worked like a charm before updating to the latest version.

const { GraphQLClient } = require("graphql-request");

const client = new GraphQLClient(url);

client.setHeaders({ authorization: `Bearer ${JWT}` });

return client;

@CapsE
Copy link
Author

CapsE commented Sep 5, 2020

@madikun are you sure about that? I tried it and while it got rid of the error it also didn't send the authorization Token for me.

@sazaam
Copy link

sazaam commented Sep 6, 2020

supposedly-native 'Headers' class seems to be the issue

----> graphql-request/dist/index.js
replace

if ( headers instanceof Headers)

by

if ( 'undefined' !== typeof Headers && headers instanceof Headers)

and it reworks instanceof Heavens

@shinnoki
Copy link

shinnoki commented Sep 6, 2020

I'm using TypeScript and workaround like below helped me.

import { GraphQLClient } from "graphql-request";
import { Headers } from "cross-fetch";

// @ts-ignore
global.Headers = global.Headers || Headers;

const client = new GraphQLClient(YOUR_URL_HERE);

@nahtnam
Copy link

nahtnam commented Sep 6, 2020

@shinnoki's workaround worked for me

@asnaseer-resilient
Copy link

@shinnoki's workaround worked for me too

@mykhailo-monchak
Copy link

+1 report of the issue. @shinnoki's workaround worked for me too

@feedm3
Copy link

feedm3 commented Sep 9, 2020

Solution for Node in plain JS:

const { GraphQLClient } = require('graphql-request');
const { Headers } = require('cross-fetch');

global.Headers = global.Headers || Headers;

const graphqlClient = new GraphQLClient(YOUR_URL, {
  headers: {
    authorization: YOUR_TOKEN,
  },
});

@aLx450
Copy link

aLx450 commented Sep 9, 2020

The solution provided by @feedm3 also works. Thank you.

@JamieBradders
Copy link

I've experienced this today also, thanks for posting workaround @feedm3

@crieggalder
Copy link

This helped me too. Thanks @feedm3!

cannikin added a commit to redwoodjs/repeaterdev-js that referenced this issue Sep 14, 2020
@Sceat
Copy link

Sceat commented Sep 15, 2020

they now use cross-fetch which totally breaks custom node-fetch usage, notably cookies as described in the readme 😒

@mi-kas
Copy link

mi-kas commented Sep 16, 2020

Importing the polyfill from cross-fetch fixed the issue for me:

import 'cross-fetch/polyfill';

@csterritt
Copy link

The workaround provided by @mi-kas worked for me... thanks!

NeoTheThird added a commit to NeoTheThird/graphql-request that referenced this issue Oct 3, 2020
@MohamadHFallah
Copy link

MohamadHFallah commented Oct 3, 2020

Solution for Node in plain JS:

const { GraphQLClient } = require('graphql-request');
const { Headers } = require('cross-fetch');

global.Headers = global.Headers || Headers;

const graphqlClient = new GraphQLClient(YOUR_URL, {
  headers: {
    authorization: YOUR_TOKEN,
  },
});

This helped me too in the typescript project. Thanks a lot, @feedm3!

@miking-the-viking
Copy link

I'm using TypeScript and workaround like below helped me.

import { GraphQLClient } from "graphql-request";
import { Headers } from "cross-fetch";

// @ts-ignore
global.Headers = global.Headers || Headers;

const client = new GraphQLClient(YOUR_URL_HERE);

You magnificent web developer! Bless you!

jasonkuhrt added a commit that referenced this issue Oct 15, 2020
Couple things that have been done to avoid similar problem in future:

1. Use namespaced import style to avoid apparent references to global
   Headers that are actually references to the imported TypeScript
   interface.
2. Run tests in jsDOM and Node environments.

closes #206
jasonkuhrt pushed a commit that referenced this issue Oct 15, 2020
Couple things that have been done to avoid similar problem in future:

1. Use namespaced import style to avoid apparent references to global
   Headers that are actually references to the imported TypeScript
   interface.
2. Run tests in jsDOM and Node environments.

closes #206
hugotiburtino added a commit to serlo/api.serlo.org that referenced this issue Oct 16, 2020
@mkausas
Copy link

mkausas commented Nov 26, 2022

in case it helps anyone, Authorization needed to be capitalized for me

  const graphQLClient = new GraphQLClient(PROD_GRAPHQL_URL, {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  });

@P4sca1
Copy link

P4sca1 commented Nov 26, 2022

HTTP header keys are case insensitive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.