schemabuilder

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const DuplicateTypeNameErrFormat string = "%s type name is duplicated in packages %s and %s"

Variables

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

Expensive is an option that can be passed to a FieldFunc to indicate that the function is expensive to execute, so it should be parallelized.

View Source
var ListEntryNonNullable fieldFuncOptionFunc = func(m *method) {
	m.MarkedListEntryNonNullable = true
}
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
	// FilterTextFields: ["filter name"]
	FilterTextFields *[]string
	// sortBy: "fieldName"
	SortBy *string
	// sortOrder: "asc" | "desc"
	SortOrder *SortOrder
	// filterType: "customFilterType"
	// Note: FilterType is not part of the Relay Spec for Connection types
	FilterType *string
}

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.

func BatchFilterField

func BatchFilterField(name string, batchFilter interface{}, options ...FieldFuncOption) FieldFuncOption

func BatchFilterFieldWithFallback

func BatchFilterFieldWithFallback(name string, batchFilter interface{}, filter interface{}, flag func(context.Context) bool, options ...FieldFuncOption) FieldFuncOption

func BatchSortField

func BatchSortField(name string, batchSort interface{}, options ...FieldFuncOption) FieldFuncOption

func BatchSortFieldWithFallback

func BatchSortFieldWithFallback(name string, batchSort interface{}, sort interface{}, flag func(context.Context) bool, options ...FieldFuncOption) FieldFuncOption

func FilterField

func FilterField(name string, filter interface{}, options ...FieldFuncOption) FieldFuncOption

func FilterFunc

func FilterFunc(name string, tokenizeFilterText func(string) []string, filterFunc func(string, []string) bool) FieldFuncOption

FilterFunc is an option that can be passed to a FieldFunc to specify custom string matching algorithms for filtering FieldFunc results.

It accepts 3 parameters: 1. name This should describe the filter behaviour (e.g. "fuzzySearch"). This parameter corresponds with the PaginationArg prop FilterType. Specifying filterType on a GraphQL query will indicate which tokenization and matching algorithms (see below) are used to filter results.

2. tokenizeFilterText This argument should be an algorithm that determines how the search query string is broken up into search tokens. It expects to receive a search string and return a list of search token strings which will be used to compare against the field string for a match in 'filterFunc' (see below).

3. filterFunc This argument should be an algorithm that determines whether there is a match between the field string and the search tokens. It expects to receive the field string and the list of search tokens (generated from 'tokenizeFilterText') and return a boolean which signals whether there is a match that should be included in the returned results.

func SortField

func SortField(name string, sort interface{}, options ...FieldFuncOption) FieldFuncOption

type Methods

type Methods map[string]*method

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

type NumParallelInvocationsFunc

type NumParallelInvocationsFunc func(ctx context.Context, numNodes int) int

NumParallelInvocationsFunc is a configuration option for non-expensive and batch fields that controls how many goroutines will get created for the field execution. The nodes for the field execution will be evenly split across the different goroutines.

type Object

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

	ServiceName string
	// 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) BatchFieldFunc

func (s *Object) BatchFieldFunc(name string, batchFunc interface{}, options ...FieldFuncOption)

func (*Object) BatchFieldFuncWithFallback

func (s *Object) BatchFieldFuncWithFallback(name string, batchFunc interface{}, fallbackFunc interface{}, flag UseFallbackFlag, options ...FieldFuncOption)

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) ManualPaginationWithFallback

func (s *Object) ManualPaginationWithFallback(name string, manualPaginatedFunc interface{}, fallbackFunc interface{}, flag UseFallbackFlag, options ...FieldFuncOption)

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 ObjectOption

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

OpjectOption is an interface for the variadic options that can be passed to a Object for configuring options on that object.

func FetchObjectFromKeys

func FetchObjectFromKeys(f interface{}, options ...ObjectOption) ObjectOption

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
	FilterTextFields *[]string
	SortBy           *string
	SortOrder        *SortOrder
	FilterType       *string
}

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
	Pages          []string
}

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 PostProcessOptions

type PostProcessOptions struct {
	// Whether or not Thunder should apply filtering on the output. This flag can
	// be used by externally managed resolvers when they decide to fall back to Thunder's
	// filtering based on the query arguments.
	ApplyTextFilter bool
	// Whether or not Thunder should set the Page Info's fields based on the output.
	// An exception to this are start and end cursors, which will be set by Thunder
	// regardless of the SetPageInfo flag.
	SetPageInfo bool
}

PostProcessOptions is used to instruct Thunder to perform additional operations on the output in the case of manually paginated resolvers. This struct should be returned as the second result from a manually-paginated field func.

type SafeBatchNodesToKeep

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

type Schema

type Schema struct {
	Name string
	// 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 NewSchemaWithName

func NewSchemaWithName(name string) *Schema

NewSchema creates a new schema with a schema name

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{}, options ...ObjectOption) *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 SortOrder

type SortOrder int64
const (
	SortOrder_Ascending SortOrder = iota
	SortOrder_Descending
)

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.

type UseFallbackFlag

type UseFallbackFlag func(context.Context) bool

Jump to

Keyboard shortcuts

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