TypeScript React-Query
GraphQL Code Generator plugin for generating React Query hooks. Supports TypeScript and Flow.
- Version
- 7.0.5
- Weekly downloads
- 273k
- License
- MIT
- Updated
- Jun 23, 2026
Installation
npm i -D @graphql-codegen/typescript-react-query
yarn add -D @graphql-codegen/typescript-react-query
pnpm add -D @graphql-codegen/typescript-react-query
bun add -D @graphql-codegen/typescript-react-query
Config API Reference
fetcher
type: object
Customize the fetcher you wish to use in the generated file. React-Query is agnostic to the data-fetching layer, so you should provide it, or use a custom one.
The following options are available to use:
- ‘fetch’ - requires you to specify endpoint and headers on each call, and uses
fetchto do the actual http call. { endpoint: string, fetchParams: RequestInit }: hardcode your endpoint and fetch options into the generated output, using the environmentfetchmethod. You can also useprocess.env.MY_VARas endpoint or header value.file#identifier- You can use custom fetcher method that should implement the exportedReactQueryFetcherinterface. Example:./my-fetcher#myCustomFetcher.graphql-request: Will generate each hook withclientargument, where you should pass your ownGraphQLClient(created fromgraphql-request).
exposeDocument
type: boolean
For each generate query hook adds document field with a
corresponding GraphQL query. Useful for queryClient.fetchQuery.
Default value: “false”
exposeQueryKeys
type: boolean
For each generate query hook adds getKey(variables: QueryVariables) function. Useful for cache updates. If addInfiniteQuery is true, it will also add a getKey function to each infinite query. Default value: “false”
exposeMutationKeys
type: boolean
For each generate mutation hook adds getKey() function. Useful for call outside of functional component. Default value: “false”
exposeFetcher
type: boolean
For each generate query hook adds fetcher field with a corresponding GraphQL query using the fetcher.
It is useful for queryClient.fetchQuery and queryClient.prefetchQuery.
Default value: “false”
errorType
type: string
Changes the default “TError” generic type. Default value: “unknown”
addInfiniteQuery
type: boolean
Adds an Infinite Query along side the standard one Default value: “false”
legacyMode
type: boolean
If false, it will work with @tanstack/react-query, default value is true.
Default value: “true”
emitLegacyCommonJSImports
type: boolean
Emit legacy common js imports.
Default it will be true this way it ensure that generated code works with non-compliant bundlers.
Default value: “true”
importExtension
type: object
Append this extension to all imports. Useful for ESM environments that require file extensions in import statements.
operationResultSuffix
type: string
Adds a suffix to generated operation result type names Default value: ""
dedupeOperationSuffix
type: boolean
Set this configuration to true if you wish to make sure to remove duplicate operation name suffix.
Default value: “false”
omitOperationSuffix
type: boolean
Set this configuration to true if you wish to disable auto add suffix of operation name, like Query, Mutation, Subscription, Fragment.
Default value: “false”
experimentalFragmentVariables
type: boolean
If set to true, it will enable support for parsing variables on fragments. Default value: “false”
scalars
type: object
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
strictScalars
type: boolean
Makes scalars strict.
If scalars are found in the schema that are not defined in scalars
an error will be thrown during codegen.
Default value: “false”
defaultScalarType
type: string
Allows you to override the type that unknown scalars will have. Default value: “unknown”
typesPrefix
type: string
Prefixes all the generated types. Default value: ""
typesSuffix
type: string
Suffixes all the generated types. Default value: ""
enumPrefix
type: boolean
Allow you to disable prefixing for generated enums, works in combination with typesPrefix.
Default value: “true”
enumSuffix
type: boolean
Allow you to disable suffixing for generated enums, works in combination with typesSuffix.
Default value: “true”
inlineFragmentTypes
type: string
Whether fragment types should be inlined into other operations. “inline” is the default behavior and will perform deep inlining fragment types within operation type definitions. “combine” is the previous behavior that uses fragment type references without inlining the types (and might cause issues with deeply nested fragment that uses list types). “mask” transforms the types for use with fragment masking. Useful when masked types are needed when not using the “client” preset e.g. such as combining it with Apollo Client’s data masking feature. Default value: “inline”
printFieldsOnNewLines
type: boolean
If you prefer to have each field in generated types printed on a new line, set this to true. This can be useful for improving readability of the resulting types, without resorting to running tools like Prettier on the output. Default value: “false”
includeExternalFragments
type: boolean
Whether to include external fragments in the generated code. External fragments are not defined in the same location as the operation definition. Default value: “false”
fragmentVariableSuffix
type: string
Changes the GraphQL fragments variables suffix. Default value: “FragmentDoc”
fragmentVariablePrefix
type: string
Changes the GraphQL fragments variables prefix. Default value: ""
documentVariablePrefix
type: string
Changes the GraphQL operations variables prefix. Default value: ""
documentVariableSuffix
type: string
Changes the GraphQL operations variables suffix. Default value: “Document”
optimizeDocumentNode
type: boolean
If you are using documentMode: documentNode | documentNodeImportFragments, you can set this to true to apply document optimizations for your GraphQL document.
This will remove all “loc” and “description” fields from the compiled document, and will remove all empty arrays (such as directives, arguments and variableDefinitions).
Default value: “true”
pureMagicComment
type: boolean
This config adds PURE magic comment to the static variables to enforce treeshaking for your bundler. Default value: “false”
namingConvention
type: object
Allow you to override the naming convention of the output.
You can either override all namings, or specify an object with specific custom naming convention per output.
The format of the converter must be a valid module#method.
Allowed values for specific output are: typeNames, enumValues.
You can also use “keep” to keep all GraphQL names as-is.
Additionally, you can set transformUnderscore to true if you want to override the default behavior,
which is to preserve underscores.
Available case functions in change-case-all are camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCase, lowerCase, localeLowerCase, lowerCaseFirst, spongeCase, titleCase, upperCase, localeUpperCase and upperCaseFirst
See more
Default value: “change-case-all#pascalCase”
Usage Examples
Using default fetch
By default, this plugin will generate a fetcher based on the environment global fetch
definition.
To use the generated hooks, import it, and then specify the endpoint and optionally fetchParams:
Using fetch with Codegen configuration
If you wish to avoid specifying endpoint and fetchParams on each hook usage, you can specify
those in the codegen.yml file:
And if you wish to have more control over the value, or even provide it in runtime, you can use environment variables:
You can even use a custom variable from your code, and add custom imports with add plugin:
The generated hooks doesn’t require you to specify anything, you can just use it as-is:
Using graphql-request
If you are using graphql-request, you can set fetcher to graphql-request, and then the
generated React Hook will expect you to pass the GraphQLClient instance (created by
graphql-request library).
And the, while using, provide your client instance:
Using Custom Fetcher
If you wish to create a custom fetcher, you can provide your own function as a Mapper string
(file#identifier). Codegen will take care of importing it and use it as a fetcher.
As a shortcut, the fetcher property may also directly contain the function as a mapper string:
Codegen will use myFetcher, and you can just use the hook directly:
Depending on the isReactHook property, your myFetcher should be in the following signature:
isReactHook: falseisReactHook: true
Usage example (isReactHook: false)
Usage example (isReactHook: true)
Using Infinite Query
If you wish to use infinite query for pagination or infinite scroll you can with the
addInfiniteQuery config setting. This will however setup an infinite query for every request
whether in reality it can do it or not.
To use this you need to return an object of new queries, and it blends them in to the query.
Usage example (addInfiniteQuery: true)
with the following query: