sugar

package module
v0.0.0-...-1694d17 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2019 License: MIT Imports: 15 Imported by: 0

README

graphql-sugar

Some helpers to make common patterns in graphql-go less boilerplatey.

This code began life at Nav, where it was written by Brent Tubbs, Zach Abrahams, Tyson Costello, and Craig Jackson. Maybe some of this should be submitted upstream.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BaseLoaders = []struct {
	LoaderFunc interface{}
	GqlType    graphql.Output
}{
	{LoaderFunc: LoadBool, GqlType: graphql.Boolean},
	{LoaderFunc: LoadBoolPointer, GqlType: graphql.Boolean},
	{LoaderFunc: LoadString, GqlType: graphql.String},
	{LoaderFunc: LoadInt, GqlType: graphql.Int},
	{LoaderFunc: LoadFloat, GqlType: graphql.Float},
	{LoaderFunc: LoadTime, GqlType: Timestamp},
}

BaseLoaders are for the 4 scalar types built into GraphQL.

View Source
var DefaultLoaders = []struct {
	LoaderFunc interface{}
	GqlType    graphql.Output
}{

	{LoaderFunc: LoadRawJSON, GqlType: JSON},
	{LoaderFunc: LoadUInt, GqlType: graphql.Int},
}

DefaultLoaders are for extra types beyond the 4 scalar types built into GraphQL.

View Source
var JSON = graphql.NewScalar(
	graphql.ScalarConfig{
		Name:         "JSON",
		Description:  "JSON is a custom scalar for attaching json blobs as leaves on a graphql API request or response.",
		Serialize:    JSONSerialize,
		ParseValue:   JSONParseValue,
		ParseLiteral: JSONParseLiteral,
	})

JSON is a custom scalar type for using custom json blobs as leaves on our responses.

View Source
var Timestamp = graphql.NewScalar(graphql.ScalarConfig{
	Name:         "Timestamp",
	Description:  "Timestamp is an ISO8601-formatted date/time string. Values that omit a time zone are assumed to be UTC.",
	Serialize:    timeSerialize,
	ParseValue:   timeParseValue,
	ParseLiteral: timeParseLiteral,
})

Timestamp is our custom scalar for handling datetimes.

Functions

func ArgsConfig

func ArgsConfig(i interface{}) graphql.FieldConfigArgument

ArgsConfig takes a struct instance with appropriate struct tags on its fields and returns a map of argument names to graphql argument configs, for assigning to the Args field in a graphql.Field. If there is an error generating the argument configs, this function will panic. It uses the default arg loader.

func JSONParseLiteral

func JSONParseLiteral(valueAST ast.Value) interface{}

JSONParseLiteral converts fragments of a graphql AST into a json Raw Message. It's used for parsing input objects for our custom scalar JSON type. The assumption is that the graphql query object is close enough to json that we can convert it.

func JSONParseValue

func JSONParseValue(value interface{}) interface{}

JSONParseValue implements the ParseValue so that JSON can satisfy the graphql.Scalar interface. This method gets called when parsing JSON type obejcts out of query variables This is also called when doing the query: variables: pattern. Assume that this can be Marshalled like below

func JSONSerialize

func JSONSerialize(value interface{}) interface{}

JSONSerialize serializes objects into the custom JSON blob graphql scalar type.

func LoadArgs

func LoadArgs(p graphql.ResolveParams, c interface{}) error

LoadArgs loads arguments from the ResolveParam's map into the provided struct. It uses the default arg loader.

func LoadBool

func LoadBool(i interface{}) (bool, error)

LoadBool loads `bool` from graphql arg

func LoadBoolPointer

func LoadBoolPointer(i interface{}) (*bool, error)

LoadBoolPointer loads `*bool` from graphql arg

func LoadFloat

func LoadFloat(i interface{}) (float64, error)

LoadFloat loads `float` from graphql arg

func LoadInt

func LoadInt(i interface{}) (int, error)

LoadInt loads `int` from graphql arg

func LoadNullInt

func LoadNullInt(i interface{}) (null.Int, error)

LoadNullInt loads `null.Int` from graphql arg

func LoadNullString

func LoadNullString(i interface{}) (null.String, error)

