/
search.go
79 lines (71 loc) · 1.93 KB
/
search.go
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
package bugs
import (
"fmt"
"github.com/Isabelle-Dev/graphql/newhorizons"
"github.com/Isabelle-Dev/graphql/parse"
"github.com/graphql-go/graphql"
"github.com/jinzhu/gorm"
)
var searchBugObj *graphql.Object
// RootObject contains the main bug-related queries
func RootObject(db *gorm.DB) *graphql.Object {
if searchBugObj != nil {
return searchBugObj
}
// bug
searchBugObj = graphql.NewObject(graphql.ObjectConfig{
Name: "bugs",
Description: "bug-related query sources",
Fields: graphql.Fields{
"query": &graphql.Field{
Name: "query fields",
Type: graphql.NewNonNull(bugSearchObj),
Args: graphql.FieldConfigArgument{
"query": &graphql.ArgumentConfig{
Type: graphql.String,
},
"month": &graphql.ArgumentConfig{
Type: graphql.String,
DefaultValue: "all",
},
"glob": &graphql.ArgumentConfig{
Type: graphql.String,
DefaultValue: "f",
},
"limit": &graphql.ArgumentConfig{
Type: graphql.Int,
DefaultValue: 500,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
query := p.Args["query"].(string)
glob := p.Args["glob"].(string)
limit := p.Args["limit"].(int)
month := p.Args["month"].(string)
options := parse.QueryParse(query)
q := parse.BuildQuery(options, "bug", limit, glob)
entries := execute(q, db, month)
if entries == nil {
return nil, fmt.Errorf("query(): no entries found")
}
return entries, nil
},
},
},
})
return searchBugObj
}
var bugSearchObj = graphql.NewObject(graphql.ObjectConfig{
Name: "bug_search",
Fields: graphql.Fields{
"bugs": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(bug)),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if val, ok := p.Source.([]*newhorizons.Bug); ok {
return val, nil
}
return nil, fmt.Errorf("search_bug(): error")
},
},
},
})