Skip to content

Commit

Permalink
Step 9.3: Add Subscription.chatAdded
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB authored and Urigo committed May 20, 2020
1 parent 5008bbf commit d361ec1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion schema/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const resolvers: Resolvers = {
return message;
},

addChat(root, { recipientId }, { currentUser }) {
addChat(root, { recipientId }, { currentUser, pubsub }) {
if (!currentUser) return null;
if (!users.some(u => u.id === recipientId)) return null;

Expand All @@ -153,6 +153,10 @@ const resolvers: Resolvers = {

chats.push(chat);

pubsub.publish('chatAdded', {
chatAdded: chat,
});

return chat;
},
},
Expand All @@ -170,6 +174,17 @@ const resolvers: Resolvers = {
}
),
},

chatAdded: {
subscribe: withFilter(
(root, args, { pubsub }) => pubsub.asyncIterator('chatAdded'),
({ chatAdded }: { chatAdded: Chat }, args, { currentUser }) => {
if (!currentUser) return false;

return chatAdded.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 @@ -39,4 +39,5 @@ type Mutation {

type Subscription {
messageAdded: Message!
chatAdded: Chat!
}

0 comments on commit d361ec1

Please sign in to comment.