From 4dae1f673247dba99fdeaa8b1129506567bed776 Mon Sep 17 00:00:00 2001 From: Dominic Garms Date: Fri, 27 Nov 2026 10:30:00 +0800 Subject: [PATCH] fix(typescript-react-query): honor plainString document mode --- .changeset/react-query-plain-string.md | 5 +++ .../src/fetcher-fetch-hardcoded.ts | 2 +- .../react-query/src/fetcher-fetch.ts | 2 +- .../src/fetcher-graphql-request.ts | 4 +-- .../typescript/react-query/src/fetcher.ts | 6 ++++ .../typescript/react-query/src/index.ts | 2 +- .../typescript/react-query/src/visitor.ts | 13 ++++++-- .../react-query/tests/react-query.spec.ts | 31 +++++++++++++++++++ 8 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 .changeset/react-query-plain-string.md diff --git a/.changeset/react-query-plain-string.md b/.changeset/react-query-plain-string.md new file mode 100644 index 0000000000..e624ba3694 --- /dev/null +++ b/.changeset/react-query-plain-string.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/typescript-react-query': patch +--- + +Honor `documentMode: plainString` when generating React Query documents and fetchers. diff --git a/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts b/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts index 5bb7b919bc..1787514427 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts @@ -39,7 +39,7 @@ export class HardcodedFetchFetcher extends FetcherRenderer { generateFetcherImplementation(): string { return ` -function fetcher(query: TypedDocumentString, variables?: TVariables) { +function fetcher(query: ${this.queryType}, variables?: TVariables) { return async (): Promise => { const res = await fetch(${this.getEndpoint()}, { ${this.getFetchParams()} diff --git a/packages/plugins/typescript/react-query/src/fetcher-fetch.ts b/packages/plugins/typescript/react-query/src/fetcher-fetch.ts index e176bae70c..acdb300cce 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-fetch.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-fetch.ts @@ -10,7 +10,7 @@ export class FetchFetcher extends FetcherRenderer { generateFetcherImplementation(): string { return ` -function fetcher(endpoint: string, requestInit: RequestInit, query: TypedDocumentString, variables?: TVariables) { +function fetcher(endpoint: string, requestInit: RequestInit, query: ${this.queryType}, variables?: TVariables) { return async (): Promise => { const res = await fetch(endpoint, { method: 'POST', diff --git a/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts b/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts index 84ccd55c22..e49e3837cd 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts @@ -18,7 +18,7 @@ export class GraphQLRequestClientFetcher extends FetcherRenderer { generateFetcherImplementation(): string { return this.clientPath ? ` -function fetcher(query: TypedDocumentString, variables?: TVariables, requestHeaders?: RequestInit['headers']) { +function fetcher(query: ${this.queryType}, variables?: TVariables, requestHeaders?: RequestInit['headers']) { return async (): Promise => graphqlClient.request({ document: query, variables, @@ -26,7 +26,7 @@ function fetcher(query: TypedD }); }` : ` -function fetcher(client: GraphQLClient, query: TypedDocumentString, variables?: TVariables, requestHeaders?: RequestInit['headers']) { +function fetcher(client: GraphQLClient, query: ${this.queryType}, variables?: TVariables, requestHeaders?: RequestInit['headers']) { return async (): Promise => client.request({ document: query, variables, diff --git a/packages/plugins/typescript/react-query/src/fetcher.ts b/packages/plugins/typescript/react-query/src/fetcher.ts index cbdda157be..f5d6709693 100644 --- a/packages/plugins/typescript/react-query/src/fetcher.ts +++ b/packages/plugins/typescript/react-query/src/fetcher.ts @@ -30,6 +30,12 @@ export abstract class FetcherRenderer { autoBind(this); } + protected get queryType(): string { + return this.visitor.usesTypedDocumentString + ? 'TypedDocumentString' + : 'string'; + } + public abstract generateFetcherImplementation(): string; public abstract generateFetcherFetch(config: GenerateConfig): string; protected abstract generateQueryHook(config: GenerateConfig, isSuspense?: boolean): string; diff --git a/packages/plugins/typescript/react-query/src/index.ts b/packages/plugins/typescript/react-query/src/index.ts index 922f867d5e..044737e23a 100644 --- a/packages/plugins/typescript/react-query/src/index.ts +++ b/packages/plugins/typescript/react-query/src/index.ts @@ -36,7 +36,7 @@ export const plugin: PluginFunction 0; } + public get usesTypedDocumentString(): boolean { + return ( + this.config.documentMode === DocumentMode.string && (this.hasFragments || this.hasOperations) + ); + } + public getImports(): string[] { const baseImports = super.getImports(); - if (this.hasFragments || this.hasOperations) { + if (this.usesTypedDocumentString) { const typedDocumentStringPropImport = this._generateImport( typedDocumentString.import, typedDocumentString.import.propName, diff --git a/packages/plugins/typescript/react-query/tests/react-query.spec.ts b/packages/plugins/typescript/react-query/tests/react-query.spec.ts index dc8991aaae..ad96a85a99 100644 --- a/packages/plugins/typescript/react-query/tests/react-query.spec.ts +++ b/packages/plugins/typescript/react-query/tests/react-query.spec.ts @@ -3,6 +3,7 @@ import { mergeOutputs, Types } from '@graphql-codegen/plugin-helpers'; import { validateTs } from '@graphql-codegen/testing'; import { plugin as tsPlugin } from '@graphql-codegen/typescript'; import { plugin as tsDocumentsPlugin } from '@graphql-codegen/typescript-operations'; +import { DocumentMode } from '@graphql-codegen/visitor-plugin-common'; import type { ReactQueryRawPluginConfig } from '../src/config'; import { plugin } from '../src/index.js'; @@ -126,6 +127,36 @@ describe('React-Query', () => { }, ]; + describe('plain string documents', () => { + it.each([ + 'fetch', + { endpoint: 'https://example.com/graphql' }, + 'graphql-request', + { clientImportPath: "import { graphqlClient } from './client';" }, + './fetcher#customFetcher', + ])('generates string operations for fetcher %j', async fetcher => { + const out = await plugin(schema, docs, { documentMode: DocumentMode.plainString, fetcher }); + const output = mergeOutputs([out]); + + expect(out.content).toContain('export const TestDocument = `'); + expect(output).not.toContain('TypedDocumentString'); + expect(output).not.toContain('DocumentTypeDecoration'); + if (fetcher !== './fetcher#customFetcher') { + expect(output).toContain('query: string'); + } + }); + + it('generates a standalone fragment as a string', async () => { + const out = await plugin(schema, notOperationDocs, { + documentMode: DocumentMode.plainString, + }); + + expect(out.content).toContain('export const EntryDataFragmentDoc = `'); + expect(out.content).not.toContain('TypedDocumentString'); + expect(out.prepend).toEqual([]); + }); + }); + it('should allow to override TError type', async () => { const config = { errorType: 'any',