types

package
Version: v0.0.0-...-c118757 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CustomIDType = graphql.NewScalar(graphql.ScalarConfig{
	Name:        "ID",
	Description: "UUID for ID type",
	Serialize: func(value interface{}) interface{} {
		switch value := value.(type) {
		case uuid.UUID:
			return value.String()
		case *uuid.UUID:
			v := *value
			return v.String()
		default:
			return NewParseError(errors.New(`No value to parse`))
		}
	},
	ParseValue: func(value interface{}) interface{} {
		switch value := value.(type) {
		case string:
			id, err := uuid.FromString(value)
			if err != nil {
				log.Println(NewParseError(err))
			}
			return id
		case *string:
			s := *value
			id, err := uuid.FromString(s)
			if err != nil {
				log.Println("Error parsing UUID string", err)
			}
			return id
		default:
			return NewParseError(errors.New(`No value to parse`))
		}
	},
	ParseLiteral: func(valueAST ast.Value) interface{} {
		switch valueAST := valueAST.(type) {
		case *ast.StringValue:
			id, err := uuid.FromString(valueAST.Value)
			if err != nil {
				log.Println(NewParseError(err))
			}
			return id
		default:
			return NewParseError(errors.New(`No value to parse`))
		}
	},
})

CustomIDType is used so graphql actually parses and uses UUID style strings

View Source
var GraphqlDateType = graphql.NewScalar(graphql.ScalarConfig{
	Name:        "Date",
	Description: "Custom date type",
	Serialize: func(value interface{}) interface{} {
		switch value := value.(type) {
		case Date:
			v := &value
			return v.GetTime().String()
		case *Date:
			return value.GetTime().String()
		default:
			return nil
		}
	},
	ParseValue: func(value interface{}) interface{} {
		var d Date
		switch value := value.(type) {
		case time.Time:
			d.Set(value)
			return d
		case *time.Time:
			d.Set(*value)
			return d
		default:
			return NewParseError(errors.New(`No date to parse`))
		}
	},
	ParseLiteral: func(valueAST ast.Value) interface{} {
		switch valueAST := valueAST.(type) {
		case *ast.StringValue:
			t, err := time.Parse(time.RFC3339, valueAST.Value)
			if err != nil {
				log.Println(NewParseError(err))
			}
			var d Date
			d.Set(t)
			return d
		default:
			return NewParseError(errors.New(`Unable to parse given value`))
		}
	},
})

GraphqlDateType Defines the graphql scalar definition for Dates

View Source
var TokenType = graphql.NewScalar(graphql.ScalarConfig{
	Name:        `Token`,
	Description: `Auth token for Users`,
	Serialize: func(value interface{}) interface{} {
		switch value := value.(type) {
		case uuid.UUID:
			return value.String()
		case *uuid.UUID:
			v := *value
			return v.String()
		default:
			return NewParseError(errors.New(`Value is not in uuid format`))
		}
	},
	ParseValue: func(value interface{}) interface{} {
		switch value := value.(type) {
		case string:
			token, err := uuid.FromString(value)
			if err != nil {
				log.Println(NewParseError(err))
			}
			return token
		case *string:
			token, err := uuid.FromString(*value)
			if err != nil {
				log.Println("Error parsing UUID string", err)
			}
			return token
		default:
			return NewParseError(errors.New(`Value is not in uuid format`))
		}
	},
	ParseLiteral: func(valueAST ast.Value) interface{} {
		switch valueAST := valueAST.(type) {
		case *ast.StringValue:
			id, err := uuid.FromString(valueAST.Value)
			if err != nil {
				log.Println(NewParseError(err))
			}
			return id
		default:
			return NewParseError(errors.New(`Value is not in uuid format`))
		}
	},
})

TokenType is used to store user auth tokens, which are basically just UUIDs

Functions

This section is empty.

Types

type Date

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

Date type for use with go-gallery

func (*Date) GetTime

func (d *Date) GetTime() time.Time

GetTime returns the underlying time object

func (*Date) Set

func (d *Date) Set(t time.Time)

Set returns the underlying time object

type ParseError

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

ParseError is an error used when parsing custome graphql types

func NewParseError

func NewParseError(err error) *ParseError

NewParseError creates a new parsing error

func (*ParseError) Error

func (p *ParseError) Error() string

Error fullfills the Error interface

Jump to

Keyboard shortcuts

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