parser

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package parser provides GraphQL query hashing functions for the latest GraphQL specification: https://spec.graphql.org/October2021/.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnexpectedEOF   = errors.New("unexpected EOF")
	ErrUnexpectedToken = errors.New("unexpected token")
)
View Source
var (
	HPrefQuery                 = []byte{0x1}
	HPrefMutation              = []byte{0x2}
	HPrefSubscription          = []byte{0x3}
	HPrefFragmentDefinition    = []byte{0x4}
	HPrefVariableDefinition    = []byte{0x5}
	HPrefDirective             = []byte{0x6}
	HPrefField                 = []byte{0x7}
	HPrefType                  = []byte{0x8}
	HPrefFieldAliasedName      = []byte{0xb} // The actual name of the aliased field.
	HPrefFragmentSpread        = []byte{0xc}
	HPrefInlineFragment        = []byte{0xe}
	HPrefArgument              = []byte{0xf}
	HPrefSelectionSet          = []byte{0x11}
	HPrefSelectionSetEnd       = []byte{0x12}
	HPrefValueInputObject      = []byte{0x13}
	HPrefValueInputObjectField = []byte{0x14}
	HPrefInputObjectEnd        = []byte{0x15}
	HPrefValueNull             = []byte{0x16}
	HPrefValueTrue             = []byte{0x17}
	HPrefValueFalse            = []byte{0x18}
	HPrefValueInteger          = []byte{0x19}
	HPrefValueFloat            = []byte{0x1a}
	HPrefValueEnum             = []byte{0x1b}
	HPrefValueString           = []byte{0x1c}
	HPrefValueList             = []byte{0x1d}
	HPrefValueListEnd          = []byte{0x1e}
	HPrefValueVariable         = []byte{0x1f}
)

The hash prefixes are used as magic bytes before writing actual query contents to prevent tokens from collapsing into one if separators aren't written, for example: query fields `{ foo bar }` might collapse into one field `{ foobar }` producing the same hash for those two different queries. 0x9, 0xA and 0xD cannot be used because they're valid bytes within string values (https://spec.graphql.org/June2018/#SourceCharacter).

Functions

func ExpectNoEOF

func ExpectNoEOF(s []byte) error

ExpectNoEOF returns ErrUnexpectedEOF if s is empty, otherwise returns nil.

func HasPrefix

func HasPrefix(s []byte, prefix string) bool

HasPrefix is equivalent to strings.HasPrefix and bytes.HasPrefix except that it works for both string and []byte.

func IsDigit

func IsDigit(b byte) bool

IsDigit returns true if b is a Digit. Reference:

func IsHexByte

func IsHexByte(b byte) bool

IsHexByte returns true if b is a hexadecimal digit.

func IsIgnorableByte

func IsIgnorableByte(b byte) bool

IsIgnorableByte returns true if b is ignorable. Reference:

func IsLetter

func IsLetter(b byte) bool

IsLetter returns true if b is Letter. Reference:

func IsNameStart

func IsNameStart(b byte) bool

IsNameStart returns true if b is NameStart. Reference:

func IsWhiteSpace

func IsWhiteSpace(b byte) bool

IsWhiteSpace returns true if b is a WhiteSpace. Reference:

func IterateBlockStringLines

func IterateBlockStringLines(s []byte, prefixLen int) iter.Seq[[]byte]

IterateBlockStringLines iterates over individual lines of a GraphQL block string. Expects s to be the content of the string without the surrounding `"""`.

func ReadArguments

func ReadArguments(h Hash, s []byte) (arguments, suffix []byte, err error)

ReadArguments reads Arguments. Reference:

func ReadDefinition

func ReadDefinition(h Hash, s []byte) (suffix []byte, err error)

ReadDefinition reads Definition. Reference:

func ReadDirectives

func ReadDirectives(h Hash, s []byte) (directives, suffix []byte, err error)

ReadDirectives reads Directives. Reference:

func ReadDocument

func ReadDocument(h Hash, s []byte) (err error)

ReadDocument reads one or many ExecutableDefinitions

func ReadFloatAfterInteger added in v1.2.4

func ReadFloatAfterInteger(s []byte) (value []byte, suffix []byte, err error)

ReadFloatAfterInteger reads the part of the FloatValue that comes after the first IntegerPart. Reference:

func ReadIntValue

func ReadIntValue(s []byte) (value []byte, suffix []byte, err error)

ReadIntValue reads IntValue. Reference:

func ReadName

func ReadName(s []byte) (name, suffix []byte, err error)

ReadName reads a Name token. Reference:

func ReadOperationDefinition

func ReadOperationDefinition(h Hash, s []byte) (suffix []byte, err error)

ReadOperationDefinition reads OperationDefinition but not the SelectionSet-only version of it. Reference:

func ReadSelectionSet

func ReadSelectionSet(h Hash, s []byte) (suffix []byte, err error)

ReadVariableDefinitions reads VariableDefinitions. Reference:

func ReadStringBlockAfterQuotes

func ReadStringBlockAfterQuotes(s []byte) (
	value []byte, prefixLen int, suffix []byte, err error,
)

ReadStringBlockAfterQuotes reads a block string StringValue contents after '"""'. Tip: Use ReadStringLineAfterQuotes for single-line strings. Reference:

func ReadStringLineAfterQuotes

func ReadStringLineAfterQuotes(s []byte) (value []byte, suffix []byte, err error)

ReadStringLineAfterQuotes reads a single-line StringValue contents after '"'. Tip: Use ReadStringBlock for block strings. Reference:

func ReadToken

func ReadToken(s []byte, token string) (suffix []byte, err error)

ReadToken expects token to be prefix of s and returns []byte the token trimmed.

func ReadType

func ReadType(s []byte) (typeDef []byte, nullable, array bool, suffix []byte, err error)

ReadType reads Type. Reference:

func ReadVariableDefinitionsAfterParenthesis

func ReadVariableDefinitionsAfterParenthesis(h Hash, s []byte) (suffix []byte, err error)

ReadVariableDefinitionsAfterParenthesis reads VariableDefinitions after '(' and any ignorables. Reference:

func SkipIgnorables

func SkipIgnorables(s []byte) []byte

SkipIgnorables skips over any comments, spaces, tabs, line-breaks and carriage-returns it encounters and returns the s suffix. Reference:

func TrimEmptyLinesSuffix

func TrimEmptyLinesSuffix(s []byte) []byte

TrimEmptyLinesSuffix removes any trailing empty lines from the s. An empty line is defined as a line that contains only whitespace characters.

Types

type Hash

type Hash interface {
	Reset()
	Sum([]byte) []byte
	Write([]byte) (int, error)
}

Hash is a subset of the standard `hash.Hash`.

type OperationType

type OperationType int8
const (
	OperationTypeQuery OperationType
	OperationTypeMutation
	OperationTypeSubscription
)

func ReadOperationType

func ReadOperationType(h Hash, s []byte) (
	operationType OperationType, suffix []byte, err error,
)

ReadOperationType reads OperationType. Reference:

type ValueType

type ValueType int8

ValueType represents the type of a value Reference:

const (
	ValueTypeInt ValueType
	ValueTypeFloat
	ValueTypeString
	ValueTypeStringBlock
	ValueTypeBooleanTrue
	ValueTypeBooleanFalse
	ValueTypeNull
	ValueTypeEnum
	ValueTypeList
	ValueTypeInputObject
	ValueTypeVariable
)

func ReadValue

func ReadValue(h Hash, s []byte) (
	value []byte, valueType ValueType, suffix []byte, err error,
)

ReadValue reads Value. Reference:

Jump to

Keyboard shortcuts

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