TypeScript React-Query
Usage Requirements
In order to use this GraphQL Codegen plugin, please make sure that you have GraphQL operations (query
/ mutation
/ subscription
and fragment
) set as documents: ...
in your codegen.yml
.
Without loading your GraphQL operations (query, mutation, subscription and fragment), you won't see any change in the generated output.
This plugin generates React-Query
Hooks with TypeScript typings.
It extends the basic TypeScript plugins: @graphql-codegen/typescript
, @graphql-codegen/typescript-operations
- and thus shares a similar configuration.
Installation
Using yarn
API Reference
fetcher
type: object | string
Customize the fetcher you wish to use in the generated file. React-Query is agnostic to the data-fetcing 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
fetch
to do the actual http call. { endpoint: string, fetchParams: RequestInit }
: hardcode your endpoint and fetch options into the generated output, using the environmentfetch
method. You can also useprocess.env.MY_VAR
as endpoint or header value.file#identifier
- You can use custom fetcher method that should implement the exportedReactQueryFetcher
interface. Example:./my-fetcher#myCustomFetcher
.graphql-request
: Will generate each hook withclient
argument, where you should pass your ownGraphQLClient
(created fromgraphql-request
).
dedupeOperationSuffix
type: boolean
default: false
Set this configuration to true
if you wish to make sure to remove duplicate operation name suffix.
omitOperationSuffix
type: boolean
default: false
Set this configuration to true
if you wish to disable auto add suffix of operation name, like Query
, Mutation
, Subscription
, Fragment
.
operationResultSuffix
type: string
default: ``
Adds a suffix to generated operation result type names
documentVariablePrefix
type: string
default: ``
Changes the GraphQL operations variables prefix.
documentVariableSuffix
type: string
default: Document
Changes the GraphQL operations variables suffix.
fragmentVariablePrefix
type: string
default: ``
Changes the GraphQL fragments variables prefix.
fragmentVariableSuffix
type: string
default: FragmentDoc
Changes the GraphQL fragments variables suffix.
optimizeDocumentNode
type: boolean
default: true
If you are using documentNode: documentMode | 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
).
pureMagicComment
type: boolean
default: false
This config adds PURE magic comment to the static variables to enforce treeshaking for your bundler.
scalars
type: ScalarsMap
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
namingConvention
type: NamingConvention
default: pascal-case#pascalCase
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 preserves underscores.
typesPrefix
type: string
default: ``
Prefixes all the generated types.
Usage Examples
typesSuffix
type: string
default: ``
Suffixes all the generated types.
Usage Examples
skipTypename
type: boolean
default: false
Does not add __typename to the generated types, unless it was specified in the selection set.
Usage Examples
nonOptionalTypename
type: boolean
default: false
Automatically adds __typename
field to the generated types, even when they are not specified
in the selection set, and makes it non-optional
Usage Examples
#
Usage ExamplesNote: all generated hooks are just wrappers around
react-query
original functions. This codegen plugin just burns the generated TypeScript types into the operation, and provides flexibility to choose yourfetcher
.
fetch
#
Using default By default, this plugin will generate a fetcher
based on the environment global fetch
definition.
To use the generate hooks, import it, and then specify the endpoint and optionally fetchParams
:
fetch
with Codegen configuration#
Using 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:
graphql-request
#
Using 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 FetcherIf you wish to create a custom fetcher, you can provide a Mapper string (file#identifier
). Codegen will take care of importing it and use it as a fetcher.
Codegen will use myFetcher
, and you can just use the hook directly:
Your myFetcher
should be in the following signature:
#
Usage exampleNote: The return value is an async function, with no params, that returns a
Promise
with the actual data.