From bf20256083c17efd1a198c0603fa47a56a4df370 Mon Sep 17 00:00:00 2001 From: KazChe Date: Tue, 22 Sep 2020 14:38:00 -0700 Subject: [PATCH] Subscription - Expanding the Posts Subscription for Edits/Updates. Closes #14 --- src/resolvers/Mutation.js | 55 ++++++++++++++++++++++++++++++++--- src/resolvers/Subscription.js | 2 +- src/schema.graphql | 7 ++++- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/resolvers/Mutation.js b/src/resolvers/Mutation.js index 6ab3bbb..938937d 100644 --- a/src/resolvers/Mutation.js +++ b/src/resolvers/Mutation.js @@ -81,12 +81,21 @@ const Mutation = { ...args.data } - db.posts.push(post); - pubsub.publish('post', {post}) + db.posts.push(post) + // publish event only if post.published is tchroo + if(args.data.published) { + pubsub.publish('post', + { + post: { + mutation: 'CREATED', + data: post + } + }) + } return post; }, - deletePost(parent, args, { db }, info) { + deletePost(parent, args, { db, pubsub }, info) { const postIndex = db.posts.findIndex((post) => post.id === args.id) if (postIndex === -1) { @@ -97,11 +106,21 @@ const Mutation = { db.comments = db.comments.filter((comment) => comment.post !== args.id) + if(deletedPosts[0].published) { + pubsub.publish('post', { + post: { + mutation: 'DELETED', + data: deletedPosts[0] + } + }) + } + return deletedPosts[0] }, - updatePost(parnt, args, { db }, info) { + updatePost(parnt, args, { db, pubsub }, info) { const { id, data } = args const post = db.posts.find((post) => post.id === id) + const origPost = { ...post } if(!post) { throw new Error("Post Not Found.") @@ -115,8 +134,36 @@ const Mutation = { } if(typeof data.published === "boolean") { post.published = data.published + + if(origPost.published && !post.published ) { + //delete event + pubsub.publish('post', + { post: { + mutation: 'DELETED', + data: origPost + } + }) + } else if(!origPost.published && post.published) { + //created + pubsub.publish('post', + { post: { + mutation: 'CREATED', + data: post + } + }) + } + } else if(post.published) { + //updated + pubsub.publish('post', + { post: { + mutation: 'UPDATED', + data: origPost + } + }) } + + return post; }, createComment(parent, args, { db, pubsub }, info){ diff --git a/src/resolvers/Subscription.js b/src/resolvers/Subscription.js index ae495d9..b7ba02b 100644 --- a/src/resolvers/Subscription.js +++ b/src/resolvers/Subscription.js @@ -30,8 +30,8 @@ const Subscription = { // next we have to publish newly created post // this will be done when the post create mutation is run, see Mutations.createPost() } + }, - } } export { Subscription as default } \ No newline at end of file diff --git a/src/schema.graphql b/src/schema.graphql index b69872c..0d71124 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -26,7 +26,7 @@ type Mutation { type Subscription { count: Int! comment(postId: ID!): Comment! - post: Post! + post: PostSubscriptionPayload! } input CreateUserInput { @@ -88,4 +88,9 @@ type Comment { text: String! author: User! post: Post! +} + +type PostSubscriptionPayload { + mutation: String! + data: Post! } \ No newline at end of file