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) |
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
subscription{ | |
post{ | |
mutation | |
data{ | |
id | |
title | |
body | |
author{ | |
id | |
name |
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
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 |
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
const server = new GraphQLServer({ | |
typeDefs, | |
resolvers, | |
context:{ | |
pubsub | |
} | |
}) |
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
const resolvers = { | |
Subscriptions:{ | |
post:{ | |
subscribe(parent, args, ctx, info){ | |
return ctx.pubsub.asyncIterator('post') | |
} | |
} | |
} | |
} |
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
import { PubSub } from 'graphql-yoga'; | |
const pubsub = new PubSub() |
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
type PostSubscriptionPayload { | |
mutation: String! | |
data: Post! | |
} |
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
type Subscription { | |
post: PostSubscriptionPayload! | |
} |
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
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') |
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
type Mutation{ | |
createNewUser(name: String!, email: String!, age: Int): User! | |
deleteUser(id: ID!): User! | |
updateUser(id: ID!, name: String, email: String, age: Int): User! | |
} |