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

Importing the library in Node pollutes the environment with DOM types #545

Closed
SimpleCreations opened this issue Jun 15, 2023 · 1 comment · Fixed by #597
Closed

Importing the library in Node pollutes the environment with DOM types #545

SimpleCreations opened this issue Jun 15, 2023 · 1 comment · Fixed by #597
Labels

Comments

@SimpleCreations
Copy link

Description

Importing graphql-request adds DOM/browser types to the environment even if the code is not meant run where DOM is available, causing potential errors by allowing developers to use incorrect types.

This is caused by a dependency on cross-fetch which imports dom library via a triple-slash directive (/// <reference lib="dom" />).

Reproduction Steps/Repo Link

tsconfig.json

{
  "compilerOptions": {
    "module": "nodenext",
    "target": "es2020",
    "lib": ["es2022"]
  },
  "include": ["*.ts"]
}

index.ts

import { GraphQLClient } from 'graphql-request';

let x: HTMLElement;

Commands to reproduce

npm init -y
npm i graphql-request typescript
npx tsc

Expected output

index.ts:3:8 - error TS2304: Cannot find name 'HTMLElement'.

3 let x: HTMLElement;
         ~~~~~~~~~~~

Actual output

*compiles successfully*

Suggested solution

Remove cross-fetch from dependencies and instead let the developer decide how to polyfill fetch if it's not available. fetch is officially supported in Node.js and all major browsers, therefore "polyfill-by-default" approach is not necessary.

@zhang-wenchao
Copy link

I also have similar questions, use nodejs and graphql-code-generator.

query Users {
  project_users {
    user_avatar
    user_id
    auth_user_binds {
      auth_user_bind_id
      auth_user_bind_key
    }
  }
}

export function getSdk(
  client: GraphQLClient,
  withWrapper: SdkFunctionWrapper = defaultWrapper,
) {
  return {
    Users(
      variables?: UsersQueryVariables,
      requestHeaders?: GraphQLClientRequestHeaders,
    ): Promise<{
      data: UsersQuery;
      extensions?: any;
      headers: Dom.Headers;
      status: number;
    }> {
      return withWrapper(
        (wrappedRequestHeaders) =>
          client.rawRequest<UsersQuery>(UsersDocumentString, variables, {
            ...requestHeaders,
            ...wrappedRequestHeaders,
          }),
        'Users',
        'query',
      );
    },
  };
}
export type Sdk = ReturnType<typeof getSdk>;

nodejs not support Dom.Headers。

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

Successfully merging a pull request may close this issue.

2 participants