Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Cauen opened this issue Nov 20, 2024 · 1 comment
Labels
kind/enhancement New feature or request

Comments

@Cauen
Copy link

Cauen commented Nov 20, 2024

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

@EmrysMyrddin EmrysMyrddin added the kind/enhancement New feature or request label Dec 10, 2024
@EmrysMyrddin
Copy link
Collaborator

Hi @Cauen ! Thank you for this proposal 🙂 Looks like a good API, pretty coherent with other hooks API.

If you want to implement this, feel free to open a PR! They are very welcome! And we are here to support you if you need help or have any questions 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants