krislefeber / nestjs-dataloader

Dataloader plugin for NestJS
https://krislefeber.github.io/nestjs-dataloader/
MIT License
148 stars 45 forks source link

Problem with @Subscription(): stale data is returned for the lifetime of the subscription #36

Open andreialecu opened 4 years ago

andreialecu commented 4 years ago

It appears that when GQL Subscriptions are used, the data returned by dataloaders will remain stale for the duration of the subscription.

I believe this happens because the context is the same for the lifetime of the subscription, so once data gets initially cached, it keeps returning the same initial result for DataLoader-resolved fields.

Manually clearing the cache by injecting the Loader and calling loader.clearAll() prior to returning from the @Subscription() method seems to work. This can be tricky to do depending on how many loaders are used and where they are defined, and is prone to bugs by forgetting to clear something.

andreialecu commented 4 years ago

Related: https://github.com/graphql/graphql-js/issues/894

andreialecu commented 4 years ago

Until merge of https://github.com/graphql/graphql-js/pull/2485 seems like the only solution is to disable caching on all data loaders via { cache: false }

dimatill commented 1 year ago

check https://the-guild.dev/graphql/envelop/v3/guides/resolving-subscription-data-loader-caching-issues , it resolves the issue