Skip to content

Commit

Permalink
refactor: fetch-custom-mapper mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
neil585456525 committed Oct 13, 2023
1 parent 40f8787 commit 985b834
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,13 @@ export const TestDocument = \`
export const useTestMutation = <
TError = unknown,
TContext = unknown
>(options?: UseMutationOptions<TTestMutation, TError, TTestMutationVariables, TContext>) =>
useMutation<TTestMutation, TError, TTestMutationVariables, TContext>(
>(options?: UseMutationOptions<TTestMutation, TError, TTestMutationVariables, TContext>) => {
return useMutation<TTestMutation, TError, TTestMutationVariables, TContext>(
['test'],
useCustomFetcher<TTestMutation, TTestMutationVariables>(TestDocument),
options
);"
)};"
`;
exports[`React-Query fetcher: custom-mapper Should generate mutation correctly with lazy variables: prepend 1`] = `
Expand Down Expand Up @@ -384,12 +385,13 @@ export const TestDocument = \`
export const useTestMutation = <
TError = unknown,
TContext = unknown
>(options?: UseMutationOptions<TTestMutation, TError, TTestMutationVariables, TContext>) =>
useMutation<TTestMutation, TError, TTestMutationVariables, TContext>(
>(options?: UseMutationOptions<TTestMutation, TError, TTestMutationVariables, TContext>) => {
return useMutation<TTestMutation, TError, TTestMutationVariables, TContext>(
['test'],
(variables?: TTestMutationVariables) => myCustomFetcher<TTestMutation, TTestMutationVariables>(TestDocument, variables)(),
options
);"
)};"
`;
exports[`React-Query fetcher: custom-mapper Should generate query correctly with external mapper: prepend 1`] = `
Expand Down Expand Up @@ -439,12 +441,13 @@ export const TestDocument = \`
export const useTestMutation = <
TError = unknown,
TContext = unknown
>(options?: UseMutationOptions<TTestMutation, TError, TTestMutationVariables, TContext>) =>
useMutation<TTestMutation, TError, TTestMutationVariables, TContext>(
>(options?: UseMutationOptions<TTestMutation, TError, TTestMutationVariables, TContext>) => {
return useMutation<TTestMutation, TError, TTestMutationVariables, TContext>(
['test'],
(variables?: TTestMutationVariables) => myCustomFetcher<TTestMutation, TTestMutationVariables>(TestDocument, variables)(),
options
);"
)};"
`;
exports[`React-Query fetcher: custom-mapper Should generate query correctly with internal mapper: prepend 1`] = `
Expand Down

0 comments on commit 985b834

Please sign in to comment.