Skip to content

Commit

Permalink
Step 9.5: Add Subscription.chatRemoved
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB authored and Urigo committed May 20, 2020
1 parent ceaf88f commit 1b26dec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
18 changes: 17 additions & 1 deletion schema/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -181,6 +181,11 @@ const resolvers: Resolvers = {

chats.splice(chatIndex, 1);

pubsub.publish('chatRemoved', {
chatRemoved: chat.id,
targetChat: chat,
});

return chatId;
},
},
Expand Down Expand Up @@ -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);
}
),
},
},
};

Expand Down
1 change: 1 addition & 0 deletions schema/typeDefs.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ type Mutation {
type Subscription {
messageAdded: Message!
chatAdded: Chat!
chatRemoved: ID!
}

0 comments on commit 1b26dec

Please sign in to comment.