diff --git a/packages/plugins/typescript/react-query/CHANGELOG.md b/packages/plugins/typescript/react-query/CHANGELOG.md index 8274a0103..6cc168b7d 100644 --- a/packages/plugins/typescript/react-query/CHANGELOG.md +++ b/packages/plugins/typescript/react-query/CHANGELOG.md @@ -1,5 +1,11 @@ # @graphql-codegen/typescript-react-query +## 4.0.5 + +### Patch Changes + +- [#8567](https://github.com/dotansimha/graphql-code-generator/pull/8567) [`af40efbd2`](https://github.com/dotansimha/graphql-code-generator/commit/af40efbd23850c6bdb5f5b3d8713bac24e72fe44) Thanks [@EandrewJones](https://github.com/EandrewJones)! - fixes issue #7549 + ## 4.0.4 ### Patch Changes diff --git a/packages/plugins/typescript/react-query/package.json b/packages/plugins/typescript/react-query/package.json index dc5910d30..a42396b29 100644 --- a/packages/plugins/typescript/react-query/package.json +++ b/packages/plugins/typescript/react-query/package.json @@ -1,6 +1,6 @@ { "name": "@graphql-codegen/typescript-react-query", - "version": "4.0.4", + "version": "4.0.5", "description": "GraphQL Code Generator plugin for generating a ready-to-use React-Query Hooks based on GraphQL operations", "repository": { "type": "git", diff --git a/packages/plugins/typescript/react-query/src/fetcher-custom-mapper.ts b/packages/plugins/typescript/react-query/src/fetcher-custom-mapper.ts index eec68f0d2..bf42262d8 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-custom-mapper.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-custom-mapper.ts @@ -59,8 +59,8 @@ export class CustomMapperFetcher implements FetcherRenderer { const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes); const implHookOuter = this._isReactHook ? `const query = ${typedFetcher}(${documentVariableName})` : ''; const impl = this._isReactHook - ? `(metaData) => query({...variables, [pageParamKey]: metaData.pageParam })` - : `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam })()`; + ? `(metaData) => query({...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})` + : `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})()`; return `export const useInfinite${operationName} = < TData = ${operationResultType}, 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 c424f8ce6..5f952318c 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts @@ -83,7 +83,7 @@ ${this.getFetchParams()} ) => ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>( ${generateInfiniteQueryKey(node, hasRequiredVariables)}, - (metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam })(), + (metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})(), options );`; } diff --git a/packages/plugins/typescript/react-query/src/fetcher-fetch.ts b/packages/plugins/typescript/react-query/src/fetcher-fetch.ts index acf09c02e..9b0b7dac9 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-fetch.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-fetch.ts @@ -61,7 +61,7 @@ function fetcher(endpoint: string, requestInit: RequestInit, ) => ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>( ${generateInfiniteQueryKey(node, hasRequiredVariables)}, - (metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam })(), + (metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})(), options );`; } 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 ce4016c45..c7482903d 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts @@ -50,7 +50,7 @@ function fetcher(client: GraphQLClient, query: string, variab ) => ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>( ${generateInfiniteQueryKey(node, hasRequiredVariables)}, - (metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam }, headers)(), + (metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})}, headers)(), options );`; } diff --git a/packages/plugins/typescript/react-query/tests/__snapshots__/react-query.spec.ts.snap b/packages/plugins/typescript/react-query/tests/__snapshots__/react-query.spec.ts.snap index 9343639bd..f5ad84ad4 100644 --- a/packages/plugins/typescript/react-query/tests/__snapshots__/react-query.spec.ts.snap +++ b/packages/plugins/typescript/react-query/tests/__snapshots__/react-query.spec.ts.snap @@ -40,7 +40,7 @@ export const useInfiniteTestQuery = < const query = useCustomFetcher(TestDocument) return useInfiniteQuery( variables === undefined ? ['test.infinite'] : ['test.infinite', variables], - (metaData) => query({...variables, [pageParamKey]: metaData.pageParam }), + (metaData) => query({...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})}), options )}; @@ -102,7 +102,7 @@ export const useInfiniteTestQuery = < return useInfiniteQuery( variables === undefined ? ['test.infinite'] : ['test.infinite', variables], - (metaData) => myCustomFetcher(TestDocument, {...variables, [pageParamKey]: metaData.pageParam })(), + (metaData) => myCustomFetcher(TestDocument, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})(), options )}; 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 f98336a79..ee1fae93d 100644 --- a/packages/plugins/typescript/react-query/tests/react-query.spec.ts +++ b/packages/plugins/typescript/react-query/tests/react-query.spec.ts @@ -93,7 +93,7 @@ export const useInfiniteTestQuery = < ) => useInfiniteQuery( variables === undefined ? ['test.infinite'] : ['test.infinite', variables], - (metaData) => fetcher(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, {...variables, [pageParamKey]: metaData.pageParam })(), + (metaData) => fetcher(dataSource.endpoint, dataSource.fetchParams || {}, TestDocument, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})(), options ); @@ -269,7 +269,7 @@ export const useTestMutation = < ) =>{ return useInfiniteQuery( variables === undefined ? ['test.infinite'] : ['test.infinite', variables], - (metaData) => myCustomFetcher(TestDocument, {...variables, [pageParamKey]: metaData.pageParam })(), + (metaData) => myCustomFetcher(TestDocument, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})(), options )};`); expect(out.content).toBeSimilarStringTo(`export const useTestMutation = < @@ -366,7 +366,7 @@ export const useTestMutation = < const query = useCustomFetcher(TestDocument) return useInfiniteQuery( variables === undefined ? ['test.infinite'] : ['test.infinite', variables], - (metaData) => query({...variables, [pageParamKey]: metaData.pageParam }), + (metaData) => query({...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})}), options )};`); expect(out.content).toBeSimilarStringTo(`export const useTestMutation = <