1package graphql
2
3import (
4 "github.com/MichaelMure/git-bug/bug"
5 "github.com/MichaelMure/git-bug/repository"
6 "github.com/graphql-go/graphql"
7)
8
9func graphqlSchema() (graphql.Schema, error) {
10 fields := graphql.Fields{
11 "bug": &graphql.Field{
12 Type: bugType,
13 Args: graphql.FieldConfigArgument{
14 "id": &graphql.ArgumentConfig{
15 Type: bugIdScalar,
16 },
17 },
18 Resolve: func(p graphql.ResolveParams) (interface{}, error) {
19 repo := p.Context.Value("repo").(repository.Repo)
20 id, _ := p.Args["id"].(string)
21 b, err := bug.FindLocalBug(repo, id)
22 if err != nil {
23 return nil, err
24 }
25
26 snapshot := b.Compile()
27
28 return snapshot, nil
29 },
30 },
31 // TODO: provide a relay-like schema with pagination
32 "allBugs": &graphql.Field{
33 Type: graphql.NewList(bugType),
34 Resolve: func(p graphql.ResolveParams) (interface{}, error) {
35 repo := p.Context.Value("repo").(repository.Repo)
36
37 var snapshots []bug.Snapshot
38
39 for sb := range bug.ReadAllLocalBugs(repo) {
40 if sb.Err != nil {
41 return nil, sb.Err
42 }
43
44 snapshots = append(snapshots, sb.Bug.Compile())
45 }
46
47 return snapshots, nil
48 },
49 },
50 }
51 rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
52 schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
53 return graphql.NewSchema(schemaConfig)
54}