or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdindex.mdtype-generation.mdutility-types.mdvisitors.md
tile.json

visitors.mddocs/

Visitor Classes

Advanced visitor classes for traversing GraphQL AST and generating TypeScript code. The plugin uses the visitor pattern to process GraphQL schema elements and convert them to TypeScript types.

Capabilities

TsVisitor Class

Main visitor class for generating TypeScript types from GraphQL schema elements.

/**
 * Main visitor class for generating TypeScript types
 * Generic class supporting custom configuration types
 */
class TsVisitor<
  TRawConfig extends TypeScriptPluginConfig = TypeScriptPluginConfig,
  TParsedConfig extends TypeScriptPluginParsedConfig = TypeScriptPluginParsedConfig
> extends BaseTypesVisitor<TRawConfig, TParsedConfig> {
  
  /**
   * Creates a new TsVisitor instance
   * @param schema - The GraphQL schema to process
   * @param pluginConfig - Plugin configuration options
   * @param additionalConfig - Additional parsed configuration options
   */
  constructor(
    schema: GraphQLSchema,
    pluginConfig: TRawConfig,
    additionalConfig?: Partial<TParsedConfig>
  );
}

Usage Examples:

import { TsVisitor } from "@graphql-codegen/typescript";
import { buildSchema } from "graphql";

const schema = buildSchema(`
  type User {
    id: ID!
    name: String!
  }
`);

const visitor = new TsVisitor(schema, {
  avoidOptionals: true,
  immutableTypes: true
});

TsVisitor Methods

Core methods for generating type definitions and handling different GraphQL constructs.

class TsVisitor {
  /** Returns array of type wrapper definitions */
  getWrapperDefinitions(): string[];
  
  /** Returns the Exact type definition */
  getExactDefinition(): string;
  
  /** Returns the MakeOptional type definition */
  getMakeOptionalDefinition(): string;
  
  /** Returns the MakeMaybe type definition */
  getMakeMaybeDefinition(): string;
  
  /** Returns the MakeEmpty type definition */
  getMakeEmptyDefinition(): string;
  
  /** Returns the Incremental type definition */
  getIncrementalDefinition(): string;
  
  /** Returns the Maybe type definition */
  getMaybeValue(): string;
  
  /** Returns the InputMaybe type definition */
  getInputMaybeValue(): string;
  
  /** Returns appropriate Maybe wrapper based on context */
  getMaybeWrapper(ancestors: any): string;
}

AST Node Handlers

Methods for processing specific GraphQL AST node types.

class TsVisitor {
  /**
   * Handles named type nodes in AST
   * @param node - The named type node
   * @param key - Node key in parent
   * @param parent - Parent AST node
   * @param path - AST path
   * @param ancestors - AST ancestor nodes
   * @returns Generated TypeScript type string
   */
  NamedType(
    node: NamedTypeNode,
    key: string | number,
    parent: any,
    path: any,
    ancestors: any
  ): string;

  /**
   * Handles list type nodes in AST
   * @param node - The list type node
   * @param key - Node key in parent
   * @param parent - Parent AST node
   * @param path - AST path
   * @param ancestors - AST ancestor nodes
   * @returns Generated TypeScript array type string
   */
  ListType(
    node: ListTypeNode,
    key: string | number,
    parent: any,
    path: any,
    ancestors: any
  ): string;

  /**
   * Handles non-null type nodes
   * @param node - The non-null type node
   * @returns Generated TypeScript type string without null wrapper
   */
  NonNullType(node: NonNullTypeNode): string;

  /**
   * Generates TypeScript union type definitions
   * @param node - The union type definition node
   * @param key - Node key in parent
   * @param parent - Parent AST node
   * @returns Generated TypeScript union type string
   */
  UnionTypeDefinition(
    node: UnionTypeDefinitionNode,
    key?: string | number,
    parent?: any
  ): string;

  /**
   * Generates field definitions for object types
   * @param node - The field definition node
   * @param key - Node key in parent
   * @param parent - Parent AST node
   * @returns Generated TypeScript field definition string
   */
  FieldDefinition(
    node: FieldDefinitionNode,
    key?: number | string,
    parent?: any
  ): string;

  /**
   * Generates input value definitions
   * @param node - The input value definition node
   * @param key - Node key in parent
   * @param parent - Parent AST node
   * @param _path - AST path (unused)
   * @param ancestors - AST ancestor nodes
   * @returns Generated TypeScript input field definition string
   */
  InputValueDefinition(
    node: InputValueDefinitionNode,
    key?: number | string,
    parent?: any,
    _path?: Array<string | number>,
    ancestors?: Array<TypeDefinitionNode>
  ): string;

  /**
   * Generates TypeScript enum definitions
   * @param node - The enum type definition node
   * @returns Generated TypeScript enum definition string
   */
  EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
}

