Documentation ¶
Index ¶
- func RegisterScalar(typ reflect.Type, name string, uf UnmarshalFunc) error
- type Bytes
- type Duration
- type EnumMapping
- type ID
- type InputObject
- type Interface
- type InterfaceObj
- type Map
- type Methods
- type Object
- type Schema
- func (s *Schema) Build() (*graphql.Schema, error)
- func (s *Schema) Clone() *Schema
- func (s *Schema) Enum(val interface{}, enumMap interface{})
- func (s *Schema) GetObject(name string, typ interface{}) (*Object, error)
- func (s *Schema) InputObject(name string, typ interface{}) *InputObject
- func (s *Schema) MustBuild() *graphql.Schema
- func (s *Schema) Mutation() *Object
- func (s *Schema) Object(name string, typ interface{}) *Object
- func (s *Schema) Query() *Object
- func (s *Schema) Subscription() *Object
- type Subscription
- type Timestamp
- type Union
- type UnmarshalFunc
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 ¶
MarshalJSON implements JSON Marshalling used to generate the output
type Duration ¶
Duration handles the duration
func (Duration) MarshalJSON ¶
MarshalJSON implements JSON Marshalling used to generate the output
type EnumMapping ¶
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 ¶
MarshalJSON implements JSON Marshalling used to generate the output
type InputObject ¶
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 ¶
InterfaceObj is a representation of graphql interface
type Map ¶
type Map struct {
Value string
}
Map handles maps
func (Map) MarshalJSON ¶
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 ¶
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 })
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 (*Schema) Build ¶
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) 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 ¶
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) Mutation ¶
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 ¶
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 ¶
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 ¶
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 ¶
Timestamp handles the time
func (Timestamp) MarshalJSON ¶
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 ¶
UnmarshalFunc is used to unmarshal scalar value from JSON