types.go

 1package graphql
 2
 3import (
 4	"fmt"
 5
 6	"github.com/graphql-go/graphql"
 7	"github.com/graphql-go/graphql/language/ast"
 8
 9	"github.com/MichaelMure/git-bug/bug"
10)
11
12func coerceString(value interface{}) interface{} {
13	if v, ok := value.(*string); ok {
14		return *v
15	}
16	return fmt.Sprintf("%v", value)
17}
18
19var bugIdScalar = graphql.NewScalar(graphql.ScalarConfig{
20	Name:        "BugID",
21	Description: "TODO",
22	Serialize:   coerceString,
23	ParseValue:  coerceString,
24	ParseLiteral: func(valueAST ast.Value) interface{} {
25		switch valueAST := valueAST.(type) {
26		case *ast.StringValue:
27			return valueAST.Value
28		}
29		return nil
30	},
31})
32
33// Internally, it's the Snapshot
34var bugType = graphql.NewObject(graphql.ObjectConfig{
35	Name: "Bug",
36	Fields: graphql.Fields{
37		"id": &graphql.Field{
38			Type: bugIdScalar,
39			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
40				return p.Source.(bug.Snapshot).Id(), nil
41			},
42		},
43		"status": &graphql.Field{
44			Type: graphql.String,
45		},
46		"comments": &graphql.Field{
47			Type: graphql.NewList(commentType),
48		},
49		"labels": &graphql.Field{
50			Type: graphql.NewList(graphql.String),
51			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
52				return p.Source.(bug.Snapshot).Labels, nil
53			},
54		},
55		// TODO: operations
56	},
57})
58
59var commentType = graphql.NewObject(graphql.ObjectConfig{
60	Name: "Comment",
61	Fields: graphql.Fields{
62		"author": &graphql.Field{
63			Type: personType,
64		},
65		"message": &graphql.Field{
66			Type: graphql.String,
67		},
68		// TODO: time
69	},
70})
71
72var personType = graphql.NewObject(graphql.ObjectConfig{
73	Name: "Person",
74	Fields: graphql.Fields{
75		"name": &graphql.Field{
76			Type: graphql.String,
77		},
78		"email": &graphql.Field{
79			Type: graphql.String,
80		},
81	},
82})