-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.go
175 lines (160 loc) · 4.52 KB
/
multi.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
package shopify
import (
"fmt"
"reflect"
"regexp"
"strings"
)
var (
reOperationArgs = regexp.MustCompile(`([\w!]+)\s*:\s*([\w!]+)`)
)
type (
Multi struct {
operationType string
items []*MultiItem
}
MultiItem struct {
multi *Multi
operation string
opArgTypes [][]string
bodyType string
body string
inputs []interface{}
targets []interface{}
}
)
// NewMulti creates a chain to define operations and output destinations,
// allowing to generate one single GraphQL query to execute multiple GraphQL
// queries or mutations at the same time.
func NewMulti(operationType string) *Multi {
return &Multi{
operationType: operationType,
}
}
// Add defines a new operation with its argument types. The operation argument
// types must be in the format of "name: type".
func (m *Multi) Add(operation string, operationArgTypes ...string) *MultiItem {
argTypes := reOperationArgs.FindAllStringSubmatch(strings.Join(operationArgTypes, ", "), -1)
mi := &MultiItem{
multi: m,
operation: operation,
opArgTypes: argTypes,
}
m.items = append(m.items, mi)
return mi
}
// Do generates gql, args and targets that can be used in Client.New() and
// Request.Do().
func (m *Multi) Do() (gql string, args, targets []interface{}) {
var gqlIns []string
var ops []string
var fragments []string
c, d, f := 0, 0, 0
for _, item := range m.items {
x := 0
body := item.body
if item.bodyType != "" {
fragments = append(fragments, fmt.Sprintf("fragment frag%d on %s %s\n", f, item.bodyType, body))
body = fmt.Sprintf("{ ...frag%d }", f)
f += 1
}
var numberOfItems int
if len(item.opArgTypes) > 0 {
numberOfItems = len(item.inputs) / len(item.opArgTypes)
}
if numberOfItems < 1 {
numberOfItems = 1
}
for i := 0; i < numberOfItems; i++ {
var opIns []string
for j := range item.opArgTypes {
name := item.opArgTypes[j][1]
typ := item.opArgTypes[j][2]
opIns = append(opIns, fmt.Sprintf("%s: $%s%d", name, name, c))
gqlIns = append(gqlIns, fmt.Sprintf("$%s%d: %s", name, c, typ))
args = append(args, fmt.Sprintf("%s%d", name, c), item.inputs[x])
x += 1
}
opIn := strings.Join(opIns, ", ")
if opIn != "" {
opIn = "(" + opIn + ")"
}
ops = append(ops, fmt.Sprintf("gql%d: %s%s %s\n", c, item.operation, opIn, body))
c += 1
}
for n := 0; n < len(item.targets)/2; n++ {
path, ok := item.targets[2*n+1].(string)
if !ok {
continue
}
rv := reflect.Indirect(reflect.ValueOf(item.targets[2*n]))
if rv.Kind() == reflect.Slice {
if elemType := rv.Type().Elem(); elemType.Kind() == reflect.Slice { // [][]type
rv.Set(reflect.MakeSlice(reflect.SliceOf(elemType), numberOfItems, numberOfItems))
for i := 0; i < numberOfItems; i++ {
targets = append(targets, rv.Index(i).Addr().Interface(), fmt.Sprintf("gql%d%s", d+i, path))
}
continue
}
}
targets = append(targets, item.targets[2*n], fmt.Sprintf("gql%d%s", d+numberOfItems-1, path))
continue
}
d += numberOfItems
}
gqlIn := strings.Join(gqlIns, ", ")
if gqlIn != "" {
gqlIn = "(" + gqlIn + ")"
}
gql = strings.Join(fragments, "") + m.operationType + gqlIn + " {\n" + strings.Join(ops, "") + "}"
return
}
// Len returns number of items in the chain.
func (m Multi) Len() int {
return len(m.items)
}
func (m Multi) String() string {
var list []string
for i := range m.items {
list = append(list, m.items[i].String())
}
return strings.Join(list, "\n")
}
// Return defines output fields of a operation. This is the body of the GraphQL
// query. If the body starts with corresponding type, then fragment will be
// used.
func (mi *MultiItem) Return(body string) *MultiItem {
var bodyType string
if i := strings.Index(body, "{"); i > -1 {
bodyType = strings.TrimSpace(body[0:i])
body = body[i:]
}
mi.bodyType = bodyType
mi.body = body
return mi
}
// In puts input values in the order of the operation argument types to the chain.
func (mi *MultiItem) In(args ...interface{}) *MultiItem {
mi.inputs = args
return mi
}
// Out puts destinations to slices followed by a JSON path to the chain.
func (mi *MultiItem) Out(targets ...interface{}) *MultiItem {
mi.targets = targets
return mi
}
// Self returns the chain.
func (mi *MultiItem) Self() *Multi {
return mi.multi
}
func (mi MultiItem) String() string {
var list []string
for i := range mi.opArgTypes {
list = append(list, mi.opArgTypes[i][1]+": "+mi.opArgTypes[i][2])
}
args := strings.Join(list, ", ")
if args != "" {
args = "(" + args + ")"
}
return mi.operation + args + " " + mi.body
}