-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.js
62 lines (54 loc) · 1.65 KB
/
resolver.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
const Post = require('./models/Post.model');
const ObjectID = require('mongodb').ObjectId;
const resolvers = {
Query: {
hello: () => {
return "Hello world";
},
getAllPosts: async () => {
return await Post.find();
},
getPost: async (parent, args, context, info) => {
let {id} = args;
id = await ObjectID(id);
return await Post.findById(id);
}
},
Mutation: {
createPost: async (parent, args, context, info) => {
const {title, description} = args.post;
const post = new Post({
"title": {
"type": title,
"required": true
},
"description": {
"type": description
}
});
await post.save();
return post;
},
deletePost: async (parent, args, context, info) => {
let {id} = args;
id = await ObjectID(id);
await Post.findByIdAndDelete(id);
return `Was deleted post with id ${id}.`
},
updatePost: async (parent, args, context, info) => {
const {title, description} = args.post;
let id = await ObjectID(args.id);
const post = Post.findByIdAndUpdate(id, {
"title": {
"type": title,
"required": true
},
"description": {
"type": description
}
}, {new: true});
return post;
}
}
};
module.exports = resolvers;