Skip to content

Stale data invalidation with delta syncing #18

Open
@vladar

Description

@vladar

GraphQL schema doesn't convey full information about data interdependencies and relations. So it is possible to have some pieces of data stale with delta sync. Quick example:

type Category {
  title: String
  totalCommentCount: Int
}

type Post {
  category: Category
  commentCount: Int
  comments: [Comment]
}

Say some comment is deleted and we are notified about it via a DELETE event. Three things are stale now:

  1. Reference to this comment in Post.comments field
  2. Post.commentCount field is also stale as it has to be decremented on comment delete
  3. Category.totalCommentCount - same

When this happens there should be a way to update specific fields of related nodes. So in addition to UPDATE and DELETE events (targeting individual nodes), we need UPDATE_FIELDS event that can notify us about specific node fields that had changed.

This event would generate dynamic new NODE_ query that will only fetch the mentioned fields and update the node. So it will look something like this:

{
  eventName: "UPDATE_FIELDS",
  remoteTypeName: "Post",
  remoteId: { __typename: "Post", id: "1" },
  fields: ["commentCount", "comments"]
},
{
  eventName: "UPDATE_FIELDS",
  remoteTypeName: "Category",
  remoteId: { __typename: "Category", id: "1" },
  fields: ["totalCommentCount"]
},

Additionally maybe allow fields to be a fragment, i.e.:

{
  eventName: "UPDATE_FIELDS",
  remoteTypeName: "Post",
  remoteId: { __typename: "Post", id: "1" },
  fields: `fragment ChangedFields on Post { commentCount, comments { __typename } }`
},

Technically we can update related nodes fully. But that can be pretty heavy in some cases, so more targeted updates can be useful in performance-sensitive scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions