6

A strange scenario with GraphQL. Let's say there is a Subscription called postIsRead, which signifies a post in a list of posts as currently being read, such as:

Post: { id: 1, read: true }

Only one post can be read at once. When a user clicks on a post, it marks that post as read, and all other posts as unread. It then dispatches a subscription update the user data accordingly.

Here's the updateQuery on the subscription via subscribeToMore.

       updateQuery: (prev, { subscriptionData }) => {
            if (!subscriptionData.data) return prev;
            let posts = [...prev.posts];
            posts.forEach((f)=> {
                f.id === subscriptionData.data.postIsRead.id
                    ? f.read = subscriptionData.data.getPosts.read
                    : f.read = false
            });
            return Object.assign({}, prev, {
                getPosts: posts
            });
        }

Now there are two problems: the status of the posts aren't propagated immediately, such when the posts are being rendered, read === true on multiple posts. It seems to lag behind, as if it is lazy-loading or something. I'm not sure if this is a React problem or a Apollo problem.

While troubleshooting this, it gets even weirder. I commented out all of the code within the updateQuery function such that:

       updateQuery: (prev, { subscriptionData }) => {}

...updates the subscription value to read: true. I appear to be misunderstanding how this works, because it's propagating changes even when there is nothing returned in the function. However if I remove the updateQuery completely, changes do not occur.

Specifically (for the bounty) I'm trying to understand the best method to propagate changes whenever subscriptions are updated.

Nathanael
  • 954
  • 3
  • 19
  • 39

0 Answers0