Skip to content

Commit

Permalink
refactor: generateQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
neil585456525 committed Oct 12, 2023
1 parent 0c6f67c commit 166c2f1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,63 +93,31 @@ function fetcher<TData, TVariables extends { [key: string]: any }>(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';`);
this.visitor.imports.add(
`${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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,13 @@ export const useTestQuery = <
variables?: TTestQueryVariables,
options?: UseQueryOptions<TTestQuery, TError, TData>,
headers?: RequestInit['headers']
) =>
useQuery<TTestQuery, TError, TData>(
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(client, TestDocument, variables, headers),
options
);
)};
export const TestDocument = \`
mutation test($name: String) {
submitRepository(repoFullName: $name) {
Expand Down Expand Up @@ -621,12 +622,13 @@ export const useTestQuery = <
variables?: TTestQueryVariables,
options?: UseQueryOptions<TTestQuery, TError, TData>,
headers?: RequestInit['headers']
) =>
useQuery<TTestQuery, TError, TData>(
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables, headers),
options
);
)};
export const TestDocument = \`
mutation test($name: String) {
submitRepository(repoFullName: $name) {
Expand Down Expand Up @@ -688,12 +690,13 @@ export const useTestQuery = <
>(
variables?: TTestQueryVariables,
options?: UseQueryOptions<TTestQuery, TError, TData>
) =>
useQuery<TTestQuery, TError, TData>(
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
);
)};
export const TestDocument = \`
mutation test($name: String) {
submitRepository(repoFullName: $name) {
Expand Down Expand Up @@ -761,12 +764,13 @@ export const useTestQuery = <
>(
variables?: TTestQueryVariables,
options?: UseQueryOptions<TTestQuery, TError, TData>
) =>
useQuery<TTestQuery, TError, TData>(
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
);
)};
export const TestDocument = \`
mutation test($name: String) {
submitRepository(repoFullName: $name) {
Expand Down Expand Up @@ -834,12 +838,13 @@ export const useTestQuery = <
>(
variables?: TTestQueryVariables,
options?: UseQueryOptions<TTestQuery, TError, TData>
) =>
useQuery<TTestQuery, TError, TData>(
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
);
)};
export const TestDocument = \`
mutation test($name: String) {
submitRepository(repoFullName: $name) {
Expand Down Expand Up @@ -906,12 +911,13 @@ export const useTestQuery = <
>(
variables?: TTestQueryVariables,
options?: UseQueryOptions<TTestQuery, TError, TData>
) =>
useQuery<TTestQuery, TError, TData>(
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
);
)};
export const TestDocument = \`
mutation test($name: String) {
submitRepository(repoFullName: $name) {
Expand Down Expand Up @@ -978,12 +984,13 @@ export const useTestQuery = <
>(
variables?: TTestQueryVariables,
options?: UseQueryOptions<TTestQuery, TError, TData>
) =>
useQuery<TTestQuery, TError, TData>(
) => {
return useQuery<TTestQuery, TError, TData>(
variables === undefined ? ['test'] : ['test', variables],
fetcher<TTestQuery, TTestQueryVariables>(TestDocument, variables),
options
);
)};
export const TestDocument = \`
mutation test($name: String) {
submitRepository(repoFullName: $name) {
Expand Down

0 comments on commit 166c2f1

Please sign in to comment.