dotansimha / graphql-code-generator-community

MIT License
119 stars 157 forks source link

exposeFetcher with custom fetcher func does not work as expected #215

Open aimproxy opened 2 years ago

aimproxy commented 2 years ago

Describe the bug

Does not work with these settings:

      fetcher: # applicable for react-query
        func: './../../@hasura-graphql/hooks/useHasura#useHasuraFetcher' # yes, the path is correct
        isReactHook: true

Steps to Reproduce the Bug or Issue

Follow the docs to reproduce the issue: https://www.graphql-code-generator.com/plugins/typescript/typescript-react-query#usage-example-isreacthook-false

Expected behavior

export const useGetShopByShopDomainQuery = <
      TData = GetShopByShopDomainQuery,
      TError = unknown
    >(
      variables: GetShopByShopDomainQueryVariables,
      options?: UseQueryOptions<GetShopByShopDomainQuery, TError, TData>
    ) =>
    useQuery<GetShopByShopDomainQuery, TError, TData>(
      ['GetShopByShopDomain', variables],
      fetcher<GetShopByShopDomainQuery, GetShopByShopDomainQueryVariables>(GetShopByShopDomainDocument, variables),
      options
    );

useGetShopByShopDomainQuery.getKey = (variables: GetShopByShopDomainQueryVariables) => ['GetShopByShopDomain', variables];

// Excepted this, an exported fetcher
useGetShopByShopDomainQuery.fetcher = (variables: GetShopByShopDomainQueryVariables) => fetcher<GetShopByShopDomainQuery, GetShopByShopDomainQueryVariables>(GetShopByShopDomainDocument, variables);

Platform

{
    "graphql": "^16.5.0",
    "react-query": "3",
    "@graphql-codegen/cli": "^2.6.3",
    "@graphql-codegen/typescript": "^2.5.1",
    "@graphql-codegen/typescript-operations": "^2.4.2",
    "@graphql-codegen/typescript-react-query": "^3.5.14"
}

Codegen Config File

schema:
  - ${HASURA_ENDPOINT}:
      headers:
        x-hasura-admin-secret: ${HASURA_SECRET}
documents: 'src/@hasura-graphql/{queries,mutations}/*.graphql'
generates:
  ./src/@hasura-graphql/generated/react-query.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-query
    config:
      pureMagicComment: true # enforce tree-shaking
      exposeQueryKeys: true # to prefetch queries (SSR)
      exposeMutationKeys: true
      fetcher: # applicable for react-query
        endpoint: ${HASURA_ENDPOINT}
        fetchParams:
          headers:
            x-hasura-admin-secret: ${HASURA_SECRET}
        #func: './../../@hasura-graphql/hooks/useHasura#useHasuraFetcher' 
        #isReactHook: true
      exposeFetcher: true
  ./src/@hasura-graphql/generated/graphql-request.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-graphql-request
coltontravers commented 2 years ago

Also having this issue. Did you ever find a solution, @aimproxy ?

ceefour commented 11 months ago

I still encounter this issue with "@graphql-codegen/typescript-react-query": "^6.0.0".

Reproducer: https://stackblitz.com/edit/github-cweboc?file=codegen.ts