nestjs-dataloader icon indicating copy to clipboard operation
nestjs-dataloader copied to clipboard

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

Open andreialecu opened this issue 4 years ago • 3 comments

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 avatar Jul 18 '20 11:07 andreialecu

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

andreialecu avatar Jul 18 '20 12:07 andreialecu

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 }

andreialecu avatar Jul 18 '20 13:07 andreialecu

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

dimatill avatar Nov 11 '22 14:11 dimatill