schemabuilder

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterScalar

func RegisterScalar(typ reflect.Type, name string, uf UnmarshalFunc) error

RegisterScalar is used to register custom scalars.

For example, to register a custom ID type,

type ID struct {
		Value string
}

Implement JSON Marshalling

func (id ID) MarshalJSON() ([]byte, error) {
 return strconv.AppendQuote(nil, string(id.Value)), nil
}

Register unmarshal func

func init() {
	typ := reflect.TypeOf((*ID)(nil)).Elem()
	if err := schemabuilder.RegisterScalar(typ, "ID", func(value interface{}, d reflect.Value) error {
		v, ok := value.(string)
		if !ok {
			return errors.New("not a string type")
		}

		d.Field(0).SetString(v)
		return nil
	}); err != nil {
		panic(err)
	}
}

Types

type Bytes

type Bytes struct {
	Value []byte
}

Bytes handles the duration

func (Bytes) MarshalJSON

func (b Bytes) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON Marshalling used to generate the output

type Duration

type Duration duration.Duration

Duration handles the duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON Marshalling used to generate the output

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 ID

type ID struct {
	Value string
}

ID is the graphql ID scalar

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON Marshalling used to generate the output

type InputObject

type InputObject struct {
	Name   string
	Type   interface{}
	Fields map[string]interface{}
}

InputObject represents the input objects passed in queries,mutations and subscriptions

func (*InputObject) FieldFunc

func (io *InputObject) FieldFunc(name string, function interface{})

FieldFunc is used to expose the fields of an input object and determine the method to fill it

type ServiceProvider struct {
	Id                   string
	FirstName            string
}

inputObj := schema.InputObject("serviceProvider", ServiceProvider{})

inputObj.FieldFunc("id", func(target *ServiceProvider, source *schemabuilder.ID) {
	target.Id = source.Value
})
inputObj.FieldFunc("firstName", func(target *ServiceProvider, source *string) {
	target.FirstName = *source
})

The target variable of the function should be pointer

type Interface

type Interface struct{}

Interface is a special marker struct that can be embedded into to denote that a type should be treated as a interface type by the schemabuilder

type InterfaceObj

type InterfaceObj struct {
	Struct reflect.Type
	Type   interface{}
}

InterfaceObj is a representation of graphql interface

type Map

type Map struct {
	Value string
}

Map handles maps

func (Map) MarshalJSON

func (m Map) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON Marshalling used to generate the output

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
}

Object - an 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{})

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

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", "Query" and "Subscription" 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, Mutation and Subscription starting points and builds a full graphql.Schema We can use graphql.Schema to execute and run queries. Essentially we read through all the methods we've attached to our Query, Mutation and Subscription Objects and ensure that those functions are returning other Objects that we can resolve in our GraphQL graph.

func (*Schema) Clone

func (s *Schema) Clone() *Schema

Clone creates a deep copy of schema and panics if it fails

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

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

GetObject gets a registered object. It is used to create linkings between different objects

func (*Schema) InputObject

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

InputObject registers a struct as inout object which can be passed as an argument to a query or mutation We'll read through the fields of the struct and create argument parsers to fill the data from graphQL JSON input

func (*Schema) MustBuild

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

MustBuild 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 mutation 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.

func (*Schema) Subscription

func (s *Schema) Subscription() *Object

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

type Subscription

type Subscription struct {
	Payload []byte
}

type Timestamp

type Timestamp timestamp.Timestamp

Timestamp handles the time

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON Marshalling used to generate the output

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 UnmarshalFunc

type UnmarshalFunc func(value interface{}, dest reflect.Value) error

UnmarshalFunc is used to unmarshal scalar value from JSON

Jump to

Keyboard shortcuts

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