-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
235 lines (217 loc) · 5.09 KB
/
index.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import GraphError from "./grapherror.js"
import Client from "./client.js"
const _stored = {}
class GraphQLClient {
static get GraphError() {
return GraphError
}
static get Client() {
return Client
}
/**
* Save to stored templates
* @param {Object} obj
*/
static Add(obj) {
Object.keys(obj).forEach(k => {
_stored[k] = obj[k]
})
}
/**
* Delete stored template
* @param {Array} keys
*/
static Remove(keys) {
keys.forEach(k => {
if (_stored[k])
delete _stored[k]
})
}
/**
*
* @param {*} config
* @returns
*/
static Init(config) {
GraphQLClient.Send = GraphQLClient.Run.bind(null, config)
return GraphQLClient
}
static ParseTemplate(template, data) {
let q = template
if (data)
q = JSON.parse(JSON.stringify(template, (k, v) => {
if (data.hasOwnProperty(v))
return data[v]
return v
}))
return q
}
/**
* @param {Object | String} str
* @param {Object} data
*/
static Get(value, data) {
const t = typeof value === "string" ? _stored[value] : value
return GraphQLClient.Do('query', GraphQLClient.ParseTemplate(t, data))
}
/**
*
* @param {Object | String} str
* @param {Object} data
*/
static Set(value, data) {
const t = typeof value === "string" ? _stored[value] : value
return GraphQLClient.Do('mutation', GraphQLClient.ParseTemplate(t, data))
}
/**
*
* @param {Object | String} str
* @param {Object} data
*/
static Sub(value, data) {
const t = typeof value === "string" ? _stored[value] : value
return GraphQLClient.Do('subscription', GraphQLClient.ParseTemplate(t, data))
}
/**
*
* @param {string} op : Operation
* @param {Object} q : Query
*/
static async Do(op, q) {
const query = GraphQLClient.Build(op, q)
try {
const result = await GraphQLClient.Send({ query }) //GraphQLClient.Send(JSON.stringify({ query }))
return result
} catch (error) {
if (!error.code)
return { error: { code: 400,message:error.message } } //no serverside error
return { error }
}
}
static Build(op, ...args) {
let result = `${op} { `
for (let i = 0; i < args.length; i++)
result += GraphQLClient.BuildObject(args[i])
return result + " }"
}
static BuildObject(obj) {
let result = ``
if (obj === null || obj === undefined) return result
if (typeof (obj) === "string" ||
typeof (obj) === "number" ||
Array.isArray(obj))
return JSON.stringify(obj)
Object.keys(obj).forEach((k) => {
result += k + " "
const values = obj[k]
const arg = values.find((element, index, array) => {
if (!element || !element.$args/* || typeof element !== "object"*/)
return false
return true
})
let acount = 0
if (arg) {
result += GraphQLClient.BuildArgs(arg.$args)
const ind = values.indexOf(arg)
values.splice(ind, 1)
}
result += values.length > acount ? " { " : ""
for (let i = 0; i < values.length; i++) {
const o = values[i]
if (!o)
continue
if (typeof (o) === "string") {
result += o + " "
} else if (o instanceof Array) {
result += o.join(' ')
} else
result += GraphQLClient.BuildObject(o)
}
result += values.length > acount ? " } " : ""
})
return result
}
static BuildArg(arg) {
let str = ''
if (arg === null)
arg = undefined
switch (typeof arg) {
case "object":
if (Array.isArray(arg)) {
const red = arg.reduce((acc, el) => {
return acc + GraphQLClient.BuildArg(el) + ' ,'
}, '')
str += `[${red.slice(0, -1)}]`
} else {
const keys = Object.keys(arg)
str += ` { `
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
str += `${k}:${GraphQLClient.BuildArg(arg[k])},`
}
str += ` } `
}
break
case "string":
if (arg.startsWith('$'))
str += `${arg.slice(1)}`
else
str += JSON.stringify(arg)
break
case "number":
str += arg
break
case "undefined":
str += 'null'
break
default:
str += JSON.stringify(arg)
break
}
return str
}
static BuildArgs(args) {
let result = " ( "
const keys = Object.keys(args)
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
const v = args[k]
result += `${k}:${GraphQLClient.BuildArg(v)},`
}
return result.slice(0, -1) + " ) "
}
/**
*
* @param {*} options
* @param {*} data
* @returns
*/
static async Run(options, d) {
const data = JSON.stringify(d)
try {
const resp = await fetch(options.url, {
...options,
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
...options.headers
},
body: data
})
const { data: value, error } = await resp.json()
if (error)
throw new GraphError(error)
return value
} catch (e) {
throw e
}
}
/**
* @param {*} data
* @returns
*/
static async Send() {
throw new Error(`GQLC.Send - Use Init before , or override Send`)
}
}
export default GraphQLClient