question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-query + customFetcher generates invalid TypeScript (`TS6133: 'RequestInit' is declared but its value is never read.`)

See original GitHub issue

Describe the bug import { RequestInit } from 'graphql-request/dist/types.dom'; is added to generated output even when using a custom fetcher. This is never used, so I’m getting the following error from tsc:

TS6133: 'RequestInit' is declared but its value is never read.

To Reproduce

Check the first line of the generated file: https://codesandbox.io/s/romantic-mirzakhani-57us1?file=/types.ts:0-62

Steps to reproduce the behavior:

  1. use typescript-react-query

  2. use custom fetcher, like `func: ‘…/fetcher#useFetchData’

  3. generate code

  4. try to build app

  5. My GraphQL schema:

no schema, this is a compile-time error

  1. My GraphQL operations:

no operations, this is a compile-time error

  1. My codegen.yml config file:
schema:
  - ${REACT_APP_GRAPHQL_ENDPOINT}
documents:
  - './src/**/*.graphql'
overwrite: true
generates:
  src/gql/generated/index.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-query
    config:
      fetcher:
        func: '../fetcher#useFetchData'
        isReactHook: true
        exposeQueryKeys: true

Expected behavior

Environment:

  • OS: macOS 12.2 on Apple Silicon
    • @graphql-codegen/cli: 2.4.0
    • @graphql-codegen/introspection: 2.1.1
    • @graphql-codegen/typescript: 2.4.2
    • @graphql-codegen/typescript-operations: 2.2.3
    • @graphql-codegen/typescript-react-query: 3.5.0
  • NodeJS: 14.18.3
  • Typescript: 4.5.5

Additional context

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "sourceMap": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "downlevelIteration": true
  },
  "include": ["src", "types"]
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:19

github_iconTop GitHub Comments

1reaction
charlypolycommented, Oct 12, 2022

FWIW, I don’t believe this currently works. mutation hooks are generated by only passing one param to the custom fetcher in v3.6.2. My code example from my generate file below:

Hi @makinde,

You are right, the headers argument is only preset if graphql-request is selected as fetcher option. Each fetcher configuration leads to different hooks signature, which is not ideal. This comes from fixing specific use cases over time, with not standarization.

For this reason, we now recommend to avoid using the generated hooks approach in favor if the new preset: 'client', as showcased here: https://www.the-guild.dev/graphql/codegen/docs/guides/react-vue#installation

1reaction
makindecommented, Oct 4, 2022

Is there any option to pass custom headers in the generated hook?

@dominikx96 if you have a custom fetcher, it would be possible yes

➡️ https://www.graphql-code-generator.com/plugins/typescript-react-query#using-custom-fetcher

FWIW, I don’t believe this currently works. mutation hooks are generated by only passing one param to the custom fetcher in v3.6.2. My code example from my generate file below:

export const useCreateConnectedUser = <
      TError = unknown,
      TContext = unknown
    >(options?: UseMutationOptions<CreateConnectedUser, TError, CreateConnectedUserVariables, TContext>) =>
    useMutation<CreateConnectedUser, TError, CreateConnectedUserVariables, TContext>(
      ['CreateConnectedUser'],
      useFetchData<CreateConnectedUser, CreateConnectedUserVariables>(CreateConnectedUserDocument),
      options
    );
Read more comments on GitHub >

github_iconTop Results From Across the Web

TS6133 error (declared but its value is never read) report ...
Using private vars in .vue files in template reported as unused. Changing to protected solved the issue. Typescript does not know the context...
Read more >
'string' is declared but its value is never read - Stack Overflow
let Customer: React.FC<IProps> = ({ name: string, //'string' is declared but its value is never read. age: number, title: string, ...
Read more >
TypeScript, disable checks for `declared but its value is never ...
If you declare a variable but never use it, TypeScript will not compile, saying '<variable>' is declared but its value is never read...
Read more >
error TS6133: 'functions' is declared but its value is never read.
When I attempt to firbase deploy my cloud functions code written in javascript, I get the following errors in the console: src/index.ts:1:1 ...
Read more >
React Query and TypeScript - TkDodo's blog
It's never wrong because it directly reflects the implementation. I frequently look at type definitions before I read API docs. React Query ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found