gql

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2019 License: MIT Imports: 11 Imported by: 2

README

gql (Work In Progress)

gql is a go package for building a GraphQL server, currently in alpha and there'll be updates that may break backward compatibility.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// String is a built-in type in GraphQL
	String = &Scalar{
		Name:        "String",
		Description: "This is the built-in String scalar",
		InputCoercion: func(s string) (interface{}, error) {
			if strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"") {
				t := s[1 : len(s)-1]
				if utf8.ValidString(t) {
					return t, nil
				}
			}
			return nil, fmt.Errorf("value '%s' couldn't be coerced as input for String", s)
		},
		OutputCoercion: func(v interface{}) ([]byte, error) {
			r := reflect.ValueOf(v)
			switch {
			case v == nil:
				return []byte("null"), nil
			case r.Kind() == reflect.String:
				if utf8.ValidString(r.String()) {
					return json.Marshal(r.String())
				}
			case r.Kind() == reflect.Bool:
				if r.Bool() {
					return []byte("true"), nil
				}
				return []byte("false"), nil
			case r.Kind() == reflect.Int || r.Kind() == reflect.Int16 || r.Kind() == reflect.Int32:
				return []byte(fmt.Sprintf(`"%v"`, r.Int())), nil
			case r.Kind() == reflect.Float32 || r.Kind() == reflect.Float64:
				return []byte(fmt.Sprintf(`"%v"`, r.Float())), nil
			}
			return nil, fmt.Errorf("value '%v' couldn't be coerced as output for String", v)
		},
	}
	// ID is a built-in type in GraphQL
	ID = &Scalar{
		Name:        "ID",
		Description: "This is the built-in ID scalar",
		InputCoercion: func(s string) (interface{}, error) {
			if strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"") {
				t := s[1 : len(s)-1]
				if utf8.ValidString(t) {
					return t, nil
				}
			}
			return nil, fmt.Errorf("value '%s' couldn't be coerced as input for String", s)
		},
		OutputCoercion: func(v interface{}) ([]byte, error) {
			r := reflect.ValueOf(v)
			switch {
			case v == nil:
				return []byte("null"), nil
			case r.Kind() == reflect.String:
				if utf8.ValidString(r.String()) {
					return json.Marshal(r.String())
				}
			case r.Kind() == reflect.Int || r.Kind() == reflect.Int16 || r.Kind() == reflect.Int32:
				return []byte(fmt.Sprintf(`"%v"`, r.Int())), nil
			}
			return nil, fmt.Errorf("value '%v' couldn't be coerced as output for String", v)
		},
	}
)

Functions

This section is empty.

Types

type Argument

type Argument struct {
	Name              string
	DefaultValue      interface{}
	Description       string
	Depricated        bool
	DepricationReason string
	Type              Type
}

Argument ...

type Directive added in v0.0.2

type Directive struct {
	Name      string
	Arguments []*Argument
}

Directive ..

type Enum added in v0.0.3

type Enum struct {
	Name        string
	Description string
	Values      EnumValues
}

func (*Enum) InputCoercion added in v0.0.3

func (e *Enum) InputCoercion(s string) (interface{}, error)

func (*Enum) Kind added in v0.0.3

func (e *Enum) Kind() TypeDefinition

func (*Enum) OutputCoercion added in v0.0.3

func (e *Enum) OutputCoercion(v interface{}) ([]byte, error)

func (*Enum) Unwrap added in v0.0.3

func (e *Enum) Unwrap() Type

func (*Enum) Validate added in v0.0.3

func (e *Enum) Validate(ctx *ValidationContext) error

type EnumValue added in v0.0.3

type EnumValue struct {
	Name        string
	Value       interface{}
	Description string
}

type EnumValues added in v0.0.3

type EnumValues []*EnumValue

type Field

type Field struct {
	Name              string
	Description       string
	Depricated        bool
	DepricationReason string
	Type              Type
	Arguments         []*Argument
	Resolver          ResolverFunc
}

