schema

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package schema turns an introspect.Catalog into a GraphQL schema.

The Builder holds a mutable intermediate representation (objects, inputs, enums + per-field metadata binding GraphQL fields back to catalog objects). SchemaHook plugins mutate the Builder; Build() then renders SDL, validates it with gqlparser, and returns the executable schema plus the metadata the SQL compiler needs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arg

type Arg struct {
	Name    string
	Type    string
	Default string // SDL literal, empty for none
}

type Builder

type Builder struct {
	Catalog *introspect.Catalog
	Inflect inflect.Next
	Options Options
	Objects map[string]*Object
	Inputs  map[string]*Input
	Enums   map[string]*Enum
	Scalars []string
	// Query/Mutation are the root objects (also present in Objects).
	Query    *Object
	Mutation *Object

	// TypeForTable maps "schema.table" to the generated object type name.
	TypeForTable map[string]string
	// TypeForComposite maps "schema.type" to the generated object type name.
	TypeForComposite map[string]string
	// InputForComposite maps "schema.type" to the generated input type name.
	InputForComposite map[string]string
	// contains filtered or unexported fields
}

Builder is the mutable schema IR handed to SchemaHook plugins.

func New

func New(cat *introspect.Catalog, inflector inflect.Next, opts Options) *Builder

New creates a Builder with all default types generated from the catalog.

func (*Builder) Build

func (b *Builder) Build() (*Built, error)

Build renders SDL, validates it, and returns the executable schema + meta.

func (*Builder) FilterOpsInputFor

func (b *Builder) FilterOpsInputFor(c *introspect.Column) string

FilterOpsInputFor returns (creating if needed) the operator input type for a column-shaped value — exported for SchemaHook plugins adding filterable fields (e.g. advanced-filters computed columns).

func (*Builder) SDL

func (b *Builder) SDL() string

SDL renders the current IR as GraphQL SDL, deterministically ordered.

type Built

type Built struct {
	Schema       *ast.Schema
	SDL          string
	Meta         Meta
	Catalog      *introspect.Catalog
	TypeForTable map[string]string
	// TableForType maps object type name -> catalog table (node() dispatch).
	TableForType map[string]*introspect.Table
	// OrderBy maps enum type name -> value name -> (column, desc).
	OrderBy map[string]map[string]OrderSpec
	// EnumValues maps enum type name -> GraphQL value -> pg value.
	EnumValues map[string]map[string]string
	// EnumTypeForPG maps "typeschema.typename" -> GraphQL enum type name.
	EnumTypeForPG map[string]string
	// CompositeTypeForPG maps "typeschema.typename" -> GraphQL object type name.
	CompositeTypeForPG map[string]string
	// CompositeInputForPG maps "typeschema.typename" -> GraphQL input type name.
	CompositeInputForPG map[string]string
	// InputColumns maps input type name -> field name -> column name.
	InputColumns map[string]map[string]string
	// FilterRelations maps filter input type name -> field name -> relation
	// binding (advanced-filters plugin; empty otherwise).
	FilterRelations map[string]map[string]*FilterRelation
	// FilterComputed maps filter input type name -> field name -> the
	// computed-column function backing it (advanced-filters plugin).
	FilterComputed map[string]map[string]*introspect.Function
}

Built is the result of Build: the validated executable schema plus everything the compiler needs.

type Enum

type Enum struct {
	Name        string
	Description string
	Values      []EnumValue
}

Enum is a GraphQL enum type.

type EnumValue

type EnumValue struct {
	Name        string
	Description string
	// Column/Desc back orderBy enum values.
	Column string
	Desc   bool
	// Computed backs orderBy enum values sorting by a computed column
	// (advanced-filters): a row-type function instead of a plain column.
	Computed *introspect.Function
	// PGValue backs catalog enum values.
	PGValue string
}

type Field

type Field struct {
	Name        string
	Type        string // SDL type reference, e.g. "String!", "[User!]!"
	Description string
	// Deprecated carries the @deprecated(reason:) directive when non-empty.
	Deprecated string
	Args       []Arg
	Meta       *FieldMeta
}

Field is one field on an object type.

type FieldKind

type FieldKind string

FieldKind tells the compiler how to resolve a field.

