TypeScript GraphQL-Request
GraphQL Code Generator plugin for generating TypeScript code using graphql-request. This plugin generates a fully-typed SDK based on your GraphQL operations.
- Version
- 8.0.2
- Weekly downloads
- 1.2M
- License
- MIT
- Updated
- Nov 12, 2026
Installation
npm i -D @graphql-codegen/typescript-graphql-request
yarn add -D @graphql-codegen/typescript-graphql-request
pnpm add -D @graphql-codegen/typescript-graphql-request
bun add -D @graphql-codegen/typescript-graphql-request
Config API Reference
rawRequest
type: boolean
By default, the request method return the data or errors key from the response. If you need to access the extensions key you can use the rawRequest method.
Default value: “false”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-graphql-request'],
config: {
rawRequest: true
},
},
},
};
export default config;
extensionsType
type: string
Allows you to override the type for extensions when rawRequest is enabled.
Default value: “any”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-graphql-request'],
config: {
rawRequest: true,
extensionsType: 'unknown'
},
},
},
};
export default config;
gqlImport
type: string
Customize from which module will gql be imported from.
This is useful if you want to use modules other than graphql-tag, e.g. graphql.macro.
Default value: “graphql-tag#gql”
Usage Examples
graphql.macro
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
gqlImport: 'graphql.macro#gql'
},
},
},
};
export default config;
Gatsby
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
gqlImport: 'gatsby#graphql'
},
},
},
};
export default config;
documentNodeImport
type: string
Customize from which module will DocumentNode be imported from.
This is useful if you want to use modules other than graphql, e.g. @graphql-typed-document-node.
Default value: “graphql#DocumentNode”
noExport
type: boolean
Set this configuration to true if you wish to tell codegen to generate code with no export identifier.
Default value: “false”
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”
operationResultSuffix
type: string
Adds a suffix to generated operation result type names 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”
fragmentVariablePrefix
type: string
Changes the GraphQL fragments variables prefix. Default value: ""
fragmentVariableSuffix
type: string
Changes the GraphQL fragments variables suffix. Default value: “FragmentDoc”
documentMode
type: object
Declares how DocumentNode are created:
graphQLTag:graphql-tagor other modules (checkgqlImport) will be used to generate document nodes. If this is used, document nodes are generated on client side i.e. the module used to generate this will be shipped to the clientdocumentNode: document nodes will be generated as objects when we generate the templates.documentNodeImportFragments: Similar to documentNode except it imports external fragments instead of embedding them.external: document nodes are imported from an external file. To be used withimportDocumentNodeExternallyFromstring: documents are generated asTypedDocumentStringinstances, aStringsubclass that carries result and variables types. The class is generated into the output.plainString: documents are generated as plain template strings, with fragments embedded. No class, import or type cast is generated, so the documents are typed asstring.
Note that some plugins (like typescript-graphql-request) also supports string for this parameter.
Default value: “graphQLTag”
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”
inlineReferencedFragmentsInFragmentDocuments
type: boolean
If you are using documentMode: graphQLTag, you can set this to true to make each fragment document include the fragments that it spreads.
This is useful if you want to use a fragment document on its own, e.g. to read or write a fragment in a client cache.
The fragment document can then contain more than one fragment definition.
Note that the same fragment can then appear more than once in the source of a document.
Use this option only if your gql tag (check gqlImport) keeps one definition for each fragment name, like graphql-tag does.
Default value: “false”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
documentMode: 'graphQLTag',
inlineReferencedFragmentsInFragmentDocuments: true,
},
},
},
};
export default config;
importOperationTypesFrom
type: string
This config is used internally by presets, but you can use it manually to tell codegen to prefix all base types that it’s using.
This is useful if you wish to generate base types from typescript-operations plugin into a different file, and import it from there.
Default value: ""
importDocumentNodeExternallyFrom
type: string
This config should be used if documentMode is external. This has 2 usage:
- any string: This would be the path to import document nodes from. This can be used if we want to manually create the document nodes e.g. Use
graphql-tagin a separate file and export the generated document - ‘near-operation-file’: This is a special mode that is intended to be used with
near-operation-filepreset to import document nodes from those files. If these files are.graphqlfiles, we make use of webpack loader. Default value: ""
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
documentMode: 'external',
importDocumentNodeExternallyFrom: 'path/to/document-node-file',
},
},
},
};
export default config;
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
documentMode: 'external',
importDocumentNodeExternallyFrom: 'near-operation-file',
},
},
},
};
export default config;
pureMagicComment
type: boolean
This config adds PURE magic comment to the static variables to enforce treeshaking for your bundler. Default value: “false”
experimentalFragmentVariables
type: boolean
If set to true, it will enable support for parsing variables on fragments. Default value: “false”
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”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
strictScalars: true,
},
},
},
};
export default config;
defaultScalarType
type: string
Allows you to override the type that unknown scalars will have. Default value: “unknown”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultScalarType: 'any'
},
},
},
};
export default config;
scalars
type: object
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
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”
typesPrefix
type: string
Prefixes all the generated types. Default value: ""
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
typesPrefix: 'I',
},
},
},
};
export default config;
typesSuffix
type: string
Suffixes all the generated types. Default value: ""
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
typesSuffix: 'I',
},
},
},
};
export default config;
enumPrefix
type: boolean
Allow you to disable prefixing for generated enums, works in combination with typesPrefix.
Default value: “true”
Usage Examples
Disable enum prefixes
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
typesPrefix: 'I',
enumPrefix: false
},
},
},
};
export default config;
enumSuffix
type: boolean
Allow you to disable suffixing for generated enums, works in combination with typesSuffix.
Default value: “true”
Usage Examples
Disable enum suffixes
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
typesSuffix: 'I',
enumSuffix: false
},
},
},
};
export default config;
useTypeImports
type: boolean
Will use import type {} rather than import {} when importing only types. This gives
compatibility with TypeScript’s “importsNotUsedAsValues”: “error” option
Default value: “false”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
useTypeImports: true
},
},
},
};
export default config;
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”
importExtension
type: object
Append this extension to all imports. Useful for ESM environments that require file extensions in import statements.
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”
Usage Example
For the given input:
It generates SDK you can import and wrap your GraphQLClient instance, and get fully-typed SDK
based on your operations:
Simple Request Middleware
The generated sdk accepts an optional middleware function to wrap the requests the client makes.
This can enable scenarios like request failure retries and logging at the sdk level.
To use middleware, just pass an SdkFunctionWrapper as the second argument to getSdk.
Examples of Middleware
- This example shows a naive request timing logger.
- This example uses polly-js to define a failure retry wrapper. (this is the use case for which I originally intended to add this capability)