import { generateInfiniteQueryKey , generateMutationKey , generateQueryKey , generateQueryVariablesSignature , } from './variables-generator' ; import { FetcherRenderer } from './fetcher' ; import { OperationDefinitionNode } from 'graphql' ; import { ReactQueryVisitor } from './visitor' ; export class FetchFetcher implements FetcherRenderer { constructor ( private visitor ) { } generateFetcherImplementaion ( ) { return `
function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
  return async (): Promise<TData> => {
    const res = await fetch(endpoint, {
      method: 'POST',
      ...requestInit,
      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 . reactQueryIdentifiersInUse . add ( hookConfig . infiniteQuery . hook ) ; this . visitor . reactQueryIdentifiersInUse . add ( hookConfig . infiniteQuery . options ) ; const options = `options?: ${ hookConfig . query . options }<${ operationResultType }, TError, TData>` ; return `export const useInfinite${ operationName } = <
      TData = ${ operationResultType },
      TError = ${ this . visitor . config . errorType }
    >(
      dataSource: { endpoint: string, fetchParams?: RequestInit },
      pageParamKey: keyof ${ operationVariablesTypes },
      ${ variables },
      ${ options }
    ) =>
    ${ hookConfig . infiniteQuery . hook }<${ operationResultType }, TError, TData>(
      ${ generateInfiniteQueryKey ( node , hasRequiredVariables ) },
      (metaData) => fetcher<${ operationResultType }, ${ operationVariablesTypes }>(dataSource.endpoint, dataSource.fetchParams || {}, ${ documentVariableName }, {...variables, ...(metaData.pageParam ?? {})})(),
      options
    );` ; } generateQueryHook ( node , documentVariableName , operationName , operationResultType , operationVariablesTypes , hasRequiredVariables ) { const variables = generateQueryVariablesSignature ( 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>` ; return `export const use${ operationName } = <
      TData = ${ operationResultType },
      TError = ${ this . visitor . config . errorType }
    >(
      dataSource: { endpoint: string, fetchParams?: RequestInit },
      ${ variables },
      ${ options }
    ) =>
    ${ hookConfig . query . hook }<${ operationResultType }, TError, TData>(
      ${ generateQueryKey ( node , hasRequiredVariables ) },
      fetcher<${ operationResultType }, ${ operationVariablesTypes }>(dataSource.endpoint, dataSource.fetchParams || {}, ${ documentVariableName }, variables),
      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>` ; return `export const use${ operationName } = <
      TError = ${ this . visitor . config . errorType },
      TContext = unknown
    >(
      dataSource: { endpoint: string, fetchParams?: RequestInit },
      ${ options }
    ) =>
    ${ hookConfig . mutation . hook }<${ operationResultType }, TError, ${ operationVariablesTypes }, TContext>(
      ${ generateMutationKey ( node ) },
      (${ variables }) => fetcher<${ operationResultType }, ${ operationVariablesTypes }>(dataSource.endpoint, dataSource.fetchParams || {}, ${ documentVariableName }, variables)(),
      options
    );` ; } generateFetcherFetch ( node , documentVariableName , operationName , operationResultType , operationVariablesTypes , hasRequiredVariables ) { const variables = generateQueryVariablesSignature ( hasRequiredVariables , operationVariablesTypes ) ; return `\nuse${ operationName }.fetcher = (dataSource: { endpoint: string, fetchParams?: RequestInit }, ${ variables }) => fetcher<${ operationResultType }, ${ operationVariablesTypes }>(dataSource.endpoint, dataSource.fetchParams || {}, ${ documentVariableName }, variables);` ; } }