const (
	KindColumn           FieldKind = "column"
	KindCompositeField   FieldKind = "composite_field"   // attribute of a composite-type column
	KindRelationForward  FieldKind = "relation_forward"  // many-to-one: FK on this table
	KindRelationBackward FieldKind = "relation_backward" // one-to-many: FK on the other table
	KindListQuery        FieldKind = "list_query"
	KindConnectionQuery  FieldKind = "connection_query"
	KindRowByKey         FieldKind = "row_by_key"
	KindCreate           FieldKind = "create"
	KindUpdate           FieldKind = "update"
	KindDelete           FieldKind = "delete"
	KindUpsert           FieldKind = "upsert"        // INSERT ... ON CONFLICT (KeyColumns) DO UPDATE
	KindCreateMany       FieldKind = "create_many"   // multi-row INSERT
	KindUpdateMany       FieldKind = "update_many"   // filtered UPDATE
	KindDeleteMany       FieldKind = "delete_many"   // filtered DELETE
	KindPayloadRows      FieldKind = "payload_rows"  // list of mutated rows in a bulk payload
	KindPayloadCount     FieldKind = "payload_count" // affected-row count in a bulk payload
	KindFunction         FieldKind = "function"
	KindComputed         FieldKind = "computed"           // stable function on a row type -> field on that type
	KindPayloadRow       FieldKind = "payload_row"        // the row field inside a mutation payload
	KindPayloadResult    FieldKind = "payload_result"     // the result field inside a function-mutation payload
	KindClientMutationID FieldKind = "client_mutation_id" // Relay-classic clientMutationId passthrough
	KindSynthetic        FieldKind = "synthetic"          // handled by the executor (e.g. connection internals)
	KindNodeID           FieldKind = "node_id"            // nodeId: ID! global identifier on a row type
	KindNode             FieldKind = "node"               // Query.node(nodeId: ID!) global lookup
)

type FieldMeta

type FieldMeta struct {
	Kind       FieldKind
	Table      *introspect.Table      // owning/target table
	Column     *introspect.Column     // for KindColumn
	KeyColumns []string               // lookup / update / delete key
	FK         *introspect.ForeignKey // for relations
	Function   *introspect.Function
	// Nested carries plugin-defined payload (e.g. nested-mutations input shape).
	Nested map[string]any
}

FieldMeta binds a GraphQL field to the catalog for SQL compilation.

type FilterRelation

type FilterRelation struct {
	// Forward: the FK lives on the filtered table and points at Table
	// (many-to-one); the field takes Table's filter directly. Otherwise the
	// FK lives on Table and points back at the filtered table (one-to-many);
	// the field takes a {some|none|every} wrapper over Table's filter.
	Forward bool
	FK      *introspect.ForeignKey
	// Table is the related table the EXISTS subquery scans.
	Table *introspect.Table
	// FilterType is Table's <Type>Filter input name.
	FilterType string
}

FilterRelation binds a relation filter field to the catalog.

type Input

type Input struct {
	Name        string
	Description string
	Fields      []*InputField
}

Input is an input object type.

func (*Input) AddField

func (in *Input) AddField(f *InputField)

func (*Input) Field

func (in *Input) Field(name string) *InputField

func (*Input) RemoveField

func (in *Input) RemoveField(name string)

RemoveField deletes a field by name.

type InputField

type InputField struct {
	Name        string
	Type        string
	Description string
	Default     string
	// Column links the input field to a table column for mutation compilation.
	Column string
	// Relation marks a relation filter field (advanced-filters); the compiler
	// turns it into an EXISTS subquery over the related table.
	Relation *FilterRelation
	// Computed marks a computed-column filter field (advanced-filters): the
	// bound row-type function is called on the current row.
	Computed *introspect.Function
	// Nested marks plugin-added nested-input fields (e.g. nested mutations).
	Nested map[string]any
}

InputField is one field on an input object type.

type Meta

type Meta map[string]map[string]*FieldMeta

Meta is the compiled lookup the executor/compiler use: type name -> field name -> metadata.

type Object

type Object struct {
	Name        string
	Description string
	Fields      []*Field
	// Interfaces lists interface names this object implements.
	Interfaces []string
	// IsInterface renders the type as `interface X` instead of `type X`.
	IsInterface bool
}

Object is an output object type (or interface when IsInterface is set).

func (*Object) AddField

func (o *Object) AddField(f *Field)

AddField appends a field, replacing any existing field of the same name.

func (*Object) Field

func (o *Object) Field(name string) *Field

func (*Object) RemoveField

func (o *Object) RemoveField(name string)

RemoveField deletes a field by name.

type Options

type Options struct {
	FilterIndexedOnly bool
	// AllowColumns lists extra filterable columns per "schema.table".
	AllowColumns map[string][]string
	// Functions exposes PostgreSQL functions as custom queries/mutations.
	Functions bool
	Logger    *slog.Logger
}

Options control default generation.

type OrderSpec

type OrderSpec struct {
	Column string
	Desc   bool
	// Computed orders by a row-type function instead of Column.
	Computed *introspect.Function
}

Jump to

Keyboard shortcuts

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