-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.js
63 lines (60 loc) · 1.89 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
61
62
63
const { AuthenticationError, PubSub } = require('apollo-server');
// const mongoose = require('mongoose');
const Pin = require('./models/Pin');
const pubSub = new PubSub();
const authenticated = next => (parent, args, ctx, info) => {
if (!ctx.currentUser) {
throw new AuthenticationError('You must be logged in');
}
return next(parent, args, ctx, info);
};
module.exports = {
Query: {
me: authenticated((parent, args, ctx, info) => ctx.currentUser),
getPins: async (parent, args, ctx, info) => {
return await Pin.find({})
.populate('author')
.populate('comments.author');
},
},
Mutation: {
createPin: authenticated(async (parent, args, ctx, info) => {
const newPin = await new Pin({
...args.input,
author: ctx.currentUser._id,
}).save();
const pinAdded = await Pin.populate(newPin, 'author');
await pubSub.publish('PIN_ADDED', { pinAdded });
return pinAdded;
}),
deletePin: authenticated(async (parent, { pinId }) => {
const pinDeleted = await Pin.findOneAndDelete({ _id: pinId }).exec();
await pubSub.publish('PIN_DELETED', { pinDeleted });
return pinDeleted;
}),
createComment: authenticated(
async (parent, { pinId, text }, { currentUser }, info) => {
const updatePin = await Pin.findOneAndUpdate(
{ _id: pinId },
{ $push: { comments: { text, author: currentUser._id } } },
{ new: true },
)
.populate('author')
.populate('comments.author');
// await pubSub.publish('PIN_UPDATED', { updatePin });
return updatePin;
},
),
},
Subscription: {
pinAdded: {
subscribe: () => pubSub.asyncIterator('PIN_ADDED'),
},
pinDeleted: {
subscribe: () => pubSub.asyncIterator('PIN_DELETED'),
},
pinUpdated: {
subscribe: () => pubSub.asyncIterator('PIN_UPDATED'),
},
},
};