aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/types.go
blob: a320ca39ff777734f6f166c5d7f2677a271c33d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package graphql

import (
	"fmt"

	"github.com/graphql-go/graphql"
	"github.com/graphql-go/graphql/language/ast"

	"github.com/MichaelMure/git-bug/bug"
)

func coerceString(value interface{}) interface{} {
	if v, ok := value.(*string); ok {
		return *v
	}
	return fmt.Sprintf("%v", value)
}

var bugIdScalar = graphql.NewScalar(graphql.ScalarConfig{
	Name:        "BugID",
	Description: "TODO",
	Serialize:   coerceString,
	ParseValue:  coerceString,
	ParseLiteral: func(valueAST ast.Value) interface{} {
		switch valueAST := valueAST.(type) {
		case *ast.StringValue:
			return valueAST.Value
		}
		return nil
	},
})

// Internally, it's the Snapshot
var bugType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Bug",
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: bugIdScalar,
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				return p.Source.(bug.Snapshot).Id(), nil
			},
		},
		"status": &graphql.Field{
			Type: graphql.String,
		},
		"comments": &graphql.Field{
			Type: graphql.NewList(commentType),
		},
		"labels": &graphql.Field{
			Type: graphql.NewList(graphql.String),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				return p.Source.(bug.Snapshot).Labels, nil
			},
		},
		// TODO: operations
	},
})

var commentType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Comment",
	Fields: graphql.Fields{
		"author": &graphql.Field{
			Type: personType,
		},
		"message": &graphql.Field{
			Type: graphql.String,
		},
		// TODO: time
	},
})

var personType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Person",
	Fields: graphql.Fields{
		"name": &graphql.Field{
			Type: graphql.String,
		},
		"email": &graphql.Field{
			Type: graphql.String,
		},
	},
})