Skip to content

Instantly share code, notes, and snippets.

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)
subscription{
post{
mutation
data{
id
title
body
author{
id
name
createPost(parent,args,ctx,info){
const userExists = ctx.users.some((user)=> user.id === args.author)
if(!userExists){
throw new Error('User does not exist!')
}
const post = {
id: uuidv4(),
...args
const server = new GraphQLServer({
typeDefs,
resolvers,
context:{
pubsub
}
})
const resolvers = {
Subscriptions:{
post:{
subscribe(parent, args, ctx, info){
return ctx.pubsub.asyncIterator('post')
}
}
}
}
import { PubSub } from 'graphql-yoga';
const pubsub = new PubSub()
type PostSubscriptionPayload {
mutation: String!
data: Post!
}
type Subscription {
post: PostSubscriptionPayload!
}
updateUser(parent, args, ctx, info){
const user = users.find((user) => user.id === args.id)
if(!user){
throw new Error('User does not exist!')
}
if(typeof args.email === 'string'){
const isEmailExists = db.users.some((user) => user.email === args.email)
if(isEmailExists){
throw new Error('Email already Taken')
type Mutation{
createNewUser(name: String!, email: String!, age: Int): User!
deleteUser(id: ID!): User!
updateUser(id: ID!, name: String, email: String, age: Int): User!
}