-
Notifications
You must be signed in to change notification settings - Fork 14
/
query.go
209 lines (171 loc) · 4.18 KB
/
query.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package graphqlparser
import (
"bytes"
"encoding/json"
"io"
"log"
"reflect"
"sort"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const writeQueryDeepLimit = 10
func constructQuery(v interface{}, variables map[string]interface{}) string {
query := query(v)
if len(variables) > 0 {
return "query(" + queryArguments(variables) + ")" + query
}
return query
}
func constructMutation(v interface{}, variables map[string]interface{}) string {
query := query(v)
if len(variables) > 0 {
return "mutation(" + queryArguments(variables) + ")" + query
}
return "mutation" + query
}
func queryArguments(variables map[string]interface{}) string {
keys := make([]string, 0, len(variables))
for k := range variables {
keys = append(keys, k)
}
sort.Strings(keys)
var buf bytes.Buffer
for _, k := range keys {
_, err := io.WriteString(&buf, "$")
if err != nil {
panic(err)
}
_, err = io.WriteString(&buf, k)
if err != nil {
panic(err)
}
_, err = io.WriteString(&buf, ":")
if err != nil {
panic(err)
}
writeArgumentType(&buf, reflect.TypeOf(variables[k]), true)
}
return buf.String()
}
func writeArgumentType(w io.Writer, t reflect.Type, value bool) {
if t.Kind() == reflect.Ptr {
writeArgumentType(w, t.Elem(), false)
return
}
switch t.Kind() {
case reflect.Slice, reflect.Array:
_, err := io.WriteString(w, "[")
if err != nil {
panic(err)
}
writeArgumentType(w, t.Elem(), true)
_, err = io.WriteString(w, "]")
if err != nil {
panic(err)
}
default:
_, err := io.WriteString(w, cases.Title(language.AmericanEnglish, cases.NoLower).String(t.Name()))
if err != nil {
panic(err)
}
}
if value {
_, err := io.WriteString(w, "!")
if err != nil {
panic(err)
}
}
}
func query(v interface{}) string {
var buf bytes.Buffer
shownError := false
writeQuery(&buf, reflect.TypeOf(v), false, 0, &shownError)
return buf.String()
}
func writeQuery(w io.Writer, t reflect.Type, inline bool, depth uint, shownError *bool) {
switch t.Kind() {
case reflect.Ptr, reflect.Slice:
writeQuery(w, t.Elem(), false, depth, shownError)
case reflect.Struct:
if reflect.PtrTo(t).Implements(jsonUnmarshaler) {
return
}
if !inline {
_, err := io.WriteString(w, "{")
if err != nil {
panic(err)
}
}
skipComma := false
for i := 0; i < t.NumField(); i++ {
hasStruct := false
if depth > writeQueryDeepLimit {
if !*shownError {
*shownError = true
log.Println("You reached writeQueryDeepLimit constant")
}
continue
}
if i != 0 && !skipComma {
_, err := io.WriteString(w, ",")
if err != nil {
panic(err)
}
}
skipComma = false
f := t.Field(i)
value, ok := f.Tag.Lookup("graphql")
fieldName, hasJSON := f.Tag.Lookup("json")
inlineField := f.Anonymous && !ok
if !inlineField {
if ok {
_, err := io.WriteString(w, value)
if err != nil {
panic(err)
}
} else {
if f.Type.Kind() == reflect.Slice && f.Type.Elem().Kind() == reflect.Ptr &&
f.Type.Elem().Elem().Kind() == reflect.Struct ||
f.Type.Kind() == reflect.Slice && f.Type.Elem().Kind() == reflect.Struct ||
f.Type.Kind() == reflect.Ptr && f.Type.Elem().Kind() == reflect.Struct && f.Type.Elem().Name() != "Time" ||
f.Type.Kind() == reflect.Struct && f.Type.Name() != "Time" {
hasStruct = true
if depth+1 > writeQueryDeepLimit {
if !*shownError {
*shownError = true
log.Println("You reached writeQueryDeepLimit constant")
}
skipComma = true
continue
}
}
//io.WriteString(w, ident.ParseMixedCaps(f.Name).ToLowerCamelCase())
if hasJSON && fieldName != "-" {
_, err := io.WriteString(w, fieldName)
if err != nil {
panic(err)
}
} else {
_, err := io.WriteString(w, f.Name)
if err != nil {
panic(err)
}
}
}
}
if hasStruct {
writeQuery(w, f.Type, inlineField, depth+1, shownError)
} else {
writeQuery(w, f.Type, inlineField, depth, shownError)
}
}
if !inline {
_, err := io.WriteString(w, "}")
if err != nil {
panic(err)
}
}
}
}
var jsonUnmarshaler = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()