-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.js
127 lines (117 loc) · 4.13 KB
/
graphql.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
import { check, Match } from 'meteor/check'
import { Meteor } from 'meteor/meteor'
import { commentTypeDef } from './commentTypeDef'
import { CommentUserResolver, getCommentResolver } from './commentResolvers'
const CommentsCollection = Comments.getCollection()
const transformAnonData = data => data ? { _id: data.id, salt: data.salt } : data
const callCollectionMethod = (method, args, paramsArray, userId) => CommentsCollection[method](
...paramsArray.map(key => args[key]),
userId,
transformAnonData(args.anonData),
)
/**
* Generate type definitions and resolvers for the comments-ui pkg.
*
* @see https://github.com/komentify/meteor-comments-ui
*/
export const wrapTypeDefsAndResolvers = (opts) => {
let {
resolvers = {},
typeDefs = [],
getUserId = (context) => context.userId,
} = opts
if (!Array.isArray(typeDefs)) typeDefs = [typeDefs]
return {
typeDefs: [
...typeDefs,
commentTypeDef,
],
resolvers: {
...resolvers,
CommentUser: CommentUserResolver,
Comment: getCommentResolver(getUserId),
Query: {
...(resolvers.Query || {}),
getComment(_, args, context) {
const { id } = args
check(id, String)
return CommentsCollection.findOnePublic(id, getUserId(context))
},
listComments(_, args, context) {
const { referenceId, limit, skip } = args
check(referenceId, String)
return CommentsCollection
.findPublic({ referenceId }, getUserId(context), { limit, skip })
.fetch()
},
countComments(_, args, context) {
const { referenceId } = args
check(referenceId, String)
return CommentsCollection
.findPublic({ referenceId }, getUserId(context))
.count()
},
},
Mutation: {
...(resolvers.Mutation || {}),
addComment(_, args, context) {
return callCollectionMethod('addComment', args, [
'referenceId',
'content',
], getUserId(context))
},
editComment(_, args, context) {
return callCollectionMethod('editComment', args, [
'id',
'content',
], getUserId(context))
},
removeComment(_, args, context) {
return callCollectionMethod('removeComment', args, ['id'], getUserId(context))
},
likeComment: async (_, args, context) => {
await callCollectionMethod('likeComment', args, ['id'], getUserId(context))
return CommentsCollection.findOnePublic(args.id, getUserId(context))
},
dislikeComment: async (_, args, context) => {
await callCollectionMethod('dislikeComment', args, ['id'], getUserId(context))
return CommentsCollection.findOnePublic(args.id, getUserId(context))
},
starComment: async (_, args, context) => {
await callCollectionMethod('starComment', args, ['id', 'starsCount'], getUserId(context))
return CommentsCollection.findOnePublic(args.id, getUserId(context))
},
addReply(_, args, context) {
return callCollectionMethod('addReply', args, [
'rootId',
'replyId',
'content',
], getUserId(context))
},
editReply(_, args, context) {
return callCollectionMethod('editReply', args, [
'rootId',
'replyId',
'content',
], getUserId(context))
},
removeReply(_, args, context) {
return callCollectionMethod('removeReply', args, ['rootId', 'replyId'], getUserId(context))
},
likeReply(_, args, context) {
return callCollectionMethod('likeReply', args, ['rootId', 'replyId'], getUserId(context))
},
dislikeReply(_, args, context) {
return callCollectionMethod('dislikeReply', args, ['rootId', 'replyId'], getUserId(context))
},
starReply(_, args, context) {
return callCollectionMethod('starReply', args, [
'rootId',
'replyId',
'starsCount',
], getUserId(context))
},
},
},
}
}