From ad8258c66edd732a1b5ef2536021f06e44219daa Mon Sep 17 00:00:00 2001 From: pentla Date: Sat, 3 Feb 2024 01:06:26 +0900 Subject: [PATCH] fix(typescript-graphql-request): Fix typescript error (#549) * Fix typescript error on graphql-request on v6.1.0 * Add changeset --------- Co-authored-by: kazuki watanabe --- .changeset/hot-ravens-compare.md | 5 +++ .../typescript/graphql-request/src/visitor.ts | 9 +++-- .../graphql-request.spec.ts.snap | 40 +++++++++---------- 3 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 .changeset/hot-ravens-compare.md diff --git a/.changeset/hot-ravens-compare.md b/.changeset/hot-ravens-compare.md new file mode 100644 index 000000000..7008f4652 --- /dev/null +++ b/.changeset/hot-ravens-compare.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/typescript-graphql-request': minor +--- + +Fix TypeScript error when moduleResolution is "nodenext" or "bundler" diff --git a/packages/plugins/typescript/graphql-request/src/visitor.ts b/packages/plugins/typescript/graphql-request/src/visitor.ts index 2bcdb9f90..35c7bdcfa 100644 --- a/packages/plugins/typescript/graphql-request/src/visitor.ts +++ b/packages/plugins/typescript/graphql-request/src/visitor.ts @@ -45,12 +45,9 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor< autoBind(this); const typeImport = this.config.useTypeImports ? 'import type' : 'import'; - const fileExtension = this.config.emitLegacyCommonJSImports ? '' : '.js'; - const buildPath = this.config.emitLegacyCommonJSImports ? 'cjs' : 'esm'; - this._additionalImports.push(`${typeImport} { GraphQLClient } from 'graphql-request';`); this._additionalImports.push( - `${typeImport} { GraphQLClientRequestHeaders } from 'graphql-request/build/${buildPath}/types${fileExtension}';`, + `${typeImport} { GraphQLClient, RequestOptions } from 'graphql-request';`, ); if (this.config.rawRequest) { @@ -61,6 +58,10 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor< } } + this._additionalImports.push( + `type GraphQLClientRequestHeaders = RequestOptions['requestHeaders'];`, + ); + this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : ''; diff --git a/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap b/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap index 1ab4b4841..9d9329bdd 100644 --- a/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap +++ b/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap @@ -6,8 +6,8 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { ID: string; @@ -291,8 +291,8 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { @@ -570,8 +570,8 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { ID: string; @@ -848,9 +848,9 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import type { GraphQLClient } from 'graphql-request'; -import type { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import type { GraphQLClient, RequestOptions } from 'graphql-request'; import { GraphQLError, print } from 'graphql' +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { @@ -1132,9 +1132,9 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; import { GraphQLError } from 'graphql' +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { ID: string; @@ -1412,8 +1412,8 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/esm/types.js'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { @@ -1692,9 +1692,9 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; import { GraphQLError, print } from 'graphql' +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { @@ -1976,9 +1976,9 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; import { GraphQLError, print } from 'graphql' +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { @@ -2260,8 +2260,8 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import type { GraphQLClient } from 'graphql-request'; -import type { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import type { GraphQLClient, RequestOptions } from 'graphql-request'; +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { @@ -2540,8 +2540,8 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import { GraphQLClient } from 'graphql-request'; -import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types'; +import { GraphQLClient, RequestOptions } from 'graphql-request'; +type GraphQLClientRequestHeaders = RequestOptions['requestHeaders']; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = {