graphql-request: Close to working with TypeScript, but not quite -- Error: "Cannot find name RequestInit"

graphql-request doesn’t work with TypeScript projects. I’m seeing the following error:

node_modules/graphql-request/dist/src/types.d.ts(5,14): error TS2304: Cannot find name 'RequestInit'

I found the following related issue: RequestInit is not defined

Any help getting graphql-request working in TypeScript would be appreciated.

Thank you.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 31
  • Comments: 25 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Can you try adding dom to your libs array?

Example:

{
  "compilerOptions": {
    "target": "es5",
    "moduleResolution": "node",
    "module": "commonjs",
    "sourceMap": true,
    "noUnusedLocals": true,
    "rootDir": "src",
    "outDir": "dist",
    "lib": [
      "dom"
    ]
  }
}

For completeness, there are other errors:

node_modules/graphql-request/dist/src/index.d.ts:11:18 - error TS2304: Cannot find name 'Headers'.

11         headers: Headers;
                    ~~~~~~~

node_modules/graphql-request/dist/src/index.d.ts:22:14 - error TS2304: Cannot find name 'Headers'.

22     headers: Headers;
                ~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:8:14 - error TS2304: Cannot find name 'RequestInit'.

8     method?: RequestInit['method'];
               ~~~~~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:10:12 - error TS2304: Cannot find name 'RequestInit'.

10     mode?: RequestInit['mode'];
              ~~~~~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:11:19 - error TS2304: Cannot find name 'RequestInit'.

11     credentials?: RequestInit['credentials'];
                     ~~~~~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:12:13 - error TS2304: Cannot find name 'RequestInit'.

12     cache?: RequestInit['cache'];
               ~~~~~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:13:16 - error TS2304: Cannot find name 'RequestInit'.

13     redirect?: RequestInit['redirect'];
                  ~~~~~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:14:16 - error TS2304: Cannot find name 'RequestInit'.

14     referrer?: RequestInit['referrer'];
                  ~~~~~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:15:22 - error TS2304: Cannot find name 'RequestInit'.

15     referrerPolicy?: RequestInit['referrerPolicy'];
                        ~~~~~~~~~~~

node_modules/graphql-request/dist/src/types.d.ts:16:17 - error TS2304: Cannot find name 'RequestInit'.

16     integrity?: RequestInit['integrity'];
                   ~~~~~~~~~~~


Found 10 errors.

You should not include a dom lib to an api project, it may leads to some weird behaviors.

Why was this closed? I’m using graphql-request for testing graphql with Jest in a nodejs API application and I don’t really want to include dom as lib in tsconfig.json.

fetch is normally a DOM-only API – this package appears to have an API similar to fetch, but not the same. It’s used like:

export interface Options {
    method?: RequestInit['method'];
    headers?: Headers;
    mode?: RequestInit['mode'];
    credentials?: RequestInit['credentials'];
    cache?: RequestInit['cache'];
    redirect?: RequestInit['redirect'];
    referrer?: RequestInit['referrer'];
    referrerPolicy?: RequestInit['referrerPolicy'];
    integrity?: RequestInit['integrity'];
}

Maybe the best solution would be to just copy the relevant parts instead of referencing RequestInit. There’s also Microsoft/TypeScript#15780 which would allow you to pull in dom typings without users needing to modify tsconfig.json, but that then adds a bunch of globals that they might not need.

Can a maintainer please look at one of the two PRs open that addresses this issue?

One quick workaround is to update tsconfig.json:

{
    "compilerOptions": {
        "skipLibCheck": true,
    }
}

I believe your TS compilation will also be faster.