-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
query.go
177 lines (153 loc) · 3.93 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
//go:build wasm && js
package main
import (
"context"
"encoding/hex"
"errors"
"syscall/js"
"github.com/dosco/graphjin/core/v3"
)
func query(gj *core.GraphJin) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
var qa queryArgs
if err := processArgs(&qa, args, "query"); !err.IsUndefined() {
return err
}
c := context.TODO()
if qa.userID != nil {
c = context.WithValue(c, core.UserIDKey, qa.userID)
}
fn := func(resolve, reject js.Value) {
res, err := gj.GraphQL(c, qa.query, qa.vars, nil)
if err != nil {
reject.Invoke(toJSError(err))
} else {
resolve.Invoke(fromResult(res))
}
}
return toAwait(fn)
})
}
// func queryWithTx(gj *core.GraphJin) js.Func {
// return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
// var qa queryArgs
// if err := processArgsWithTx(&qa, args, "query"); !err.IsUndefined() {
// return err
// }
// c := context.TODO()
// if qa.userID != nil {
// c = context.WithValue(c, core.UserIDKey, qa.userID)
// }
// fn := func(resolve, reject js.Value) {
// res, err := gj.GraphQLTx(c, qa.conn, qa.query, qa.vars, nil)
// if err != nil {
// reject.Invoke(toJSError(err))
// } else {
// resolve.Invoke(fromResult(res))
// }
// }
// return toAwait(fn)
// })
// }
func queryByName(gj *core.GraphJin) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
var qa queryArgs
if err := processArgs(&qa, args, "name"); !err.IsUndefined() {
return err
}
c := context.TODO()
if qa.userID != nil {
c = context.WithValue(c, core.UserIDKey, qa.userID)
}
fn := func(resolve, reject js.Value) {
res, err := gj.GraphQLByName(c, qa.query, qa.vars, nil)
if err != nil {
reject.Invoke(toJSError(err))
} else {
resolve.Invoke(fromResult(res))
}
}
return toAwait(fn)
})
}
func subscribe(gj *core.GraphJin) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
var qa queryArgs
if err := processArgs(&qa, args, "query"); !err.IsUndefined() {
return err
}
c := context.TODO()
if qa.userID != nil {
c = context.WithValue(c, core.UserIDKey, qa.userID)
}
fn := func(resolve, reject js.Value) {
res, err := gj.Subscribe(c, qa.query, qa.vars, nil)
if err != nil {
reject.Invoke(toJSError(err))
} else {
resolve.Invoke(fromMember(res))
}
}
return toAwait(fn)
})
}
func subscribeByName(gj *core.GraphJin) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
var qa queryArgs
if err := processArgs(&qa, args, "name"); !err.IsUndefined() {
return err
}
c := context.TODO()
if qa.userID != nil {
c = context.WithValue(c, core.UserIDKey, qa.userID)
}
fn := func(resolve, reject js.Value) {
res, err := gj.SubscribeByName(c, qa.query, qa.vars, nil)
if err != nil {
reject.Invoke(toJSError(err))
} else {
resolve.Invoke(fromMember(res))
}
}
return toAwait(fn)
})
}
func fromResult(res *core.Result) map[string]interface{} {
sql := res.SQL()
sqlFn := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return sql
})
data := string(res.Data)
dataFn := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return js.Global().Get("JSON").Call("parse", data)
})
hash := res.Hash
hashFn := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return hex.EncodeToString(hash[:])
})
return map[string]interface{}{
"sql": sqlFn,
"data": dataFn,
"hash": hashFn,
"role": res.Role(),
}
}
func fromMember(m *core.Member) map[string]interface{} {
dataFn := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) == 0 || args[0].Type() != js.TypeFunction {
err := errors.New("callback argument missing")
return toJSError(err)
}
cb := args[0]
go func() {
for {
msg := <-m.Result
cb.Invoke(fromResult(msg))
}
}()
return nil
})
return map[string]interface{}{
"data": dataFn,
}
}