Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. deletePost(parent,args,{db,pubsub},info){
  2. const isPostExists = db.posts.findIndex((post)=> post.id === args.id)
  3. if(isPostExists === -1){
  4. throw new Error('Post does not exist!')
  5. }
  6. //splice will return the index of the removed items from the array object
  7. const [post] = db.posts.splice(isPostExists, 1)
  8.  
  9. db.comments = db.comments.filter((comment) => comment.post !== args.id)
  10.  
  11. if(post.published){
  12. pubsub.publish('post', {
  13. post:{
  14. mutation: 'DELETED',
  15. data: post
  16. }
  17. })
  18. }
  19. return post
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement