Created
June 9, 2019 15:38
-
-
Save desaijay315/2308f75dba8bd90b481d3516f3fd072e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment