tools

package module
v0.0.0-...-0e396c4 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2017 License: MIT Imports: 9 Imported by: 0

README

Generator GraphQL schema from Go-structs

#IN WORKING!

Based on https://github.com/graphql-go/graphql library

Build Status

Example

Star Wars, based on https://github.com/facebook/relay/blob/master/examples/star-wars/

Query's example

query Q1{ 
		rebels{
			id 
			name
			ships{
				edges{
					node{
						id
						name
					}
				}
			}
		} 
		empire{
			id 
			name
		}				 
	}

Mutation's example

mutation M1{ introduceShip(input:{shipName:"New shippy"}){
	ship{
		id
		name
	}
} }	

Schema declaration

type Node struct {
	Id string `json:"id" "graphql:id"`
}

func (n Node) IsInterface() bool {
	return true
}

type Faction struct {
	Node  `graphql:"interface"`
	Id    string         `json:"id" "graphql:id"`
	Name  string         `json:"name"`
	Ships ShipConnection `json:"ships"`
}
type Ship struct {
	Id   string `json:"id" graphql:"id"`
	Name string `json:"name"`
}
type ShipConnection struct {
	Edges    []ShipEdge `json:"edges"`
	PageInfo *PageInfo  `json:"pageInfo"`
}

type ShipEdge struct {
	Cursor *string `json:"cursor"`
	Node   Ship    `json:"node"`
}

type PageInfo struct {
	HasNextPage     *bool  `json:"hasNextPage"`
	HasPreviousPage *bool  `json:"hasPreviousPage"`
	StartCursor     string `json:"startCursor"`
	EndCursor       string `json:"endCursor"`
}
type Query struct {
	Rebels Faction `json:"rebels"`
	Empire Faction `json:"empire"`
	Node   Node    `json:"node"`
}
type QueryNodeArgs struct {
	Id string `json:"id" graphql:"id"`
}

func (q Query) ArgsForNode() QueryNodeArgs {
	return QueryNodeArgs{}
}

type IntroduceShipInput struct {
	ClientMutationId *string `json:"clientMutationId"`
	ShipName         *string `json:"shipName"`
	FactionId        *string `json:"factionId" graphql:"id"`
}

type IntroduceShipPayload struct {
	ClientMutationId *string `json:"clientMutationId"`
	Ship             Ship    `json:"ship"`
	Faction          Faction `json:"faction"`
}

type Mutation struct {
	IntroduceShip IntroduceShipPayload `json:"introduceShip"`
}
type MutationIntroduceShipArgs struct {
	Input *IntroduceShipInput `json:"input" graphql:"input"`
}

func (m Mutation) ArgsForIntroduceShip() MutationIntroduceShipArgs {
	return MutationIntroduceShipArgs{}
}

Resolve

router.Query("Query.Rebels", func(query Query) (Faction, error) {
	return GetFaction("1"), nil
})
router.Query("Query.Empire", func(query Query) (Faction, error) {
	return GetFaction("2"), nil
})
router.Query("Faction.Ships", func(faction Faction, p tools.ResolveParams) (*relay.Connection, error) {
	return relay.ConnectionFromArray(GetShips(faction.Id), relay.NewConnectionArguments(p.Params.Args)), nil
})
router.Query("Mutation.IntroduceShip", func(m Mutation, args MutationIntroduceShipArgs) (interface{}, error) {
	return IntroduceShipPayload{
		Ship: Ship{
			Id:   nextShipId,
			Name: *args.Input.ShipName,
		},
	}, nil
})

Documentation

Index

Constants

View Source
const (
	ArgTypeParams  = 1
	ArgTypeSource  = 2
	ArgTypeArgs    = 3
	ArgTypeContext = 4
)

Variables

This section is empty.

Functions

func MapToStruct

func MapToStruct(input interface{}, output interface{}) error

func ResolveGlobalId

func ResolveGlobalId(params ResolveParams) (interface{}, error)

Types

type FieldInfo

type FieldInfo struct {
	Name   string
	Type   reflect.Type
	Tag    reflect.StructTag
	Source interface{}
	Args   interface{}
	Path   string
}

type Generator

type Generator struct {
	Types    map[reflect.Type]graphql.Output
	Resolver Resolver
}

func NewGenerator

func NewGenerator(resolver Resolver) *Generator

func (*Generator) Generate

func (generator *Generator) Generate(typ interface{}) interface{}

func (*Generator) GenerateObject

func (generator *Generator) GenerateObject(typ interface{}) *graphql.Object

func (*Generator) ResolveType

func (generator *Generator) ResolveType(p graphql.ResolveTypeParams) *graphql.Object

type InvalidSourceError

type InvalidSourceError struct {
	RouterError
}

type NotFoundRoute

type NotFoundRoute struct {
	RouterError
}

type ResolveParams

type ResolveParams struct {
	FieldInfo FieldInfo
	Source    interface{}
	Args      interface{}
	Context   interface{}
	Params    graphql.ResolveParams
}

type Resolver

type Resolver interface {
	IsResolve(sourceType reflect.Type, field reflect.StructField) bool
	Resolve(FieldInfo, graphql.ResolveParams) (interface{}, error)
}

type RouteParams

type RouteParams struct {
	Args    []int
	Context reflect.Type
	Handle  interface{}
}

type Router

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

func NewRouter

func NewRouter() *Router

func (*Router) IsResolve

func (r *Router) IsResolve(sourceType reflect.Type, field reflect.StructField) bool

func (*Router) Mutation

func (r *Router) Mutation(name string, handle interface{})

func (*Router) Query

func (r *Router) Query(path string, handle interface{})

func (*Router) Resolve

func (r *Router) Resolve(fieldInfo FieldInfo, p graphql.ResolveParams) (interface{}, error)

func (*Router) ResolveQuery

func (r *Router) ResolveQuery(fieldInfo FieldInfo, p graphql.ResolveParams) (interface{}, error)

func (*Router) Routes

func (r *Router) Routes() map[string]interface{}

func (*Router) SourceForResolve

func (r *Router) SourceForResolve(fieldInfo FieldInfo, p graphql.ResolveParams) (interface{}, error)

* *

func (*Router) Use

func (r *Router) Use(fn UseFn)

func (*Router) UseResolve

func (r *Router) UseResolve(name string, handle interface{})

type RouterError

type RouterError struct {
	Text string
}

func (RouterError) Error

func (e RouterError) Error() string

type UseFn

type UseFn func(params ResolveParams) (interface{}, bool, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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