From 985b834d5947279256b30c2186da44185fde593f Mon Sep 17 00:00:00 2001 From: sam585456525 Date: Fri, 13 Oct 2023 11:09:01 +0800 Subject: [PATCH] refactor: fetch-custom-mapper mutation --- .../react-query/src/fetcher-custom-mapper.ts | 33 +++++-------------- .../__snapshots__/react-query.spec.ts.snap | 21 +++++++----- 2 files changed, 21 insertions(+), 33 deletions(-) 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 aed443f6d..e1d0a7339 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-custom-mapper.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-custom-mapper.ts @@ -98,43 +98,28 @@ export class CustomMapperFetcher extends FetcherRenderer { const { documentVariableName, operationResultType, operationVariablesTypes } = config; const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes); - const impl = this._isReactHook + const implFetcher = this._isReactHook ? `${typedFetcher}(${documentVariableName}).bind(null, variables)` : `${typedFetcher}(${documentVariableName}, variables)`; return generateBaseQueryHook({ - implFetcher: impl, + implFetcher, }); } generateMutationHook(config: BuildOperationConfig): string { - const { - node, - documentVariableName, - operationResultType, - operationVariablesTypes, - operationName, - } = config; - const variables = `variables?: ${operationVariablesTypes}`; - const hookConfig = this.visitor.queryMethodMap; - this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook); - this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options); + const { documentVariableName, operationResultType, operationVariablesTypes } = config; + + const { generateBaseMutationHook, variables } = this.generateMutationHelper(config); - const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`; const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes); - const impl = this._isReactHook + const implFetcher = this._isReactHook ? `${typedFetcher}(${documentVariableName})` : `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`; - return `export const use${operationName} = < - TError = ${this.visitor.config.errorType}, - TContext = unknown - >(${options}) => - ${ - hookConfig.mutation.hook - }<${operationResultType}, TError, ${operationVariablesTypes}, TContext>( - ${this.generateMutationFormattedParameters(this.generateMutationKey(node), impl)} - );`; + return generateBaseMutationHook({ + implFetcher, + }); } generateFetcherFetch( 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 18395beb7..531dad03c 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 @@ -315,12 +315,13 @@ export const TestDocument = \` export const useTestMutation = < TError = unknown, TContext = unknown - >(options?: UseMutationOptions) => - useMutation( + >(options?: UseMutationOptions) => { + + return useMutation( ['test'], useCustomFetcher(TestDocument), options - );" + )};" `; exports[`React-Query fetcher: custom-mapper Should generate mutation correctly with lazy variables: prepend 1`] = ` @@ -384,12 +385,13 @@ export const TestDocument = \` export const useTestMutation = < TError = unknown, TContext = unknown - >(options?: UseMutationOptions) => - useMutation( + >(options?: UseMutationOptions) => { + + return useMutation( ['test'], (variables?: TTestMutationVariables) => myCustomFetcher(TestDocument, variables)(), options - );" + )};" `; exports[`React-Query fetcher: custom-mapper Should generate query correctly with external mapper: prepend 1`] = ` @@ -439,12 +441,13 @@ export const TestDocument = \` export const useTestMutation = < TError = unknown, TContext = unknown - >(options?: UseMutationOptions) => - useMutation( + >(options?: UseMutationOptions) => { + + return useMutation( ['test'], (variables?: TTestMutationVariables) => myCustomFetcher(TestDocument, variables)(), options - );" + )};" `; exports[`React-Query fetcher: custom-mapper Should generate query correctly with internal mapper: prepend 1`] = `