Field ...

func (*Field) Validate added in v0.0.3

func (f *Field) Validate(ctx *ValidationContext) error

type Fields

type Fields []*Field

Fields ...

func (Fields) Get added in v0.0.2

func (fs Fields) Get(name string) (*Field, error)

Get returns the field with the given name (if exists)

type InputCoercion added in v0.0.2

type InputCoercion func(string) (interface{}, error)

InputCoercion func

type InputFields added in v0.0.3

type InputFields []*InputObjectField

InputFields contains InputObjectFields

func (InputFields) Get added in v0.0.3

func (i InputFields) Get(name string) *InputObjectField

Get returns the InputObjectField with the given name

type InputObject added in v0.0.3

type InputObject struct {
	Name        string
	Description string
	Fields      InputFields
}

InputObject ...

func (*InputObject) Kind added in v0.0.3

func (o *InputObject) Kind() TypeDefinition

Kind returns the kind of the type, which is InputObjectTypeDefinition

func (*InputObject) Unwrap added in v0.0.3

func (o *InputObject) Unwrap() Type

Unwrap is defined to implement the Type interface

func (*InputObject) Validate added in v0.0.3

func (o *InputObject) Validate(ctx *ValidationContext) error

Validate runs a check if everything is okay with the Type

type InputObjectField added in v0.0.3

type InputObjectField struct {
	Name        string
	Type        Type
	Description string
}

InputObjectField describes a field of an InputObject

type Interface added in v0.0.3

type Interface struct {
	Name        string
	Description string
	Fields      Fields
	ResolveType TypeResolver
}

Interface ...

func (*Interface) GetFields added in v0.0.3

func (i *Interface) GetFields() Fields

GetFields implemets the HasFields interface

func (*Interface) Kind added in v0.0.3

func (i *Interface) Kind() TypeDefinition

Kind returns InterfaceTypeDefinition

func (*Interface) Unwrap added in v0.0.3

func (i *Interface) Unwrap() Type

Unwrap returns nothing for an Interface

func (*Interface) Validate added in v0.0.3

func (i *Interface) Validate(ctx *ValidationContext) error

Validate the type

type List added in v0.0.2

type List struct {
	Type Type
}

List ...

func (*List) Kind added in v0.0.2

func (l *List) Kind() TypeDefinition

Kind ...

func (*List) Unwrap added in v0.0.2

func (l *List) Unwrap() Type

Unwrap ...

func (*List) Validate added in v0.0.3

func (l *List) Validate(ctx *ValidationContext) error

Validate ...

type NonNull added in v0.0.2

type NonNull struct {
	Type Type
}

NonNull ...

func (*NonNull) Kind added in v0.0.2

func (nn *NonNull) Kind() TypeDefinition

Kind ...

func (*NonNull) Unwrap added in v0.0.2

func (nn *NonNull) Unwrap() Type

Unwrap ...

func (*NonNull) Validate added in v0.0.3

func (nn *NonNull) Validate(ctx *ValidationContext) error

Validate ...

type Object

type Object struct {
	Name        string
	Description string
	Implements  []*Interface
	Fields      Fields
	// contains filtered or unexported fields
}

Object ...

func (*Object) GetFields added in v0.0.3

func (o *Object) GetFields() Fields

GetFields implements the HasFields interface

func (*Object) Kind added in v0.0.2

func (o *Object) Kind() TypeDefinition

Kind returns ObjectTypeDefinition

func (*Object) Unwrap added in v0.0.2

func (o *Object) Unwrap() Type

Unwrap returns nothing for an Object

func (*Object) Validate added in v0.0.3

func (o *Object) Validate(ctx *ValidationContext) error

Validate returns any error caused by invalid object definition

type OutputCoercion added in v0.0.2

type OutputCoercion func(interface{}) ([]byte, error)

OutputCoercion func

type Params added in v0.0.3

