graphql

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ActionGet    = "get"
	ActionList   = "list"
	ActionUpdate = "update"
	ActionDelete = "delete"
	ActionInsert = "insert"
)

Constants for resolver actions

Variables

View Source
var (
	ErrFieldHasNoName            = errors.New("fields must have a 'name' attribute")
	ErrFieldHasBadTypeDefinition = errors.New("field has bad type definition, must be <type> or [<type>]")
	ErrTypeAndResolver           = errors.New("field cannot declare Type and Resolver")
)

Custom errors

View Source
var GeneratedFilesPath = "./generated"

GeneratedFilesPath defines where to put files created from parsing the schema

Functions

This section is empty.

Types

type DynamoKeyType

type DynamoKeyType struct {
	Name string `yaml:"name"`
	Type string `yaml:"type"`
}

DynamoKeyType represents a key with type

type DynamoSource

type DynamoSource struct {
	HashKey *DynamoKeyType `yaml:"hash_key"`
	SortKey *DynamoKeyType `yaml:"sort_key,omitempty"`
	Backup  bool           `yaml:"backup,omitempty"`
}

DynamoSource represents a dynamo db data source

type Enum

type Enum struct {
	Name   string   `yaml:"name"`
	Values []string `yaml:"values"`
}

Enum represents a graphql enumeration definition

type Field

type Field struct {
	Name string

	// (Optional) Type defines the scalar or object type of the field. If a type is not
	// specified, it will default to the `String` scalar type if not set and
	// Resolver has not been specified
	// Must specify exactly one of Type OR Resolver
	Type *FieldType

	// (Optional) Only applicable when used as a field is used as a key in
	// a nested resolver. It gives the name of the field in the parent to
	// be used as the key to the nested data source.
	Parent string

	// (Optional) Define a resolver to fetch the value for this field
	// Resolver *Resolver `yaml:"resolver,omitempty"`
	// Must specify exactly one of Type OR Resolver
	Resolver *Resolver

	// (Optional) Type to be used when this field is included in an input
	// object definition if it differs from the main defined type
	InputType *FieldType
}

Field represents a field of a graphql object

func (*Field) IsLegalScalarType

func (f *Field) IsLegalScalarType() bool

IsLegalScalarType tests whether a the field is defined with one of the allowable graphql (and AWS) scalar types

func (*Field) UnmarshalYAML

func (f *Field) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML satisfies the custom unmarshaler interface for go-yaml. It's called automatically by the unmarshaler to ensure default values get set where they haven't been supplied in the user's definition.

type FieldType

type FieldType struct {
	Name        string
	IsList      bool
	NonNullable bool
}

FieldType represents the type associated with a field.

func (*FieldType) UnmarshalYAML

func (ft *FieldType) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML satisfies the custom unmarshaler interface for go-yaml. It is called automatically by the YAML unmarshal to deal with the [] list notation in field type definitions as they need to be read as scalar values, not YAML lists.

type FilterObject

type FilterObject Object

FilterObject is generated from a base object and used to filter queries

func NewFilterFromObject

func NewFilterFromObject(o *Object) *FilterObject

NewFilterFromObject creates a new filter object input type from an existing Object definition. Fields that can't be (currently) filtered (custom types and embedded objects) are omitted. Non-standard, but mappable, types are translated to simple scalar types

type FilterObjectList

type FilterObjectList []*FilterObject

FilterObjectList is a list type containing FilterObjects

type InputObject

type InputObject Object

InputObject is generated from a base object and used for mutation queries

func NewInputFromObject

func NewInputFromObject(o *Object, action string) (*InputObject, error)

NewInputFromObject creates a new input object type from an existing Object definition. Fields that can't be (currently) filtered (custom types and embedded objects) are omitted. Non-standard, but mappable, types are translated to simple scalar types

type InputObjectList

type InputObjectList []*InputObject

InputObjectList is a list type containing InputObjects

type Mutation

type Mutation Query

Mutation is a mutation query type

type Object

type Object struct {
	Name   string   `yaml:"name"`
	Fields []*Field `yaml:"fields"`
}

Object represents a graphql object type definition

type Query

type Query struct {
	Name     string     `yaml:"name"`
	Type     *FieldType `yaml:"type"`
	Resolver *Resolver  `yaml:"resolver"`
}

Query represents the definition for a graphql query

Query {
- name: <query_name>
  type: <return_type>
 resolver:
    action: <get|list|insert|update>
    source: <source_name> # optional
...
}

type Resolver

type Resolver struct {
	// The Action for the resolver to perform - get, list, insert, update, delete
	Action string `yaml:"action"`

	// The graphql type that the resolver returns. Where this resolver action
	// is other than get or delete, set this to be the base object type.
	// Appropriate "Input" variants of the object will then be defined.
	// Where the action is "list", the base type will also have an associated
	// "Connection" object defined.
	// Type string `yaml:"type"`
	Type *FieldType `yaml:"type"`

	// Set of fields to be used as keys in the query (primary key etc)
	KeyFields []*Field `yaml:"keyFields"`

	// Name of the datasource to be used for this resolver. Must have
	// been defined in the `sources` section of the manifest
	SourceKey string `yaml:"source"`

	// The below are set automatically as the schema is parsed. They should
	// not be included in the manifest YAML.
	DataSource *Source // Key to a datasource defined in the manifest
	ArgsSource string  // $ctx.{{ArgSource}}.get() - "args" or "source"
	Parent     string  // Parent field
	FieldName  string  // Field name attached to
}

Resolver is the represenatation of a field resolver

func (*Resolver) GenerateBytes

func (r *Resolver) GenerateBytes() ([]byte, error)

GenerateBytes renders the resolver ready to be written to an output stream

func (*Resolver) KeyFieldArgsString

func (r *Resolver) KeyFieldArgsString() string

KeyFieldArgsString returns the keyfield names and types in a string format suitable to be used as the arguments list in a resolver defintion in the schemea

func (*Resolver) KeyFieldJSONList

func (r *Resolver) KeyFieldJSONList() string

KeyFieldJSONList converts the `KeyFields` into a JSON formatted list of names

func (*Resolver) KeyFieldJSONMap

func (r *Resolver) KeyFieldJSONMap() string

KeyFieldJSONMap converts the `KeyFields` into a JSON formatted map

func (*Resolver) OutputName

func (r *Resolver) OutputName() string

OutputName returns the file name to be written for the resolver

type SQLSource

type SQLSource struct {
	PrimaryKey string `yaml:"primary_key"`
}

SQLSource represents a sql based db data source

type Schema

type Schema struct {
	Enums     []*Enum     `yaml:"enums"`
	Objects   []*Object   `yaml:"objects"`
	Queries   []*Query    `yaml:"queries"`
	Mutations []*Mutation `yaml:"mutations"`

	Sources map[string]*Source `yaml:"sources"`

	// Automatically populated to create
	// filtering options for list types
	FilterInputs []string

	// Connection objects to be built - populated automatically by "list" resolvers
	Connections   []string
	FilterObjects FilterObjectList
	InputObjects  InputObjectList

	// Contains any errors raised during the generation process
	Errors []error
	// contains filtered or unexported fields
}

Schema represents the elements of a graphql schema

func NewSchemaFromManifest

func NewSchemaFromManifest(manifest []byte) (*Schema, error)

NewSchemaFromManifest parses a schema manifest in YAML format and generates a new schema struct

func (*Schema) AddFilterFromObject

func (s *Schema) AddFilterFromObject(o *Object)

AddFilterFromObject adds a new filter object definition to the schema

func (*Schema) AddInputFromObject

func (s *Schema) AddInputFromObject(o *Object, action string) error

AddInputFromObject adds a new filter object definition to the schema

func (*Schema) CleanOutput

func (s *Schema) CleanOutput() error

CleanOutput empties the output path

func (*Schema) GenerateBytes

func (s *Schema) GenerateBytes() ([]byte, error)

GenerateBytes renders the schema to a bytes buffer ready to be writen to an output stream

func (*Schema) OutputName

func (s *Schema) OutputName() string

OutputName returns the file name to be written for the schema

func (*Schema) WriteAll

func (s *Schema) WriteAll() error

WriteAll outputs the generated public schema and any resolver files to the location given by `GeneratedFilesPath`

type Source

type Source struct {
	Name   string        `yaml:"name"`
	Dynamo *DynamoSource `yaml:"dynamo"`
	SQL    *SQLSource    `yaml:"sql"`

	// Set automatically
	Type string
}

Source is a datasource configuration

func (*Source) GenerateBytes

func (ds *Source) GenerateBytes() ([]byte, error)

GenerateBytes renders the datasource ready to be written to the output stream

func (*Source) OutputName

func (ds *Source) OutputName() string

OutputName returns the file name to be written for the data source

func (*Source) UnmarshalYAML

func (ds *Source) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML satisfies the custom unmarshaler interface for go-yaml. It is called automatically by the YAML unmarshal.

Jump to

Keyboard shortcuts

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