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! | |
| } |
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
| deleteUser(parent,args,ctx,info){ | |
| const isUserExists = users.findIndex((user)=> user.id === args.author) | |
| if(!isUserExists){ | |
| throw new Error('User does not exist!') | |
| } | |
| //splice will return the removed items from the array object | |
| const userdeleted = users.splice(isUserExists, 1) | |
| return userdeleted[0] | |
| } |