type Params struct {
	Ctx           context.Context        `json:"-"`
	Query         string                 `json:"query"`
	OperationName string                 `json:"operationName"`
	Variables     map[string]interface{} `json:"variables"`
}

Params ...

type ResolverFunc

type ResolverFunc func(context.Context, map[string]interface{}, interface{}) (interface{}, error)

ResolverFunc ..

type Result

type Result struct {
	Data   interface{} `json:"data"`
	Errors []error     `json:"errors"`
}

type Scalar

type Scalar struct {
	Name           string
	Description    string
	InputCoercion  InputCoercion
	OutputCoercion OutputCoercion
}

Scalar ...

func (*Scalar) Kind added in v0.0.2

func (s *Scalar) Kind() TypeDefinition

Kind ...

func (*Scalar) Unwrap added in v0.0.2

func (s *Scalar) Unwrap() Type

Unwrap ...

func (*Scalar) Validate added in v0.0.3

func (s *Scalar) Validate(ctx *ValidationContext) error

Validate validates the Type

type Schema

type Schema struct {
	Mutation    *Object
	Query       *Object
	Subsciption *Object
}

Schema ...

func (*Schema) Execute added in v0.0.3

func (s *Schema) Execute(p *Params) *Result

Execute an operation on the schema

type Type

type Type interface {
	Unwrap() Type
	Kind() TypeDefinition
	Validate(*ValidationContext) error
}

Type ...

func NewList added in v0.0.2

func NewList(t Type) Type

NewList ..

func NewNonNull added in v0.0.2

func NewNonNull(t Type) Type

NewNonNull ...

type TypeDefinition added in v0.0.2

type TypeDefinition int

TypeDefinition ...

const (
	// NonNullTypeDefinition NonNull type
	NonNullTypeDefinition TypeDefinition = iota
	// ListTypeDefinition List type
	ListTypeDefinition
	// ScalarTypeDefinition scalar type
	ScalarTypeDefinition
	// ObjectTypeDefinition object type
	ObjectTypeDefinition
	// InterfaceTypeDefinition interface type
	InterfaceTypeDefinition
	// UnionTypeDefinition union type
	UnionTypeDefinition
	// EnumTypeDefinition enum type
	EnumTypeDefinition
	// InputObjectTypeDefinition input object type
	InputObjectTypeDefinition
)

type TypeResolver added in v0.0.3

type TypeResolver func(ctx context.Context, value interface{}) *Object

TypeResolver resolves the Object for an Interface or Union based on the resolver's result

type ValidationContext added in v0.0.3

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

type Validator added in v0.0.3

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

func (*Validator) ValidateFieldsInSetCanMerge added in v0.0.3

func (v *Validator) ValidateFieldsInSetCanMerge(ctx *execCtx, o *Object, set []ast.Selection) []error

func (*Validator) ValidateLeafFieldSelections added in v0.0.3

func (v *Validator) ValidateLeafFieldSelections(ctx *execCtx, f *Field)

func (*Validator) ValidateOperations added in v0.0.3

func (v *Validator) ValidateOperations(ctx *execCtx) []error

func (*Validator) ValidateRequest added in v0.0.3

func (v *Validator) ValidateRequest(ctx *execCtx) []error

func (*Validator) ValidateSameResponseShape added in v0.0.3

func (v *Validator) ValidateSameResponseShape(ctx *execCtx, o *Object, fieldA *ast.Field, fieldB *ast.Field) bool

func (*Validator) ValidateSelectionSet added in v0.0.3

func (v *Validator) ValidateSelectionSet(ctx *execCtx, o *Object, set []ast.Selection) []error

func (*Validator) ValidateSelectionSetMerges added in v0.0.3

func (v *Validator) ValidateSelectionSetMerges(ctx *execCtx, o *Object, set []ast.Selection) []error

Directories

Path Synopsis
examples
language
ast
pkg
vm

Jump to

Keyboard shortcuts

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