The TypeScriptResolversVisitor class handles AST traversal and generates TypeScript code with proper type safety and customization. It extends the base visitor pattern used throughout GraphQL Code Generator.
Advanced visitor class that processes GraphQL schema AST nodes and generates TypeScript resolver signatures.
/**
* Visitor class for generating TypeScript resolver signatures
* Extends BaseResolversVisitor with TypeScript-specific behavior
*/
class TypeScriptResolversVisitor extends BaseResolversVisitor<
TypeScriptResolversPluginConfig,
ParsedTypeScriptResolversConfig
> {
constructor(pluginConfig: TypeScriptResolversPluginConfig, schema: GraphQLSchema);
transformParentGenericType(parentType: string): string;
formatRootResolver(schemaTypeName: string, resolverType: string, declarationKind: DeclarationKind): string;
ListType(node: ListTypeNode): string;
NamedType(node: NamedTypeNode): string;
NonNullType(node: NonNullTypeNode): string;
buildEnumResolverContentBlock(node: EnumTypeDefinitionNode, mappedEnumType: string): string;
buildEnumResolversExplicitMappedValues(node: EnumTypeDefinitionNode, valuesMapping: Record<string, string | number>): string;
}Internal configuration interface used by the visitor with processed and validated options.
/**
* Parsed configuration used internally by the visitor
* Contains processed versions of plugin configuration options
*/
interface ParsedTypeScriptResolversConfig extends ParsedResolversConfig {
useIndexSignature: boolean;
wrapFieldDefinitions: boolean;
allowParentTypeOverride: boolean;
optionalInfoArgument: boolean;
}Creates a new visitor instance with configuration and schema.
/**
* Creates a new TypeScriptResolversVisitor instance
* @param pluginConfig - Plugin configuration options
* @param schema - GraphQL schema to process
*/
constructor(pluginConfig: TypeScriptResolversPluginConfig, schema: GraphQLSchema);Usage Example:
import { buildSchema } from 'graphql';
import { TypeScriptResolversVisitor } from '@graphql-codegen/typescript-resolvers';
const schema = buildSchema(`
type Query {
user(id: ID!): User
}
type User {
id: ID!
name: String!
}
`);
const visitor = new TypeScriptResolversVisitor({
contextType: './context#Context',
useIndexSignature: true
}, schema);Transforms parent type generics based on configuration.
/**
* Transforms parent type generic constraints
* @param parentType - Parent type name to transform
* @returns Transformed generic type constraint
*/
transformParentGenericType(parentType: string): string;Behavior:
// With allowParentTypeOverride: false (default)
transformParentGenericType('User')
// Returns: "ParentType extends User = User"
// With allowParentTypeOverride: true
transformParentGenericType('User')
// Returns: "ParentType = User"Formats root resolver type declarations with proper optional/required handling.
/**
* Formats root resolver type declarations
* @param schemaTypeName - Name of the GraphQL schema type
* @param resolverType - Generated resolver type name
* @param declarationKind - Kind of declaration being formatted
* @returns Formatted resolver declaration
*/
formatRootResolver(
schemaTypeName: string,
resolverType: string,
declarationKind: DeclarationKind
): string;Usage Example:
// With avoidOptionals.resolvers: false (default)
formatRootResolver('Query', 'QueryResolvers<ContextType>', DeclarationKind.type)
// Returns: "Query?: QueryResolvers<ContextType>;"
// With avoidOptionals.resolvers: true
formatRootResolver('Query', 'QueryResolvers<ContextType>', DeclarationKind.type)
// Returns: "Query: QueryResolvers<ContextType>;"Handles different GraphQL type nodes and converts them to TypeScript types.
/**
* Handles GraphQL ListType nodes
* @param node - ListType AST node
* @returns TypeScript type string for list types
*/
ListType(node: ListTypeNode): string;
/**
* Handles GraphQL NamedType nodes
* @param node - NamedType AST node
* @returns TypeScript type string for named types
*/
NamedType(node: NamedTypeNode): string;
/**
* Handles GraphQL NonNullType nodes
* @param node - NonNullType AST node
* @returns TypeScript type string for non-null types
*/
NonNullType(node: NonNullTypeNode): string;Type Transformation Examples:
// ListType handling
// GraphQL: [String!]!
// TypeScript: Array<ResolversTypes['String']>
// NamedType handling
// GraphQL: User
// TypeScript: Maybe<ResolversTypes['User']>
// NonNullType handling
// GraphQL: String!
// TypeScript: ResolversTypes['String']Specialized methods for generating enum resolver types.
/**
* Builds enum resolver content block with proper type signatures
* @param node - EnumTypeDefinition AST node
* @param mappedEnumType - Mapped enum type name
* @returns Generated enum resolver signature
*/
buildEnumResolverContentBlock(
node: EnumTypeDefinitionNode,
mappedEnumType: string
): string;
/**
* Builds enum resolvers with explicit value mappings
* @param node - EnumTypeDefinition AST node
* @param valuesMapping - Explicit value mappings for enum values
* @returns Generated enum resolver with explicit mappings
*/
buildEnumResolversExplicitMappedValues(
node: EnumTypeDefinitionNode,
valuesMapping: Record<string, string | number>
): string;Enum Resolver Examples:
// GraphQL enum definition
enum Status {
ACTIVE
INACTIVE
PENDING
}
// Generated enum resolver (buildEnumResolverContentBlock)
export type StatusResolvers = EnumResolverSignature<
{ ACTIVE?: any, INACTIVE?: any, PENDING?: any },
ResolversTypes['Status']
>;
// With explicit mappings (buildEnumResolversExplicitMappedValues)
export type StatusResolvers = {
ACTIVE: 'active',
INACTIVE: 'inactive',
PENDING: 'pending'
};Constant defining the TypeScript signature template for enum resolvers.
/**
* TypeScript signature template for enum resolvers
* Used in generated enum resolver types
*/
const ENUM_RESOLVERS_SIGNATURE: string =
'export type EnumResolverSignature<T, AllowedValues = any> = { [key in keyof T]?: AllowedValues };';The visitor handles complex type scenarios:
Federation Support:
// Federation reference resolver parent type
getParentTypeForSignature(node: FieldDefinitionNode): string;
// Returns: 'UnwrappedObject<ParentType>' for reference resolvers
// Returns: 'ParentType' for regular resolversImmutable Types:
// With immutableTypes: true
wrapWithListType(str: string): string;
// Returns: `ReadonlyArray<${str}>`
// With immutableTypes: false (default)
wrapWithListType(str: string): string;
// Returns: `Array<${str}>`Optional Type Clearing:
// Internal utility for handling Maybe types
clearOptional(str: string): string;
// Input: 'Maybe<User>'
// Output: 'User'The TypeScriptResolversVisitor extends BaseResolversVisitor and inherits core functionality:
Complete Usage Example:
import { buildSchema } from 'graphql';
import { getCachedDocumentNodeFromSchema, oldVisit } from '@graphql-codegen/plugin-helpers';
import { TypeScriptResolversVisitor } from '@graphql-codegen/typescript-resolvers';
const schema = buildSchema(`
type Query {
users: [User!]!
user(id: ID!): User
}
type User {
id: ID!
name: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
author: User!
}
enum Status {
PUBLISHED
DRAFT
}
`);
const visitor = new TypeScriptResolversVisitor({
contextType: './context#Context',
mappers: {
User: './models#UserModel',
Post: './models#PostModel'
},
useIndexSignature: false,
optionalInfoArgument: false
}, schema);
const astNode = getCachedDocumentNodeFromSchema(schema);
const result = oldVisit(astNode, { leave: visitor });
// Result contains generated resolver type definitions