I'm using RESTDataSource as the basis for all REST backend operations in Apollo server. This works fine for Queries and Mutations, and in particular the ability to use cached responses within a single request is working as expected.
I want to use my RESTDataSource implementations in the context of Subscriptions as well, in case the client asks for detail that is not present in the "native" payload of the subscription message (in which case Apollo will call the appropriate resolver to go get it).
Currently I am initialising my RESTDataSources as part of the context callback function used by ApolloServer, and adding them to the returned context (I am only doing this if the callback is passed a connection
object).
The problem is that this is called on establishment of the subscription, rather than on arrival of each message over that subscription. So the natural caching behaviour of the RESTDataSource kicks in for as long as the subscription is held open, and this may be too long to be comfortable with cached responses.
I'd be interested in any advice as to how to overcome this; possibly instantiating the data sources in some way as to disable caching in this particular case, or somehow reinitialising the datasources in the context on the arrival of each message (although I couldn't see a suitable entry point for that).