LoadNullString will attempt to load a `null.String` from graphql arg

func LoadRawJSON

func LoadRawJSON(i interface{}) (pqjson.RawMessage, error)

LoadRawJSON loads `pqjson.RawMessage` from graphql arg

func LoadString

func LoadString(i interface{}) (string, error)

LoadString loads `string` from graphql arg

func LoadTime

func LoadTime(i interface{}) (time.Time, error)

LoadTime loads `time.Time` from graphql arg

func LoadUInt

func LoadUInt(i interface{}) (uint, error)

LoadUInt loads `uint` from graphql arg

func OutputType

func OutputType(name, desc string, val interface{}) graphql.Output

OutputType builds a GraphQL type for you from the given name, desc, and val. val may be a struct instance, slice, array, pointer, or an alias to a builtin type like int or string.

func RegisterArgParser

func RegisterArgParser(f interface{}, gqlType graphql.Output) error

RegisterArgParser takes a func (string) (<anytype>, error) and registers it on the ArgLoader as the parser for <anytype>. It uses the default arg loader.

func RegisterKnownType

func RegisterKnownType(val interface{}, gqlType graphql.Output)

RegisterKnownType takes any value, and the GraphQL type that should represent it, and will use that when building types.

func SafeArgsConfig

func SafeArgsConfig(i interface{}) (graphql.FieldConfigArgument, error)

SafeArgsConfig -- this takes a struct instance w/ tags and returns a map of argument names. This will not cause a panic upon error. It uses the default arg loader.

func Union

func Union(name, desc string, vals ...interface{}) *graphql.Union

Union is just like OutputType, but takes in multiple vals and builds a GraphQL union type out of them.

Types

type ArgLoader

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

ArgLoader is a helper for reading arguments from a graphql.ResolveParams, converting them to Go types, and setting their values to fields on a user-provided struct.

func Base

func Base() (*ArgLoader, error)

Base returns a ArgLoader with the 4 base loader funcs enabled.

func Empty

func Empty() *ArgLoader

Empty returns a ArgLoader without any loader funcs enabled.

func New

func New() (*ArgLoader, error)

New returns a ArgLoader with all default loader funcs enabled.

func (*ArgLoader) ArgsConfig

func (e *ArgLoader) ArgsConfig(i interface{}) graphql.FieldConfigArgument

ArgsConfig takes a struct instance with appropriate struct tags on its fields and returns a map of argument names to graphql argument configs, for assigning to the Args field in a graphql.Field. If there is an error generating the argument configs, this function will panic.

func (*ArgLoader) LoadArgs

func (e *ArgLoader) LoadArgs(p graphql.ResolveParams, c interface{}) error

LoadArgs loads arguments from the provided map into the provided struct.

func (*ArgLoader) RegisterArgParser

func (e *ArgLoader) RegisterArgParser(f interface{}, gqlType graphql.Output) error

RegisterArgParser takes a func (string) (<anytype>, error) and registers it on the ArgLoader as the parser for <anytype>

func (*ArgLoader) SafeArgsConfig

func (e *ArgLoader) SafeArgsConfig(i interface{}) (graphql.FieldConfigArgument, error)

SafeArgsConfig -- this takes a struct instance w/ tags and returns a map of argument names. This will not cause a panic upon error

type TypeBuilder

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

A TypeBuilder helps create graphql-go output types

func NewTypeBuilder

func NewTypeBuilder() *TypeBuilder

NewTypeBuilder creates a new TypeBuilder and registers known types on it.

func (*TypeBuilder) OutputType

func (tb *TypeBuilder) OutputType(name, desc string, val interface{}) graphql.Output

OutputType builds a GraphQL type for you from the given name, desc, and val. val may be a struct instance, slice, array, pointer, or an alias to a builtin type like int or string.

func (*TypeBuilder) RegisterKnownType

func (tb *TypeBuilder) RegisterKnownType(val interface{}, gqlType graphql.Output)

RegisterKnownType takes any value, and the GraphQL type that should represent it, and will use that when building types.

func (*TypeBuilder) Union

func (tb *TypeBuilder) Union(name, desc string, vals ...interface{}) *graphql.Union

Union is just like OutputType, but takes in multiple vals and builds a GraphQL union type out of them.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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