-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresolvers.js
60 lines (54 loc) · 1.21 KB
/
resolvers.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
let data = require('@begin/data')
let xss = require('xss')
module.exports = {
account,
draft,
drafts,
save,
destroy
}
async function account (root, args, session) {
if (!session.account)
throw Error('not authorized')
let copy = session.account
delete copy.token
return copy
}
async function draft (root, args) {
return data.get({
table: 'drafts',
...args
})
}
async function drafts () {
return data.get({
table: 'drafts',
})
}
async function save (root, draft, session) {
if (!session.account)
throw Error('not authorized')
let required = [ 'title', 'body' ]// , 'author', 'avatar']
for (let param of required) {
if (!draft[param])
throw ReferenceError(`missing param ${param}`)
if (draft[param] && draft[param].length < 4)
throw RangeError(`${param} must be four or more characters`)
}
draft.author = session.account.name
draft.avatar = session.account.avatar
draft.title = xss(draft.title)
draft.body = xss(draft.body)
return data.set({
table: 'drafts',
...draft
})
}
async function destroy (root, draft, session) {
if (!session.account)
throw Error('not authorized')
return data.destroy({
table: 'drafts',
...draft
})
}