schemabuilder

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2019 License: MIT Imports: 15 Imported by: 50

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NonNullable fieldFuncOptionFunc = func(m *method) {
	m.MarkedNonNullable = true
}

NonNullable is an option that can be passed to a FieldFunc to indicate that its return value is required, even if the return value is a pointer type.

View Source
var Paginated fieldFuncOptionFunc = func(m *method) {
	m.Paginated = true
}

Paginated is an option that can be passed to a FieldFunc to indicate that its return value should be paginated.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	TotalCount int64
	Edges      []Edge
	PageInfo   PageInfo
}

Connection conforms to the GraphQL Connection type in the Relay Pagination spec.

type ConnectionArgs

type ConnectionArgs struct {
	// first: n
	First *int64
	// last: n
	Last *int64
	// after: cursor
	After *string
	// before: cursor
	Before *string
	// User-facing args.
	Args interface{}
	// filterText: "text search"
	FilterText *string
	// sortBy: "fieldName"
	SortBy *string
	// sortOrder: "asc" | "desc"
	SortOrder *SortOrder
}

ConnectionArgs conform to the pagination arguments as specified by the Relay Spec for Connection types. https://facebook.github.io/relay/graphql/connections.htm#sec-Arguments

type Edge

type Edge struct {
	Node   interface{}
	Cursor string
}

Edge consists of a node paired with its b64 encoded cursor.

type EnumMapping

type EnumMapping struct {
	Map        map[string]interface{}
	ReverseMap map[interface{}]string
}

EnumMapping is a representation of an enum that includes both the mapping and reverse mapping.

type FieldFuncOption

type FieldFuncOption interface {
	// contains filtered or unexported methods
}

FieldFuncOption is an interface for the variadic options that can be passed to a FieldFunc for configuring options on that function.

type Methods

type Methods map[string]*method

A Methods map represents the set of methods exposed on a Object.

type Object

type Object struct {
	Name        string // Optional, defaults to Type's name.
	Description string
	Type        interface{}
	Methods     Methods // Deprecated, use FieldFunc instead.
	// contains filtered or unexported fields
}

A Object represents a Go type and set of methods to be converted into an Object in a GraphQL schema.

func (*Object) FieldFunc

func (s *Object) FieldFunc(name string, f interface{}, options ...FieldFuncOption)

FieldFunc exposes a field on an object. The function f can take a number of optional arguments: func([ctx context.Context], [o *Type], [args struct {}]) ([Result], [error])

For example, for an object of type User, a fullName field might take just an instance of the object:

user.FieldFunc("fullName", func(u *User) string {
   return u.FirstName + " " + u.LastName
})

An addUser mutation field might take both a context and arguments:

mutation.FieldFunc("addUser", func(ctx context.Context, args struct{
    FirstName string
    LastName  string
}) (int, error) {
    userID, err := db.AddUser(ctx, args.FirstName, args.LastName)
    return userID, err
})

func (*Object) Key

func (s *Object) Key(f string)

Key registers the key field on an object. The field should be specified by the name of the graphql field. For example, for an object User:

  type struct User {
	   UserKey int64
  }

The key will be registered as: object.Key("userKey")

func (*Object) PaginateFieldFunc deprecated

func (o *Object) PaginateFieldFunc(name string, f interface{})

PaginateFieldFunc registers a function that is also paginated according to the Relay Connection Spec. The field is registered as a Connection Type and first, last, before and after are automatically added as arguments to the function. The return type to the function must be a list. The element of the list is wrapped as a Node Type. If the resolver needs to use the pagination arguments, then the PaginationArgs struct must be embedded in the args struct passed in the resolver function, and the PaginationInfo struct needs to be returned in the resolver func.

Deprecated: Use FieldFunc(name, func, Paginated) instead.

type PageInfo

type PageInfo struct {
	HasNextPage bool
	EndCursor   string
	HasPrevPage bool
	StartCursor string
	Pages       []string
}

