-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
53 lines (44 loc) · 765 Bytes
/
query.go
File metadata and controls
53 lines (44 loc) · 765 Bytes
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
package graphqb
import "strings"
const (
qComma = ","
qLB = "{"
qRB = "}"
qLP = "("
qRP = ")"
qColumn = ":"
qSpace = " "
)
type Query struct {
Type string
Name string
Fields []*Field
Err error
}
func NewQuery(qt string) *Query {
return &Query{Type: qt}
}
func (q *Query) Stringify() string {
var sb strings.Builder
// default is query
if len(q.Type) > 0 {
sb.WriteString(q.Type)
}
if len(q.Name) > 0 {
sb.WriteString(qSpace)
sb.WriteString(q.Name)
}
sb.WriteString(qLB)
for k, field := range q.Fields {
if k != 0 {
sb.WriteString(qComma)
}
sb.WriteString(field.Stringify())
}
sb.WriteString(qRB)
return sb.String()
}
func (q *Query) SetFields(fs ...*Field) *Query {
q.Fields = fs
return q
}