import { ParsedMapper , buildMapperImport , parseMapper } from '@graphql-codegen/visitor-plugin-common' ; import { generateInfiniteQueryKey , generateMutationKey , generateQueryKey } from './variables-generator' ; import { CustomFetch } from './config' ; import { FetcherRenderer } from './fetcher' ; import { OperationDefinitionNode } from 'graphql' ; import { ReactQueryVisitor } from './visitor' ; export class CustomMapperFetcher implements FetcherRenderer { private _mapper : ParsedMapper ; private _isReactHook : boolean ; constructor ( private visitor , customFetcher ) { if ( typeof customFetcher === 'string' ) { customFetcher = { func : customFetcher } ; } this . _mapper = parseMapper ( customFetcher . func ) ; this . _isReactHook = customFetcher . isReactHook ; } private getFetcherFnName ( operationResultType , operationVariablesTypes ) { return `${ this . _mapper . type }<${ operationResultType }, ${ operationVariablesTypes }>` ; } generateFetcherImplementaion ( ) { if ( this . _mapper . isExternal ) { return buildMapperImport ( this . _mapper . source , [ { identifier : this . _mapper . type , asDefault : this . _mapper . default , } , ] , this . visitor . config . useTypeImports ) ; } return null ; } generateInfiniteQueryHook ( node , documentVariableName , operationName , operationResultType , operationVariablesTypes , hasRequiredVariables ) { const variables = `variables${ hasRequiredVariables ? '' : '?' }: ${ operationVariablesTypes }` ; const hookConfig = this . visitor . queryMethodMap ; this . visitor . reactQueryIdentifiersInUse . add ( hookConfig . infiniteQuery . hook ) ; this . visitor . reactQueryIdentifiersInUse . add ( hookConfig . infiniteQuery . options ) ; const options = `options?: ${ hookConfig . infiniteQuery . options }<${ operationResultType }, TError, TData>` ; const typedFetcher = this . getFetcherFnName ( operationResultType , operationVariablesTypes ) ; const implHookOuter = this . _isReactHook ? `const query = ${ typedFetcher }(${ documentVariableName })` : '' ; const impl = this . _isReactHook ? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})` : `(metaData) => ${ typedFetcher }(${ documentVariableName }, {...variables, ...(metaData.pageParam ?? {})})()` ; return `export const useInfinite${ operationName } = <
      TData = ${ operationResultType },
      TError = ${ this . visitor . config . errorType }
    >(
      ${ variables },
      ${ options }
    ) =>{
    ${ implHookOuter }
    return ${ hookConfig . infiniteQuery . hook }<${ operationResultType }, TError, TData>(
      ${ generateInfiniteQueryKey ( node , hasRequiredVariables ) },
      ${ impl },
      options
    )};` ; } generateQueryHook ( node , documentVariableName , operationName , operationResultType , operationVariablesTypes , hasRequiredVariables ) { const variables = `variables${ hasRequiredVariables ? '' : '?' }: ${ operationVariablesTypes }` ; const hookConfig = this . visitor . queryMethodMap ; this . visitor . reactQueryIdentifiersInUse . add ( hookConfig . query . hook ) ; this . visitor . reactQueryIdentifiersInUse . add ( hookConfig . query . options ) ; const options = `options?: ${ hookConfig . query . options }<${ operationResultType }, TError, TData>` ; 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>(
      ${ generateQueryKey ( node , hasRequiredVariables ) },
      ${ impl },
      options
    );` ; } generateMutationHook ( node , documentVariableName , operationName , operationResultType , operationVariablesTypes , hasRequiredVariables ) { const variables = `variables?: ${ operationVariablesTypes }` ; const hookConfig = this . visitor . queryMethodMap ; this . visitor . reactQueryIdentifiersInUse . add ( hookConfig . mutation . hook ) ; this . visitor . reactQueryIdentifiersInUse . add ( hookConfig . mutation . options ) ; const options = `options?: ${ hookConfig . mutation . options }<${ operationResultType }, TError, ${ operationVariablesTypes }, TContext>` ; const typedFetcher = this . getFetcherFnName ( operationResultType , operationVariablesTypes ) ; const impl = 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>(
      ${ generateMutationKey ( node ) },
      ${ impl },
      options
    );` ; } generateFetcherFetch ( node , documentVariableName , operationName , operationResultType , operationVariablesTypes , hasRequiredVariables ) { if ( this . _isReactHook ) return '' ; const variables = `variables${ hasRequiredVariables ? '' : '?' }: ${ operationVariablesTypes }` ; const typedFetcher = this . getFetcherFnName ( operationResultType , operationVariablesTypes ) ; const impl = `${ typedFetcher }(${ documentVariableName }, variables)` ; return `\nuse${ operationName }.fetcher = (${ variables }) => ${ impl };` ; } }