schemabuilder

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NonNullable

func NonNullable(m *method)

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.

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  *int64
	Last   *int64
	After  *string
	Before *string
	Args   interface{}
}

ConnectionArgs conform to the pagination arguments as specified by the Relay Spec for Connection types. The Args field consits of the user-facing args.

type Edge

type Edge struct {
	Node   interface{}
	Cursor string
}

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

func EdgesToReturn

func EdgesToReturn(allEdges []Edge, before *string, after *string, first *int64, last *int64) ([]Edge, bool, bool, error)

EdgesToReturn returns the slice of edges by appyling the pagination arguments. It also returns the hasNextPage and hasPrevPage values respectively. The behavior is expected to conform to the Relay Cursor spec: https://facebook.github.io/relay/graphql/connections.htm#EdgesToReturn()

type EnumMapping

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

type FieldFuncOption

type FieldFuncOption func(*method)

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

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.

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
}

PaginationArgs are embedded in a struct

type PaginationInfo

type PaginationInfo struct {
	TotalCount  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.

type Schema

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

func NewSchema

func NewSchema() *Schema

func (*Schema) Build

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

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

func (*Schema) Object

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

func (*Schema) Query

func (s *Schema) Query() *Object

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 {
  graphql.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