-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
99 lines (88 loc) · 1.74 KB
/
request.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package monday
import (
"fmt"
"strings"
)
type Query struct {
Object string
Where Where
Select Queries
Wrap bool
}
type Queries []Query
func (q Query) String() string {
qParts := []string{q.Object}
if len(q.Where) > 0 {
qParts = append(qParts, q.Where.String())
}
if len(q.Select) == 0 {
return WrapQuery(strings.Join(qParts, " "), q.Wrap)
}
subfields := []string{}
for _, subfield := range q.Select {
subfields = append(subfields, subfield.String())
}
if len(subfields) > 0 {
qParts = append(qParts,
"{"+strings.Join(subfields, " ")+"}")
}
return WrapQuery(strings.Join(qParts, " "), q.Wrap)
}
type Where map[string]string
func (w Where) String() string {
if len(w) == 0 {
return ""
}
parts := []string{}
for k, v := range w {
parts = append(parts, k+":"+v)
}
return "(" + strings.Join(parts, ",") + ")"
}
func WrapQuery(gql string, wrap bool) string {
if wrap {
return fmt.Sprintf("query {%s}", gql)
}
return gql
}
func BoardQuery(boardId string) Query {
return Query{
Wrap: true,
Object: "boards",
Where: map[string]string{
"ids": boardId},
Select: Queries{
{Object: "name"},
{Object: "state"},
{Object: "columns", Select: Queries{
{Object: "id"},
{Object: "title"},
{Object: "type"},
}},
{Object: "owner", Select: Queries{
{Object: "id"},
}},
{Object: "items", Select: Queries{
{Object: "id"},
{Object: "name"},
{Object: "state"},
{Object: "column_values", Select: Queries{
{Object: "title"},
{Object: "id"},
{Object: "value"},
{Object: "text"},
}},
}},
},
}
}
/*
gql := "query {
boards (ids: 12345) {
name
columns { id title type }
owner {id}
items{id name state column_values {title id value text } } state
}
}"
*/