compile

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: 13 Imported by: 0

Documentation

Overview

Package compile turns one validated GraphQL root field into exactly one parameterized SQL statement, PostGraphile-style: nested selections become jsonb_build_object trees, relations become LEFT JOIN LATERAL subqueries, lists become jsonb_agg — so the database returns the response JSON directly and there is no N+1 problem to paper over.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CoerceInput added in v0.1.1

func CoerceInput(built *schema.Built, col *introspect.Column, v any) any

CoerceInput converts a GraphQL input value into a driver-friendly parameter for the given column — enum value names map to their PostgreSQL labels, json/jsonb values are re-encoded as text, array elements coerce recursively. CompileHook plugins building their own DML must route column values through this before ParamSet.Add, or enum/jsonb params reach the driver raw.

func QuoteIdent

func QuoteIdent(s string) string

QuoteIdent is exported for plugins building SQL fragments.

func TableRefSQL

func TableRefSQL(schemaName, table string) string

TableRefSQL renders a quoted schema-qualified relation reference.

Types

type Compiler

type Compiler struct {
	Built *schema.Built
}

Compiler compiles GraphQL fields against a built schema.

func New

func New(built *schema.Built) *Compiler

func (*Compiler) Compile

func (c *Compiler) Compile(ctx context.Context, req *Request) (*Statement, error)

Compile is the base Func.

func (*Compiler) FieldMeta

func (c *Compiler) FieldMeta(typeName, fieldName string) *schema.FieldMeta

FieldMeta exposes metadata lookup to plugins.

func (*Compiler) MutationWithCTEs

func (c *Compiler) MutationWithCTEs(req *Request, f *ast.Field, meta *schema.FieldMeta, ctes string, ps *ParamSet, overrides map[string]string) (*Statement, error)

MutationWithCTEs compiles the payload selection of a mutation root field against a caller-supplied CTE chain. The chain's final CTE must be named __mut and yield the mutated row(s); params created via ps are preserved and the payload's own params continue after them. This is the extension point CompileHook plugins (e.g. nested-mutations) use to replace the DML while reusing payload/relation compilation. overrides remaps "schema.table" row sources to CTE names so the payload can see rows inserted by the supplied CTEs (same-snapshot rule).

type Func

type Func func(ctx context.Context, req *Request) (*Statement, error)

Func compiles a request; CompileHook plugins wrap it middleware-style.

type ParamSet

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

ParamSet lets CompileHook plugins build parameterized SQL fragments whose placeholder numbering composes with the core compiler.

func (*ParamSet) Add

func (p *ParamSet) Add(v any) string

func (*ParamSet) Args

func (p *ParamSet) Args() []any

type Request

type Request struct {
	Field     *ast.Field
	Fragments ast.FragmentDefinitionList
	Vars      map[string]any
	Built     *schema.Built
	MaxDepth  int
	// MaxPageSize caps first/last and is the default page size when neither
	// is given. Values <= 0 fall back to defaultMaxPageSize.
	MaxPageSize int
}

Request is one root field to compile.

type Statement

type Statement struct {
	SQL  string
	Args []any
	// ReturnsRow is false when zero rows means null (single lookups).
	Mutation bool
}

Statement is a single executable SQL statement. The statement always returns a single row with a single jsonb column named "data" (possibly zero rows for missed single-row lookups).

Jump to

Keyboard shortcuts

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