PageInfo contains information for pagination on a connection type. The list of Pages is used for page-number based pagination where the ith index corresponds to the start cursor of (i+1)st page.

type PaginationArgs

type PaginationArgs struct {
	First  *int64
	Last   *int64
	After  *string
	Before *string

	FilterText *string
	SortBy     *string
	SortOrder  *SortOrder
}

PaginationArgs are used in externally set connections by embedding them in an args struct. They are mapped onto ConnectionArgs, which follows the Relay spec for connection types.

type PaginationInfo

type PaginationInfo struct {
	TotalCountFunc func() int64
	HasNextPage    bool
	HasPrevPage    bool
}

PaginationInfo can be returned in a PaginateFieldFunc. The TotalCount function returns the totalCount field on the connection Type. If the resolver makes a SQL Query, then HasNextPage and HasPrevPage can be resolved in an efficient manner by requesting first/last:n + 1 items in the query. Then the flags can be filled in by checking the result size.

func (PaginationInfo) TotalCount

func (i PaginationInfo) TotalCount() (int64, error)

type Schema

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

Schema is a struct that can be used to build out a GraphQL schema. Functions can be registered against the "Mutation" and "Query" objects in order to build out a full GraphQL schema.

func NewSchema

func NewSchema() *Schema

NewSchema creates a new schema.

func (*Schema) Build

func (s *Schema) Build() (*graphql.Schema, error)

Build takes the schema we have built on our Query and Mutation starting points and builds a full graphql.Schema we can use to execute and run queries. Essentially we read through all the methods we've attached to our Query and Mutation Objects and ensure that those functions are returning other Objects that we can resolve in our GraphQL graph.

func (*Schema) Enum

func (s *Schema) Enum(val interface{}, enumMap interface{})

Enum registers an enumType in the schema. The val should be any arbitrary value of the enumType to be used for reflection, and the enumMap should be the corresponding map of the enums.

For example a enum could be declared as follows:

  type enumType int32
  const (
	  one   enumType = 1
	  two   enumType = 2
	  three enumType = 3
  )

Then the Enum can be registered as:

s.Enum(enumType(1), map[string]interface{}{
  "one":   enumType(1),
  "two":   enumType(2),
  "three": enumType(3),
})

func (*Schema) MustBuild

func (s *Schema) MustBuild() *graphql.Schema

MustBuildSchema builds a schema and panics if an error occurs.

func (*Schema) Mutation

func (s *Schema) Mutation() *Object

Mutation returns an Object struct that we can use to register all the top level graphql mutations functions we'd like to expose.

func (*Schema) Object

func (s *Schema) Object(name string, typ interface{}) *Object

Object registers a struct as a GraphQL Object in our Schema. (https://facebook.github.io/graphql/June2018/#sec-Objects) We'll read the fields of the struct to determine it's basic "Fields" and we'll return an Object struct that we can use to register custom relationships and fields on the object.

func (*Schema) Query

func (s *Schema) Query() *Object

Query returns an Object struct that we can use to register all the top level graphql query functions we'd like to expose.

type SortFields added in v0.5.0

type SortFields map[string]interface{}

type SortOrder added in v0.5.0

type SortOrder int64
const (
	SortOrder_Ascending SortOrder = iota
	SortOrder_Descending
)

type TextFilterFields added in v0.5.0

type TextFilterFields map[string]interface{}

type Union

type Union struct{}

Union is a special marker struct that can be embedded into to denote that a type should be treated as a union type by the schemabuilder.

For example, to denote that a return value that may be a *Asset or *Vehicle might look like:

type GatewayUnion struct {
  schemabuilder.Union
  *Asset
  *Vehicle
}

Fields returning a union type should expect to return this type as a one-hot struct, i.e. only Asset or Vehicle should be specified, but not both.

Jump to

Keyboard shortcuts

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