Description
Describe the bug
When using subscriptions in conjunction with DataLoaders, queries of subsequent subscription events return stale data.
To Reproduce
- Create a query that has a field resolver that leverages a DataLoader
- Create a subscription where the field in step 1. is included in the graph
- Create a mutation that modifies the underlying data and triggers the subscription in Step 2.
- Run the app and subscribe to the subscription in step 2., referencing the field that leverages the DataLoader in Step 1.
- With the app running, invoke the mutation in step 3. multiple times with different data each time.
- Every time the subscription is triggered, the query run in Step 4. will return stale data after the first event.
The following repo demonstrates the issue: https://github.com/rafd123/SubscriptionIssueRepro
Expected behavior
When the subscription event is triggered, its query should reflect the current state of the data.
Desktop (please complete the following information):
- OS: Windows
- Version 10
Additional context
The issue appears to be caused by the fact that the subscription event queries end up sharing the same service scope as the initial request to create the subscription instead of having their own scope.
Using the example repo, this is what the service scope looks like during the initial subscription event query:
This is what the service scope looks like after subsequent subscription event queries; note the DataLoader from the initial query is in the scope with the original data: