What is the ideal way to setup a notifications schema/flow?

Hi, I’m running a Meteor/Apollo/React/MongoDB stack, and I’m wondering what the best way would be to implement a notifications system. My current idea is to create a new Notifications collection in MongoDB, and have the following schema defined:

type Notification {
    _id: String!
    # user_id points to the user's ID in the Users collection
    user_id: String!
    # Here type_of_content can be a Post, or Like, etc but we just represent
    # it as a string
    type_of_content: String!
    timestamp: String!

But I’m having a bit of trouble here proceeding:

  1. If User A posts something, I need to notify all of User A’s current followers. I can do this by having an open WebSocket subscription for every user. But I need to know who these followers are, and for that I can refer to the array of followers stored in my main User object. These users can then mark their own notification from User A as “read”. This is a simple Boolean, but how should this be stored? I thought of storing it as an array in the Notification object but this isn’t ideal because User A’s followers can change and these array sizes will not correspond. I also don’t think it’s a good idea storing the entire array of follower IDs either because that seems like a lot of duplicate data.
  2. Should the Notifications instead be stored as an array within each User object? This seems simple because then I can just replace the user_id field with read: Boolean! and it’s done (_id would then point to User A’s userID), just that write operations have more overheads because you push to all of User A’s follower’s Notification arrays. The Subscription here for all users would be to their own Notifications field.

How should I proceed from here?

This isn’t really a Apollo question, but nevertheless…

I think a separate notifications collection might make more sense? That way you have a centralized place to store the information, and an easy way to tag the owner/followers and track read states without replicating the content across every single user (which could/would grow unbounded).

1 Like

Thanks, there was an overlap in terms of the how I envisioned the data to be stored in MongoDB and the GraphQL API, but I see that fundamentally this is more of a data storage question rather than an Apollo/GraphQL question. I shall seek this out in the MongoDB community, and will update this post with an answer for completeness.