UNPKG

@graphql-codegen/typescript-react-query

Version:

GraphQL Code Generator plugin for generating a ready-to-use React-Query Hooks based on GraphQL operations

105 lines (100 loc) 5.02 kB
import { generateInfiniteQueryKey, generateMutationKey, generateQueryKey, generateQueryVariablesSignature, } from './variables-generator.js'; export class HardcodedFetchFetcher { constructor(visitor, config) { this.visitor = visitor; this.config = config; } getEndpoint() { try { new URL(this.config.endpoint); return JSON.stringify(this.config.endpoint); } catch (e) { return `${this.config.endpoint} as string`; } } getFetchParams() { let fetchParamsPartial = ''; if (this.config.fetchParams) { const fetchParamsString = typeof this.config.fetchParams === 'string' ? this.config.fetchParams : JSON.stringify(this.config.fetchParams); fetchParamsPartial = `\n ...(${fetchParamsString}),`; } return ` method: "POST",${fetchParamsPartial}`; } generateFetcherImplementaion() { return ` function fetcher<TData, TVariables>(query: string, variables?: TVariables) { return async (): Promise<TData> => { const res = await fetch(${this.getEndpoint()}, { ${this.getFetchParams()} body: JSON.stringify({ query, variables }), }); const json = await res.json(); if (json.errors) { const { message } = json.errors[0]; throw new Error(message); } return json.data; } }`; } generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) { const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes); const hookConfig = this.visitor.queryMethodMap; this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook); this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options); const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`; return `export const useInfinite${operationName} = < TData = ${operationResultType}, TError = ${this.visitor.config.errorType} >( pageParamKey: keyof ${operationVariablesTypes}, ${variables}, ${options} ) => ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>( ${generateInfiniteQueryKey(node, hasRequiredVariables)}, (metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam })(), options );`; } generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) { const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes); const hookConfig = this.visitor.queryMethodMap; this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook); this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options); const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`; return `export const use${operationName} = < TData = ${operationResultType}, TError = ${this.visitor.config.errorType} >( ${variables}, ${options} ) => ${hookConfig.query.hook}<${operationResultType}, TError, TData>( ${generateQueryKey(node, hasRequiredVariables)}, fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables), options );`; } generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) { const variables = `variables?: ${operationVariablesTypes}`; const hookConfig = this.visitor.queryMethodMap; this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook); this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options); const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`; return `export const use${operationName} = < TError = ${this.visitor.config.errorType}, TContext = unknown >(${options}) => ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>( ${generateMutationKey(node)}, (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(), options );`; } generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) { const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes); return `\nuse${operationName}.fetcher = (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables);`; } }