From 1b26dec38cba698d0bddd2c9881fdd344a0de48a Mon Sep 17 00:00:00 2001 From: DAB0mB Date: Tue, 9 Apr 2019 14:53:20 +0800 Subject: [PATCH] Step 9.5: Add Subscription.chatRemoved --- schema/resolvers.ts | 18 +++++++++++++++++- schema/typeDefs.graphql | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/schema/resolvers.ts b/schema/resolvers.ts index 8863ad607..8e198e94a 100644 --- a/schema/resolvers.ts +++ b/schema/resolvers.ts @@ -160,7 +160,7 @@ const resolvers: Resolvers = { return chat; }, - removeChat(root, { chatId }, { currentUser }) { + removeChat(root, { chatId }, { currentUser, pubsub }) { if (!currentUser) return null; const chatIndex = chats.findIndex(c => c.id === chatId); @@ -181,6 +181,11 @@ const resolvers: Resolvers = { chats.splice(chatIndex, 1); + pubsub.publish('chatRemoved', { + chatRemoved: chat.id, + targetChat: chat, + }); + return chatId; }, }, @@ -209,6 +214,17 @@ const resolvers: Resolvers = { } ), }, + + chatRemoved: { + subscribe: withFilter( + (root, args, { pubsub }) => pubsub.asyncIterator('chatRemoved'), + ({ targetChat }: { targetChat: Chat }, args, { currentUser }) => { + if (!currentUser) return false; + + return targetChat.participants.some(p => p === currentUser.id); + } + ), + }, }, }; diff --git a/schema/typeDefs.graphql b/schema/typeDefs.graphql index 7f5e2c848..bf0ba2d5e 100644 --- a/schema/typeDefs.graphql +++ b/schema/typeDefs.graphql @@ -41,4 +41,5 @@ type Mutation { type Subscription { messageAdded: Message! chatAdded: Chat! + chatRemoved: ID! }