Why do we need a *per request* based data source instance?

Yep. In Apollo Server 4 we’re making this more clear: instead of having special built-in support for data sources that involves invisibly passing the context to the data source, data sources will just be another thing you can set up in your context function. If access to all or part of the context object in your data source is helpful, you can pass it directly to your data source’s constructor, instead of it happening via sleight of hand behind your back. (This also means that the type of the object returned from your context function is “your context type” not “your context type minus the dataSources field”.)

For RESTDataSource, the idea is that there are two levels of caching: a shared cache that obeys HTTP cache response headers, and a second per-response in-memory cache that ensures that a single operation never makes precisely the same HTTP call twice (even if the response to that operation doesn’t say it’s cacheable). The second is implemented by having the DataSource object be per-request.

1 Like