Skip to content

Commit

Permalink
refactor: test & custom-mapper query
Browse files Browse the repository at this point in the history
  • Loading branch information
neil585456525 committed Oct 12, 2023
1 parent 8789c7a commit 0c6f67c
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,18 @@ export class CustomMapperFetcher extends FetcherRenderer {
}

generateQueryHook(config: GenerateQueryHookConfig): string {
const {
node,
documentVariableName,
operationName,
operationResultType,
operationVariablesTypes,
hasRequiredVariables,
} = config;

const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;

const hookConfig = this.visitor.queryMethodMap;
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
const { generateBaseQueryHook } = this.generateQueryHelper(config);

const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
const { documentVariableName, operationResultType, operationVariablesTypes } = config;

const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
const impl = this._isReactHook
? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
: `${typedFetcher}(${documentVariableName}, variables)`;

return `export const use${operationName} = <
TData = ${operationResultType},
TError = ${this.visitor.config.errorType}
>(
${variables},
${options}
) =>
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
${this.generateQueryFormattedParameters(
this.generateQueryKey(node, hasRequiredVariables),
impl,
)}
);`;
return generateBaseQueryHook({
implFetcher: impl,
});
}

generateMutationHook(
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/typescript/react-query/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export class BaseFetcherRenderer {
implHookOuter?: string;
implFetcher: string;
}) => {
const { implArguments = '', implHookOuter = '', implFetcher = '' } = config;
const { implArguments, implHookOuter = '', implFetcher } = config;

const argumentsResult =
implArguments ??
`${variables},
${options}`;
${options}`;

return `export const use${operationName} = <
TData = ${operationResultType},
Expand Down
Loading

0 comments on commit 0c6f67c

Please sign in to comment.