Skip to content

Make it possible to skipResult() inside of onSubscribe.onSubscribeResult.onNext envelop plugin to avoid/skip re-send unchanged data #2331

Open
@Cauen

Description

@Cauen

Problem

I was recently researching the best approaches to implement real-time subscriptions sync with database and came across Hasura's approach.

https://github.com/hasura/graphql-engine/blob/master/architecture/live-queries.md#when-do-we-refetch

Instead of watching to real database events, they are syncing at every 1 second, and the justifications are quite reasonable.
Furthermore, in order to optimize resources, they avoid sending same data over the network every 1 second, they only send if it changed. Which also makes perfect sense.

Inside of Schema builder, its simple to make the resolver call every 1 second.

But getting it to avoid sending duplicate messages seems to be a challenge.

Solution

From Envelop plguins, we can create a custom functions to "rewrite" the result of each data response of subscription, but there is no API to avoid/skip sending it.

I think with a new option called skipResult It would make it possible to create a cache like Hasura's for Subscriptions
Does this make any sense?

onNext: ({ args, result, setResult, skipResult }) => {
  const alreadySent = cacheCompare(args, result);
  if (alreadySent) {
    skipResult();
  }
},

I took a good dive into the core/plugins code, did several tests/changes but always tried to avoid sending the expected data, graphql-ws threw an error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions