compile

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 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 SkipByDirectives added in v0.7.0

func SkipByDirectives(dl ast.DirectiveList, vars map[string]any) bool

SkipByDirectives reports whether a selection is excluded by its @skip / @include directives per the GraphQL spec: excluded when any @skip has if: true or any @include has if: false (with both present, the selection survives only when @skip is false and @include is true). The `if` argument is Boolean! and validated upstream; an unresolvable value keeps the selection.

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).

type UserError added in v0.7.0

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

UserError is a compile error whose message is safe to return to API clients even with errors.detail "prod": it describes a problem with the request itself (pagination arguments, cursors, input shape) and contains no schema internals. Compile errors that are not UserErrors are sanitized to a generic message by the executor in prod mode.

func (*UserError) Error added in v0.7.0

func (e *UserError) Error() string

Jump to

Keyboard shortcuts

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