From 166c2f19aa2cbf9bd0438d3d7b7f3f25aa9a38e3 Mon Sep 17 00:00:00 2001 From: sam585456525 Date: Thu, 12 Oct 2023 18:22:10 +0800 Subject: [PATCH] refactor: generateQuery --- .../src/fetcher-fetch-hardcoded.ts | 35 ++---------- .../src/fetcher-graphql-request.ts | 56 ++++--------------- .../__snapshots__/react-query.spec.ts.snap | 49 +++++++++------- 3 files changed, 45 insertions(+), 95 deletions(-) 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 c0c15c356..79c0c02c2 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-fetch-hardcoded.ts @@ -94,38 +94,13 @@ ${this.getFetchParams()} } generateQueryHook(config: GenerateQueryHookConfig): string { - const { - node, - documentVariableName, - operationName, - operationResultType, - operationVariablesTypes, - hasRequiredVariables, - } = config; - - const variables = this.generateQueryVariablesSignature( - 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; - 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), - `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)`, - )} - );`; + return generateBaseQueryHook({ + implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)`, + }); } generateMutationHook( 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 69cebcff3..4bee22ec7 100644 --- a/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts +++ b/packages/plugins/typescript/react-query/src/fetcher-graphql-request.ts @@ -93,20 +93,6 @@ function fetcher(client: Graph } generateQueryHook(config: GenerateQueryHookConfig): string { - const { - node, - documentVariableName, - operationName, - operationResultType, - operationVariablesTypes, - hasRequiredVariables, - } = config; - - const variables = this.generateQueryVariablesSignature( - hasRequiredVariables, - operationVariablesTypes, - ); - const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import'; if (this.clientPath) this.visitor.imports.add(this.clientPath); this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`); @@ -114,42 +100,24 @@ function fetcher(client: Graph `${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`, ); - const hookConfig = this.visitor.queryMethodMap; - this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook); - this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options); + const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config); - const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`; + const { documentVariableName, operationResultType, operationVariablesTypes } = config; return this.clientPath - ? `export const use${operationName} = < - TData = ${operationResultType}, - TError = ${this.visitor.config.errorType} - >( - ${variables}, + ? generateBaseQueryHook({ + implArguments: `${variables}, ${options}, - headers?: RequestInit['headers'] - ) => - ${hookConfig.query.hook}<${operationResultType}, TError, TData>( - ${this.generateQueryFormattedParameters( - this.generateQueryKey(node, hasRequiredVariables), - `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)`, - )} - );` - : `export const use${operationName} = < - TData = ${operationResultType}, - TError = ${this.visitor.config.errorType} - >( - client: GraphQLClient, + headers?: RequestInit['headers']`, + implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)`, + }) + : generateBaseQueryHook({ + implArguments: `client: GraphQLClient, ${variables}, ${options}, - headers?: RequestInit['headers'] - ) => - ${hookConfig.query.hook}<${operationResultType}, TError, TData>( - ${this.generateQueryFormattedParameters( - this.generateQueryKey(node, hasRequiredVariables), - `fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)`, - )} - );`; + headers?: RequestInit['headers']`, + implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)`, + }); } generateMutationHook( 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 d4d4692f7..6438e6b41 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 @@ -553,12 +553,13 @@ export const useTestQuery = < variables?: TTestQueryVariables, options?: UseQueryOptions, headers?: RequestInit['headers'] - ) => - useQuery( + ) => { + + return useQuery( variables === undefined ? ['test'] : ['test', variables], fetcher(client, TestDocument, variables, headers), options - ); + )}; export const TestDocument = \` mutation test($name: String) { submitRepository(repoFullName: $name) { @@ -621,12 +622,13 @@ export const useTestQuery = < variables?: TTestQueryVariables, options?: UseQueryOptions, headers?: RequestInit['headers'] - ) => - useQuery( + ) => { + + return useQuery( variables === undefined ? ['test'] : ['test', variables], fetcher(TestDocument, variables, headers), options - ); + )}; export const TestDocument = \` mutation test($name: String) { submitRepository(repoFullName: $name) { @@ -688,12 +690,13 @@ export const useTestQuery = < >( variables?: TTestQueryVariables, options?: UseQueryOptions - ) => - useQuery( + ) => { + + return useQuery( variables === undefined ? ['test'] : ['test', variables], fetcher(TestDocument, variables), options - ); + )}; export const TestDocument = \` mutation test($name: String) { submitRepository(repoFullName: $name) { @@ -761,12 +764,13 @@ export const useTestQuery = < >( variables?: TTestQueryVariables, options?: UseQueryOptions - ) => - useQuery( + ) => { + + return useQuery( variables === undefined ? ['test'] : ['test', variables], fetcher(TestDocument, variables), options - ); + )}; export const TestDocument = \` mutation test($name: String) { submitRepository(repoFullName: $name) { @@ -834,12 +838,13 @@ export const useTestQuery = < >( variables?: TTestQueryVariables, options?: UseQueryOptions - ) => - useQuery( + ) => { + + return useQuery( variables === undefined ? ['test'] : ['test', variables], fetcher(TestDocument, variables), options - ); + )}; export const TestDocument = \` mutation test($name: String) { submitRepository(repoFullName: $name) { @@ -906,12 +911,13 @@ export const useTestQuery = < >( variables?: TTestQueryVariables, options?: UseQueryOptions - ) => - useQuery( + ) => { + + return useQuery( variables === undefined ? ['test'] : ['test', variables], fetcher(TestDocument, variables), options - ); + )}; export const TestDocument = \` mutation test($name: String) { submitRepository(repoFullName: $name) { @@ -978,12 +984,13 @@ export const useTestQuery = < >( variables?: TTestQueryVariables, options?: UseQueryOptions - ) => - useQuery( + ) => { + + return useQuery( variables === undefined ? ['test'] : ['test', variables], fetcher(TestDocument, variables), options - ); + )}; export const TestDocument = \` mutation test($name: String) { submitRepository(repoFullName: $name) {