TypeScript Resolvers
Generate TypeScript resolvers types. Works with graphql-tools and apollo-server. Supports custom mappers and enum resolvers.
- Version
- 7.0.1
- Weekly downloads
- 2.0M
- License
- MIT
- Updated
- Nov 12, 2026
Installation
npm i -D @graphql-codegen/typescript-resolvers
yarn add -D @graphql-codegen/typescript-resolvers
pnpm add -D @graphql-codegen/typescript-resolvers
bun add -D @graphql-codegen/typescript-resolvers
Config API Reference
useIndexSignature
type: boolean
Adds an index signature to any generates resolver. Default value: “false”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
useIndexSignature: true
},
},
},
};
export default config;
noSchemaStitching
type: boolean
Disables/Enables Schema Stitching support.
By default, the resolver signature does not include the support for schema-stitching.
Set to false to enable that.
Default value: “true”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
noSchemaStitching: false
},
},
},
};
export default config;
wrapFieldDefinitions
type: boolean
Set to true in order to wrap field definitions with FieldWrapper.
This is useful to allow return types such as Promises and functions. Needed for
compatibility with federation: true when
Default value: “true”
customResolveInfo
type: string
You can provide your custom GraphQLResolveInfo instead of the default one from graphql-js Default value: “graphql#GraphQLResolveInfo”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
customResolveInfo: './my-types#MyResolveInfo'
},
},
},
};
export default config;
customResolverFn
type: string
You can provide your custom ResolveFn instead the default. It has to be a type that uses the generics <TResult, TParent, TContext, TArgs>
Default value: “(parent: TParent, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Promise
Usage Examples
Custom Signature
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
customResolverFn: './my-types#MyResolveFn'
},
},
},
};
export default config;
With Graphile
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
customResolverFn: './my-types#MyResolveFn'
},
},
},
};
export default config;
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
"path/to/file.ts": {
"plugins": [
{
"add": {
"content": "import { GraphileHelpers } from 'graphile-utils/node8plus/fieldHelpers';"
}
},
"typescript",
"typescript-resolvers"
],
"config": {
"customResolverFn": "(\n parent: TParent,\n args: TArgs,\n context: TContext,\n info: GraphQLResolveInfo & { graphile: GraphileHelpers<TParent> }\n) => Promise<TResult> | TResult;\n"
}
}
}
};
export default config;
directiveResolverMappings
type: object
Map the usage of a directive into using a specific resolver.
allowParentTypeOverride
type: boolean
Allow you to override the ParentType generic in each resolver, by avoid enforcing the base type of the generated generic type.
This will generate ParentType = Type instead of ParentType extends Type = Type in each resolver.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
allowParentTypeOverride: true
},
},
},
};
export default config;
optionalInfoArgument
type: boolean
Sets info argument of resolver function to be optional field. Useful for testing.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
optionalInfoArgument: true
},
},
},
};
export default config;
makeResolverTypeCallable
type: boolean
Set to true in order to allow the Resolver type to be callable
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
makeResolverTypeCallable: true
},
},
},
};
export default config;
addUnderscoreToArgsType
type: boolean
Adds _ to generated Args types in order to avoid duplicate identifiers.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
addUnderscoreToArgsType: true
},
},
},
};
export default config;
contextType
type: string
Use this configuration to set a custom type for your context, and it will
affect all the resolvers, without the need to override it using generics each time.
If you wish to use an external type and import it from another file, you can use add plugin
and add the required import statement, or you can use a module#type syntax.
Usage Examples
Custom Context Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
contextType: 'MyContext'
},
},
},
};
export default config;
Custom Context Type by Path
Note that the path should be relative to the generated file.
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
contextType: './my-types#MyContext'
},
},
},
};
export default config;
fieldContextTypes
type: object
Use this to set a custom type for a specific field context.
It will only affect the targeted resolvers.
You can either use Field.Path#ContextTypeName or Field.Path#ExternalFileName#ContextTypeName
Usage Examples
Custom Field Context Types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
fieldContextTypes: ['MyType.foo#CustomContextType', 'MyType.bar#./my-file#ContextTypeOne']
},
},
},
};
export default config;
rootValueType
type: string
Use this configuration to set a custom type for the rootValue, and it will
affect resolvers of all root types (Query, Mutation and Subscription), without the need to override it using generics each time.
If you wish to use an external type and import it from another file, you can use add plugin
and add the required import statement, or you can use both module#type or module#namespace#type syntax.
Usage Examples
Custom RootValue Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
rootValueType: 'MyRootValue'
},
},
},
};
export default config;
Custom RootValue Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
rootValueType: './my-types#MyRootValue'
},
},
},
};
export default config;
directiveContextTypes
type: object
Use this to set a custom type for a specific field context decorated by a directive.
It will only affect the targeted resolvers.
You can either use Field.Path#ContextTypeName or Field.Path#ExternalFileName#ContextTypeName
ContextTypeName should by a generic Type that take the context or field context type as only type parameter.
Usage Examples
Directive Context Extender
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
directiveContextTypes: ['myCustomDirectiveName#./my-file#CustomContextExtender']
},
},
},
};
export default config;
mapperTypeSuffix
type: string
Adds a suffix to the imported names to prevent name clashes.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
mapperTypeSuffix: 'Model'
},
},
},
};
export default config;
mappers
type: object
Replaces a GraphQL type usage with a custom type, allowing you to return custom object from
your resolvers.
You can use both module#type and module#namespace#type syntax.
Usage Examples
Custom Context Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
mappers: {
User: './my-models#UserDbObject',
Book: './my-models#Collections',
}
},
},
},
};
export default config;
defaultMapper
type: string
Allow you to set the default mapper when it’s not being override by mappers or generics.
You can specify a type name, or specify a string in module#type or module#namespace#type format.
The default value of mappers is the TypeScript type generated by typescript package.
Usage Examples
Replace with any
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultMapper: 'any',
},
},
},
};
export default config;
Custom Base Object
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultMapper: './my-file#BaseObject',
},
},
},
};
export default config;
Wrap default types with Partial
You can also specify a custom wrapper for the original type, without overriding the original generated types, use {T} to specify the identifier. (for flow, use $Shape<{T}>)
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultMapper: 'Partial<{T}>',
},
},
},
};
export default config;
Allow deep partial with utility-types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver', { add: { content: "import { DeepPartial } from 'utility-types';" } }],
config: {
defaultMapper: 'DeepPartial<{T}>',
avoidCheckingAbstractTypesRecursively: true // required if you have complex nested abstract types
},
},
},
};
export default config;
avoidOptionals
type: object
This will cause the generator to avoid using optionals (?),
so all field resolvers must be implemented in order to avoid compilation errors.
Default value: “false”
Usage Examples
Override all definition types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
avoidOptionals: true
},
},
},
};
export default config;
Override only specific definition types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
avoidOptionals: {
field: true,
inputValue: true,
object: true,
defaultValue: true,
query: true,
mutation: true,
subscription: true,
}
},
},
},
};
export default config;
showUnusedMappers
type: boolean
Warns about unused mappers. Default value: “true”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
showUnusedMappers: true,
},
},
},
};
export default config;
enumValues
type: object
Overrides the default value of enum values declared in your GraphQL schema, supported
in this plugin because of the need for integration with typescript package.
See documentation under typescript plugin for more information and examples.
resolverTypeWrapperSignature
type: string
Allow you to override resolverTypeWrapper definition.
Default value: “Promise
federation
type: boolean
Supports Apollo Federation Default value: “false”
customDirectives
type: object
Configures behavior for custom directives from various GraphQL libraries.
Usage Examples
@semanticNonNull
First, install graphql-sock peer dependency:
npm install -D graphql-sock
Now, you can enable support for @semanticNonNull directive:
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript-resolvers'],
config: {
customDirectives: {
semanticNonNull: true
}
},
},
},
};
export default config;
optionalResolveType
type: boolean
Sets the __resolveType field as optional field.
Default value: “false”
immutableTypes
type: boolean
Generates immutable types by adding readonly to properties and uses ReadonlyArray.
Default value: “false”
namespacedImportName
type: string
Prefixes all GraphQL related generated types with that value, as namespaces import. You can use this feature to allow separation of plugins to different files. Default value: ”””
resolverTypeSuffix
type: string
Suffix we add to each generated type resolver. Default value: “Resolvers”
allResolversTypeName
type: string
The type name to use when exporting all resolvers signature as unified type. Default value: “Resolvers”
internalResolversPrefix
type: string
Defines the prefix value used for __resolveType and __isTypeOf resolvers.
If you are using mercurius-js, please set this field to empty string for better compatibility.
Default value: ”’__’”
resolversNonOptionalTypename
type: object
Makes __typename of resolver mappings non-optional without affecting the base types.
Default value: “false”
Usage Examples
Enable for all
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
resolversNonOptionalTypename: true // or { unionMember: true, interfaceImplementingType: true }
},
},
},
};
export default config;
Enable except for some types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
resolversNonOptionalTypename: {
unionMember: true,
interfaceImplementingType: true,
excludeTypes: ['MyType'],
}
},
},
},
};
export default config;
avoidCheckingAbstractTypesRecursively
type: boolean
If true, recursively goes through all object type’s fields, checks if they have abstract types and generates expected types correctly.
This may not work for cases where provided default mapper types are also nested e.g. defaultMapper: DeepPartial<{T}> or defaultMapper: Partial<{T}>.
Default value: “false”
addInterfaceFieldResolverTypes
type: boolean
If true, add field resolver types to Interfaces. By default, GraphQL Interfaces do not trigger any field resolvers, meaning every implementing type must implement the same resolver for the shared fields.
Some tools provide a way to change the default behaviour by making GraphQL Objects inherit missing resolvers from their Interface types. In these cases, it is fine to turn this option to true.
For example, if you are using @graphql-tools/schema#makeExecutableSchema with inheritResolversFromInterfaces: true,
you can make addInterfaceFieldResolverTypes: true as well
https://the-guild.dev/graphql/tools/docs/generate-schema#makeexecutableschema
Default value: “false”
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
addInterfaceFieldResolverTypes: true,
},
},
},
};
export default config;
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
Run graphql-codegen as usual, with this new plugin:
Import the types from the generated file and use in the resolver:
This will make the resolver fully typed and compatible with typescript compiler, including the handler’s arguments and return value.
Generated resolvers can be passed directly into
graphql-tools makeExecutableSchema function.
Integration with Apollo-Server
By default apollo-server will not work with generated resolvers signature.
If you are using Apollo Server with TypeScript, note that you need to set useIndexSignature: true
in your config, in order to add a compatible index signature
(more info).
If you wish to have an easy start, and have the ability to use resolvers chaining without models
types, you can also add to your config defaultMapper: Partial<{T}>. This will allow you to return
partial types in your resolvers.
Use Your Model Types (mappers)
If you wish to use your custom model types, codegen allow you to use mappers feature to map
GraphQL types to your custom model types.
You can find an article explaining how to use mappers here.
Here’s the basic example of using it:
Enum Resolvers
Apollo-Server and schemas built with
graphql-tools supports creating resolvers for GraphQL enums.
This is helpful because you can have internal values that are different from the public enum values, and you can use the internal values in your resolvers.
Codegen allows you to specify either mappers or enumValues to map enums in your resolvers, and
if you are using it for enums, you’ll get a resolver signature for the enum resolvers as well.
Usage Example
With the following schema:
You can also define the same with explicit enum values:
Or, with mappers:
Defined shared mappers across configurations
In some case, you might want to share some common mappers between many output file configurations.
To do so, you can leverage the YAML references features as follow:
The above configuration will provide the ID type mapping to both resolvers-types-1.ts and
resolvers-types-2.ts files.
The complete example is available here: codegen-repros/reusable-mappers
You can also achieve a similar reusable mappers configuration by providing a TypeScript or JavaScript file configuration.