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		"title": &graphql.Field{
47			Type: graphql.String,
48		},
49		"comments": &graphql.Field{
50			Type: graphql.NewList(commentType),
51		},
52		"labels": &graphql.Field{
53			Type: graphql.NewList(graphql.String),
54			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
55				return p.Source.(bug.Snapshot).Labels, nil
56			},
57		},
58		// TODO: operations
59	},
60})
61
62var commentType = graphql.NewObject(graphql.ObjectConfig{
63	Name: "Comment",
64	Fields: graphql.Fields{
65		"author": &graphql.Field{
66			Type: personType,
67		},
68		"message": &graphql.Field{
69			Type: graphql.String,
70		},
71		// TODO: time
72	},
73})
74
75var personType = graphql.NewObject(graphql.ObjectConfig{
76	Name: "Person",
77	Fields: graphql.Fields{
78		"name": &graphql.Field{
79			Type: graphql.String,
80		},
81		"email": &graphql.Field{
82			Type: graphql.String,
83		},
84	},
85})