Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- deletePost(parent,args,{db,pubsub},info){
- const isPostExists = db.posts.findIndex((post)=> post.id === args.id)
- if(isPostExists === -1){
- throw new Error('Post does not exist!')
- }
- //splice will return the index of the removed items from the array object
- const [post] = db.posts.splice(isPostExists, 1)
- db.comments = db.comments.filter((comment) => comment.post !== args.id)
- if(post.published){
- pubsub.publish('post', {
- post:{
- mutation: 'DELETED',
- data: post
- }
- })
- }
- return post
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement