-
Notifications
You must be signed in to change notification settings - Fork 308
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
Comments
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; |
Hi, instead of headers: {} use Headers: {}, it helped me const client = new GraphQLClient(url, { Headers: { authorization: `Bearer ${JWT}` })
|
@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. |
supposedly-native 'Headers' class seems to be the issue ----> graphql-request/dist/index.js if ( headers instanceof Headers) by if ( 'undefined' !== typeof Headers && headers instanceof Headers) and it reworks instanceof Heavens |
I'm using TypeScript and workaround like below helped me.
|
@shinnoki's workaround worked for me |
@shinnoki's workaround worked for me too |
+1 report of the issue. @shinnoki's workaround worked for me too |
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,
},
}); |
The solution provided by @feedm3 also works. Thank you. |
I've experienced this today also, thanks for posting workaround @feedm3 |
This helped me too. Thanks @feedm3! |
they now use |
Importing the polyfill from import 'cross-fetch/polyfill'; |
The workaround provided by @mi-kas worked for me... thanks! |
This helped me too in the typescript project. Thanks a lot, @feedm3! |
You magnificent web developer! Bless you! |
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
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
Because of issue graffle-js/graffle#206
in case it helps anyone,
|
HTTP header keys are case insensitive. |
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/'
`
}
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
The text was updated successfully, but these errors were encountered: