visitor

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package visitor ports the shared pieces of @graphql-codegen/visitor-plugin-common: naming conventions, scalar resolution, and the DeclarationBlock code writer. It is the substrate the typescript plugins build on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSuffix added in v0.4.1

func AddSuffix(element, suffix string) string

AddSuffix mirrors parseMapper's addSuffix: appends the suffix to a type name, preserving any generic arguments.

func CamelCase

func CamelCase(s string) string

CamelCase: fooBar

func CapitalCase

func CapitalCase(s string) string

CapitalCase: Foo Bar

func ConstantCase

func ConstantCase(s string) string

ConstantCase: FOO_BAR

func DirectiveOverrideType added in v0.4.1

func DirectiveOverrideType(mappings map[string]DirectiveMappingEntry, directives []*ast.Directive) string

DirectiveOverrideType mirrors _getDirectiveOverrideType: the LAST directive (reverse order) present in directiveArgumentAndInputFieldMappings wins, and yields `DirectiveArgumentAndInputFieldMappings['<name>']`. Returns "" when no applicable directive is present.

func Indent

func Indent(str string, count int) string

Indent prepends count levels (2 spaces each) to a single line (exported).

func InputScalars

func InputScalars(schema *ast.Schema, cfg map[string]any) map[string]ScalarMapping

InputScalars mirrors buildScalarsFromConfig with DEFAULT_INPUT_SCALARS: ID's input type is "string | number" (array input coercion), used by the operations plugin for variables and input fields.

func LowerCase

func LowerCase(s string) string

LowerCase: foobar

func LowerCaseFirst

func LowerCaseFirst(s string) string

LowerCaseFirst: foo Bar

func OrderedTypeNames

func OrderedTypeNames(schema *ast.Schema) []string

OrderedTypeNames returns schema type names in stable (alphabetical) order, excluding built-in introspection types (prefixed __).

func ParseEnumValues added in v0.2.0

func ParseEnumValues(cfg map[string]any, convertEnumName func(string) string) map[string]EnumValuesEntry

ParseEnumValues ports parseEnumValues for the typescript plugin: string forms (internal type or external source#Type) and object forms (explicit value mapping).

func ParseMapperString added in v0.4.1

func ParseMapperString(raw, gqlTypeName, suffix string, useTypeImports bool) (string, string)

ParseMapperString ports upstream's parseMapper for STRING mapper values (the object form crashes upstream 6.x with "value.includes is not a function", so only strings are supported): a plain type (no '#') is used verbatim (parseMapper does not apply the suffix to internal types); an external form ("source#Identifier", "source#Identifier as Alias", "source#default", "source#default as Alias", or namespace "source#NS#Identifier") yields the mapped type plus its import statement. Returns the type string and the import statement ("" for internal types).

func PascalCase

func PascalCase(s string) string

PascalCase: FooBar

func SnakeCase

func SnakeCase(s string) string

SnakeCase: foo_bar

func UpperCase

func UpperCase(s string) string

UpperCase: FOOBAR

func UpperCaseFirst

func UpperCaseFirst(s string) string

UpperCaseFirst: Foo bar

Types

type BaseTypesVisitor

type BaseTypesVisitor struct {
	Schema *ast.Schema
	Config *ParsedConfig
	// contains filtered or unexported fields
}

BaseTypesVisitor renders a schema's type definitions to TypeScript.

func NewBaseTypesVisitor

func NewBaseTypesVisitor(schema *ast.Schema, cfg *ParsedConfig) *BaseTypesVisitor

NewBaseTypesVisitor builds a visitor for a schema + config.

func (*BaseTypesVisitor) DirectiveMappingsDefinition added in v0.4.1

func (v *BaseTypesVisitor) DirectiveMappingsDefinition() string

DirectiveMappingsDefinition renders the `DirectiveArgumentAndInputFieldMappings` type block (entries sorted by name — fixture configs define mappings in alphabetical order to match). Returns "" when no mappings are configured.

func (*BaseTypesVisitor) DirectiveMappingsImports added in v0.4.1

func (v *BaseTypesVisitor) DirectiveMappingsImports() []string

DirectiveMappingsImports returns the prepend import statements for external directive mappings, in name order.

func (*BaseTypesVisitor) EnumImports added in v0.2.0

func (v *BaseTypesVisitor) EnumImports() []string

EnumImports renders the import statements for external enumValues enums (mirrors getEnumsImports + buildTypeImport).

func (*BaseTypesVisitor) RenderArgs

func (v *BaseTypesVisitor) RenderArgs(def *ast.Definition) []string

RenderArgs renders argument-object types for fields with arguments.

func (*BaseTypesVisitor) RenderEnum

func (v *BaseTypesVisitor) RenderEnum(def *ast.Definition) string

RenderEnum renders an enum definition (native, const, or string-literal union).

func (*BaseTypesVisitor) RenderInputObject

func (v *BaseTypesVisitor) RenderInputObject(def *ast.Definition) string

RenderInputObject renders an input object type definition.

func (*BaseTypesVisitor) RenderInterface

func (v *BaseTypesVisitor) RenderInterface(def *ast.Definition) string

RenderInterface renders an interface type definition.

func (*BaseTypesVisitor) RenderObject

func (v *BaseTypesVisitor) RenderObject(def *ast.Definition) string

RenderObject renders an object type definition (plus its Args types).

func (*BaseTypesVisitor) RenderScalars

func (v *BaseTypesVisitor) RenderScalars() string

RenderScalars emits the Scalars map type.

func (*BaseTypesVisitor) RenderUnion

func (v *BaseTypesVisitor) RenderUnion(def *ast.Definition) string

RenderUnion renders a union type definition.

func (*BaseTypesVisitor) WrapperDefinitions

func (v *BaseTypesVisitor) WrapperDefinitions() []string

WrapperDefinitions returns the Maybe/InputMaybe type aliases (plus the FieldWrapper/EntireFieldWrapper aliases when the corresponding wrap flags are set, mirroring getWrapperDefinitions).

type ConvertFn

type ConvertFn func(name string, transformUnderscore bool) string

ConvertFn converts a GraphQL name to a TypeScript name per a naming convention.

type DeclarationBlock

type DeclarationBlock struct {
	// contains filtered or unexported fields
}

DeclarationBlock mirrors the DeclarationBlock class in utils.ts: a fluent builder for TS declarations (type/interface/enum/etc.).

func NewDeclarationBlock

func NewDeclarationBlock() *DeclarationBlock

NewDeclarationBlock creates a block with default config.

func (*DeclarationBlock) AsKind

func (d *DeclarationBlock) AsKind(kind string) *DeclarationBlock

func (*DeclarationBlock) Export

func (d *DeclarationBlock) Export() *DeclarationBlock

func (*DeclarationBlock) IgnoreExport

func (d *DeclarationBlock) IgnoreExport(v bool) *DeclarationBlock

IgnoreExport controls whether Export() emits the "export" keyword.

func (*DeclarationBlock) String

func (d *DeclarationBlock) String() string

func (*DeclarationBlock) WithBlock

func (d *DeclarationBlock) WithBlock(block string) *DeclarationBlock

func (*DeclarationBlock) WithBlockTransformer added in v0.2.0

func (d *DeclarationBlock) WithBlockTransformer(fn func(string) string) *DeclarationBlock

WithBlockTransformer wraps the rendered `{ ... }` block before it is appended (mirrors DeclarationBlockConfig.blockTransformer).

func (*DeclarationBlock) WithComment

func (d *DeclarationBlock) WithComment(comment string) *DeclarationBlock

func (*DeclarationBlock) WithContent

func (d *DeclarationBlock) WithContent(content string) *DeclarationBlock

func (*DeclarationBlock) WithDecorator

func (d *DeclarationBlock) WithDecorator(decorator string) *DeclarationBlock

func (*DeclarationBlock) WithGenerics

func (d *DeclarationBlock) WithGenerics(generics string) *DeclarationBlock

WithGenerics sets type parameters rendered as `<generics>` after the name.

func (*DeclarationBlock) WithName

func (d *DeclarationBlock) WithName(name string) *DeclarationBlock

type DirectiveMappingEntry added in v0.4.1

type DirectiveMappingEntry struct {
	TypeStr    string
	ImportStmt string
}

DirectiveMappingEntry is one parsed directiveArgumentAndInputFieldMappings entry (mirrors ParsedMapper): the mapped type plus an optional import statement for external ("source#Type") mappings.

type EnumValuesEntry added in v0.2.0

type EnumValuesEntry struct {
	SourceFile              string
	SourceIdentifier        string
	TypeIdentifierConverted string
	ImportIdentifier        string
	IsDefault               bool
	MappedValues            map[string]any
}

EnumValuesEntry mirrors parseEnumValues' parsed entry.

type ParsedConfig

type ParsedConfig struct {
	Scalars                   map[string]ScalarMapping
	ScalarOrder               []string
	Convert                   ConvertFn
	EnumConvert               ConvertFn // may differ if namingConvention.enumValues is set
	TypesPrefix               string
	TypesSuffix               string
	EnumPrefix                bool
	EnumSuffix                bool
	UseTypeImports            bool
	ImmutableTypes            bool
	AllowEnumStringTypes      bool
	EmitLegacyCommonJSImports bool
	ImportExtension           string
	PrintFieldsOnNewLines     bool
	NamespacedImportName      string

	// types-plugin flags
	AvoidOptionalsField        bool
	AvoidOptionalsInputValue   bool
	AvoidOptionalsDefaultValue bool
	AvoidOptionalsResolvers    bool
	AvoidOptionalsQuery        bool
	AvoidOptionalsMutation     bool
	AvoidOptionalsSubscription bool
	AddUnderscoreToArgsType    bool
	EnumValues                 map[string]EnumValuesEntry
	ConstEnums                 bool
	EnumsAsTypes               bool
	FutureProofEnums           bool
	FutureProofUnions          bool
	EnumsAsConst               bool
	NumericEnums               bool
	OnlyEnums                  bool
	OnlyOperationTypes         bool
	MaybeValue                 string
	InputMaybeValue            string
	NoExport                   bool
	UseImplementingTypes       bool
	SkipTypename               bool
	NonOptionalTypename        bool
	DeclarationKind            map[string]string

	// directiveArgumentAndInputFieldMappings (typescript + operations): maps
	// a directive name to the TS type that replaces the type of any field,
	// argument, or variable carrying that directive.
	DirectiveArgumentAndInputFieldMappings map[string]DirectiveMappingEntry

	// wrapFieldDefinitions / wrapEntireFieldDefinitions (typescript plugin):
	// wrap output-side field types in FieldWrapper<T> / EntireFieldWrapper<T>.
	WrapFieldDefinitions    bool
	FieldWrapperValue       string
	WrapEntireDefinitions   bool
	EntireFieldWrapperValue string
}

ParsedConfig holds the parsed (defaulted) plugin config, mirroring the ParsedConfig / ParsedTypesConfig structures in visitor-plugin-common.

func ParseConfig

func ParseConfig(schema *ast.Schema, cfg map[string]any) (*ParsedConfig, error)

ParseConfig builds a ParsedConfig from a raw config map, defaulting everything.

type ScalarMapping

type ScalarMapping struct {
	Input  string
	Output string
}

ScalarMapping is a scalar's input/output TS type (ParsedScalarsMap entry).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL