Skip to content

Instantly share code, notes, and snippets.

@desaijay315
Created June 9, 2019 15:38
Show Gist options
  • Save desaijay315/2308f75dba8bd90b481d3516f3fd072e to your computer and use it in GitHub Desktop.
Save desaijay315/2308f75dba8bd90b481d3516f3fd072e to your computer and use it in GitHub Desktop.
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