language

package
v2.16.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package language reads and writes GraphQL documents: a lexer and a parser for executable documents (operations and fragments) and type-system documents (schema definitions, the SDL), the syntax tree both produce, and a printer. It follows the GraphQL specification (October 2021): block strings, \u{…} escapes and descriptions included.

Every node records where it starts (Loc): a 1-based line and a 1-based column counted in characters.

Index

Constants

View Source
const (
	OpQuery        = "query"
	OpMutation     = "mutation"
	OpSubscription = "subscription"
)

Operation kinds, as OperationDefinition.Operation and OperationTypeDefinition.Operation spell them.

Variables

This section is empty.

Functions

func BlockStringValue

func BlockStringValue(raw string) string

BlockStringValue is a block string's value from its raw text (between the quotes, \""" already unescaped): its common indentation and its leading and trailing blank lines removed, lines joined with \n.

func Print

func Print(n Node) string

Print writes a node as GraphQL: a document, a definition, a selection, a value or a type, two spaces an indentation level.

func PrintBlockString

func PrintBlockString(s, indent string) string

PrintBlockString writes s as a block string ("""…"""), its lines after the first indented with indent; a string a block string can't hold as it is (one with leading spaces on its first line, or ending in a quote) is written so it reads back the same.

func PrintString

func PrintString(s string) string

PrintString writes s as a GraphQL string literal, quoted and escaped.

Types

type Argument

type Argument struct {
	Loc
	Name  string
	Value Value
}

Argument is name: value, of a field or a directive.

type BooleanValue

type BooleanValue struct {
	Loc
	Value bool
}

BooleanValue is true or false.

type Definition

type Definition interface {
	Node
	// contains filtered or unexported methods
}

Definition is a top-level definition of a document: an *OperationDefinition or *FragmentDefinition (executable), or a type-system definition.

type Directive

type Directive struct {
	Loc
	Name      string
	Arguments []*Argument
}

Directive is @name(args).

type DirectiveDefinition

type DirectiveDefinition struct {
	Loc
	Description string
	Name        string
	Arguments   []*InputValueDefinition
	Repeatable  bool
	Locations   []string
}

DirectiveDefinition is directive @name(args) repeatable on LOCATIONS.

type Document

type Document struct {
	Loc
	Definitions []Definition
}

Document is a parsed GraphQL document.

func Parse

func Parse(src string) (doc *Document, err error)

Parse parses a GraphQL document: operations and fragments, and type-system definitions (the SDL) too — a document of either kind, or a mix, which validation then refuses where it runs operations.

func (*Document) Fragments

func (d *Document) Fragments() map[string]*FragmentDefinition

Fragments are the document's fragment definitions by name; the first of a name when several share it.

func (*Document) Operations

func (d *Document) Operations() []*OperationDefinition

Operations are the document's operation definitions, in order.

type EnumTypeDefinition

type EnumTypeDefinition struct {
	Loc
	Extend      bool
	Description string
	Name        string
	Directives  []*Directive
	Values      []*EnumValueDefinition
}

EnumTypeDefinition is enum Name { VALUES }.

type EnumValue

type EnumValue struct {
	Loc
	Value string
}

EnumValue is a bare name in a value's position: an enum value.

type EnumValueDefinition

type EnumValueDefinition struct {
	Loc
	Description string
	Name        string
	Directives  []*Directive
}

EnumValueDefinition is one value of an enum.

type Field

type Field struct {
	Loc
	Alias        string // "" when it has none
	Name         string
	Arguments    []*Argument
	Directives   []*Directive
	SelectionSet *SelectionSet // nil for a leaf
}

Field selects a field: alias: name(args) @directives { … }.

func (*Field) ResponseKey

func (f *Field) ResponseKey() string

ResponseKey is the key the field's value takes in the response: its alias, else its name.

type FieldDefinition

type FieldDefinition struct {
	Loc
	Description string
	Name        string
	Arguments   []*InputValueDefinition
	Type        TypeRef
	Directives  []*Directive
}

FieldDefinition is a field of an object or interface type.

type FloatValue

type FloatValue struct {
	Loc
	Value string
}

FloatValue is a float literal, as written.

type FragmentDefinition

type FragmentDefinition struct {
	Loc
	Name          string
	NameLoc       Loc
	TypeCondition *NamedType
	Directives    []*Directive
	SelectionSet  *SelectionSet
}

FragmentDefinition is a named fragment: fragment Name on Type { … }.

type FragmentSpread

type FragmentSpread struct {
	Loc
	Name       string
	Directives []*Directive
}

FragmentSpread is ...Name @directives.

type InlineFragment

type InlineFragment struct {
	Loc
	TypeCondition *NamedType
	Directives    []*Directive
	SelectionSet  *SelectionSet
}

InlineFragment is ... on Type @directives { … }; TypeCondition is nil when the fragment names no type.

type InputObjectTypeDefinition

type InputObjectTypeDefinition struct {
	Loc
	Extend      bool
	Description string
	Name        string
	Directives  []*Directive
	Fields      []*InputValueDefinition
}

InputObjectTypeDefinition is input Name { fields }.

type InputValueDefinition

type InputValueDefinition struct {
	Loc
	Description  string
	Name         string
	Type         TypeRef
	DefaultValue Value // nil when it has none
	Directives   []*Directive
}

InputValueDefinition is an argument or an input object's field.

type IntValue

type IntValue struct {
	Loc
	Value string
}

IntValue is an integer literal, as written.

type InterfaceTypeDefinition

type InterfaceTypeDefinition struct {
	Loc
	Extend      bool
	Description string
	Name        string
	Interfaces  []*NamedType
	Directives  []*Directive
	Fields      []*FieldDefinition
}

InterfaceTypeDefinition is interface Name implements A { fields }.

type ListType

type ListType struct {
	Loc
	Type TypeRef
}

ListType is [Type].

func (*ListType) String

func (t *ListType) String() string

type ListValue

type ListValue struct {
	Loc
	Values []Value
}

ListValue is [ … ].

type Loc

type Loc struct{ Line, Column int }

Loc is where a node starts in its source: 1-based line and column, the column counted in characters (runes).

func (Loc) Location

func (l Loc) Location() Loc

Location is the node's position.

type NamedType

type NamedType struct {
	Loc
	Name string
}

NamedType is a type's name.

func (*NamedType) String

func (t *NamedType) String() string

type Node

type Node interface{ Location() Loc }

Node is any node of the syntax tree.

type NonNullType

type NonNullType struct {
	Loc
	Type TypeRef
}

NonNullType is Type!.

func (*NonNullType) String

func (t *NonNullType) String() string

type NullValue

type NullValue struct{ Loc }

NullValue is null.

type ObjectField

type ObjectField struct {
	Loc
	Name  string
	Value Value
}

ObjectField is one name: value of an ObjectValue.

type ObjectTypeDefinition

type ObjectTypeDefinition struct {
	Loc
	Extend      bool
	Description string
	Name        string
	Interfaces  []*NamedType
	Directives  []*Directive
	Fields      []*FieldDefinition
}

ObjectTypeDefinition is type Name implements A & B { fields }.

type ObjectValue

type ObjectValue struct {
	Loc
	Fields []*ObjectField
}

ObjectValue is { name: value … }.

type OperationDefinition

type OperationDefinition struct {
	Loc
	Operation           string // "query", "mutation" or "subscription"
	Name                string
	NameLoc             Loc
	VariableDefinitions []*VariableDefinition
	Directives          []*Directive
	SelectionSet        *SelectionSet
	Shorthand           bool
}

OperationDefinition is a query, mutation or subscription. Shorthand is a query written as a bare selection set ("{ a }").

type OperationTypeDefinition

type OperationTypeDefinition struct {
	Loc
	Operation string
	Type      *NamedType
}

OperationTypeDefinition is query: Query inside a schema definition.

type ScalarTypeDefinition

type ScalarTypeDefinition struct {
	Loc
	Extend      bool
	Description string
	Name        string
	Directives  []*Directive
}

ScalarTypeDefinition is scalar Name.

type SchemaDefinition

type SchemaDefinition struct {
	Loc
	Extend         bool
	Description    string
	Directives     []*Directive
	OperationTypes []*OperationTypeDefinition
}

SchemaDefinition is schema { query: Query … }.

type Selection

type Selection interface {
	Node
	// contains filtered or unexported methods
}

Selection is a *Field, *FragmentSpread or *InlineFragment.

type SelectionSet

type SelectionSet struct {
	Loc
	Selections []Selection
}

SelectionSet is { … }: the fields and fragments selected.

type StringValue

type StringValue struct {
	Loc
	Value string
	Block bool
}

StringValue is a string literal, its escapes resolved (a block string's indentation removed); Block says it was written """…""".

type SyntaxError

type SyntaxError struct {
	Message      string
	Line, Column int
}

SyntaxError is a document that doesn't parse: what is wrong, and where.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type TypeRef

type TypeRef interface {
	Node

	String() string
	// contains filtered or unexported methods
}

TypeRef is a type as a document writes it: *NamedType, *ListType or *NonNullType.

func ParseType

func ParseType(src string) (t TypeRef, err error)

ParseType parses a type reference: "[String!]!".

type UnionTypeDefinition

type UnionTypeDefinition struct {
	Loc
	Extend      bool
	Description string
	Name        string
	Directives  []*Directive
	Types       []*NamedType
}

UnionTypeDefinition is union Name = A | B.

type Value

type Value interface {
	Node
	// contains filtered or unexported methods
}

Value is a literal or a variable: *Variable, *IntValue, *FloatValue, *StringValue, *BooleanValue, *NullValue, *EnumValue, *ListValue or *ObjectValue.

func ParseValue

func ParseValue(src string, constant bool) (v Value, err error)

ParseValue parses a single value, such as a default value written on its own: "[1, 2]", "{a: true}". Variables are allowed unless constant.

type Variable

type Variable struct {
	Loc
	Name string
}

Variable is $name in a value's position.

type VariableDefinition

type VariableDefinition struct {
	Loc
	Variable     string // its name, without $
	Type         TypeRef
	DefaultValue Value // nil when it has none
	Directives   []*Directive
}

VariableDefinition declares an operation's variable: $name: Type = default.

Jump to

Keyboard shortcuts

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