TsIntrospectionVisitor Class

Specialized visitor for handling GraphQL introspection types with selective type inclusion.

/**
 * Specialized visitor for handling introspection types
 * Extends TsVisitor with selective type inclusion
 */
class TsIntrospectionVisitor extends TsVisitor {
  /**
   * Creates a new TsIntrospectionVisitor instance
   * @param schema - The GraphQL schema
   * @param pluginConfig - Plugin configuration options
   * @param typesToInclude - Array of GraphQL types to include in generation
   */
  constructor(
    schema: GraphQLSchema,
    pluginConfig?: TypeScriptPluginConfig,
    typesToInclude: GraphQLNamedType[]
  );

  /**
   * Skips directive definitions (returns null)
   * @returns null - Directives are not processed
   */
  DirectiveDefinition(): null;

  /**
   * Generates object type definitions only for included types
   * @param node - The object type definition node
   * @param key - Node key in parent
   * @param parent - Parent AST node
   * @returns Generated TypeScript type string or null if not included
   */
  ObjectTypeDefinition(
    node: ObjectTypeDefinitionNode,
    key: string | number,
    parent: any
  ): string | null;

  /**
   * Generates enum definitions only for included types
   * @param node - The enum type definition node
   * @returns Generated TypeScript enum string or null if not included
   */
  EnumTypeDefinition(node: EnumTypeDefinitionNode): string | null;
}

Usage Examples:

import { TsIntrospectionVisitor } from "@graphql-codegen/typescript";
import { buildSchema, getIntrospectionQuery, parse } from "graphql";

const schema = buildSchema(`
  type Query {
    __schema: __Schema
  }
`);

// Types to include in introspection generation
const typesToInclude = [
  schema.getType('__Schema'),
  schema.getType('__Type')
].filter(Boolean);

const visitor = new TsIntrospectionVisitor(
  schema,
  { avoidOptionals: false },
  typesToInclude
);

TypeScriptOperationVariablesToObject Class

Handles conversion of GraphQL operation variables to TypeScript object types.

/**
 * Handles conversion of operation variables to TypeScript objects
 * Extends OperationVariablesToObject with TypeScript-specific behavior
 */
class TypeScriptOperationVariablesToObject extends OperationVariablesToObject {
  /**
   * Creates a new TypeScriptOperationVariablesToObject instance
   * @param _scalars - Scalar type mappings
   * @param _convertName - Name conversion function
   * @param _avoidOptionals - Optional handling configuration
   * @param _immutableTypes - Whether to generate readonly types
   * @param _namespacedImportName - Namespaced import name
   * @param _enumNames - Array of enum names
   * @param _enumPrefix - Whether to use enum prefix
   * @param _enumSuffix - Whether to use enum suffix
   * @param _enumValues - Enum value mappings
   * @param _applyCoercion - Whether to apply type coercion
   * @param _directiveArgumentAndInputFieldMappings - Directive mappings
   * @param _maybeType - Maybe type name
   */
  constructor(
    _scalars: NormalizedScalarsMap,
    _convertName: ConvertNameFn,
    _avoidOptionals: NormalizedAvoidOptionalsConfig,
    _immutableTypes: boolean,
    _namespacedImportName?: string | null,
    _enumNames?: string[],
    _enumPrefix?: boolean,
    _enumSuffix?: boolean,
    _enumValues?: ParsedEnumValuesMap,
    _applyCoercion?: boolean,
    _directiveArgumentAndInputFieldMappings?: ParsedDirectiveArgumentAndInputFieldMappings,
    _maybeType?: string
  );

  /**
   * Wraps AST types with appropriate TypeScript modifiers
   * @param baseType - Base TypeScript type string
   * @param typeNode - GraphQL type node from AST
   * @param applyCoercion - Whether to apply type coercion
   * @returns TypeScript type string with proper modifiers
   */
  wrapAstTypeWithModifiers(
    baseType: string,
    typeNode: TypeNode,
    applyCoercion?: boolean
  ): string;
}

TypeScriptPluginParsedConfig Interface

Internal parsed configuration interface used by visitor classes.

/**
 * Parsed configuration interface used internally by visitor classes
 * Contains normalized and processed configuration values
 */
interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
  avoidOptionals: NormalizedAvoidOptionalsConfig;
  constEnums: boolean;
  enumsAsTypes: boolean;
  futureProofEnums: boolean;
  futureProofUnions: boolean;
  enumsAsConst: boolean;
  numericEnums: boolean;
  onlyEnums: boolean;
  onlyOperationTypes: boolean;
  immutableTypes: boolean;
  maybeValue: string;
  inputMaybeValue: string;
  noExport: boolean;
  useImplementingTypes: boolean;
}