aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/schema.go
blob: 7419e98a7df371705837c851be67e5a047cfce0c (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
package graphql

import (
	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/repository"
	"github.com/graphql-go/graphql"
)

func graphqlSchema() (graphql.Schema, error) {
	fields := graphql.Fields{
		"bug": &graphql.Field{
			Type: bugType,
			Args: graphql.FieldConfigArgument{
				"id": &graphql.ArgumentConfig{
					Type: bugIdScalar,
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				repo := p.Context.Value("repo").(repository.Repo)
				id, _ := p.Args["id"].(string)
				bug, err := bug.FindBug(repo, id)
				if err != nil {
					return nil, err
				}

				snapshot := bug.Compile()

				return snapshot, nil
			},
		},
		// TODO: provide a relay-like schema with pagination
		"allBugs": &graphql.Field{
			Type: graphql.NewList(bugType),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				repo := p.Context.Value("repo").(repository.Repo)
				ids, err := repo.ListRefs(bug.BugsRefPattern)

				if err != nil {
					return nil, err
				}

				var snapshots []bug.Snapshot

				for _, ref := range ids {
					bug, err := bug.ReadBug(repo, bug.BugsRefPattern+ref)

					if err != nil {
						return nil, err
					}

					snapshots = append(snapshots, bug.Compile())
				}

				return snapshots, nil
			},
		},
	}
	rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
	schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
	return graphql.NewSchema(schemaConfig)
}