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

Upon reading the data-source section for apollo, the following line suggests that data sources are created on each request

dataSources: () => {
    return {
      moviesAPI: new MoviesAPI(),
      personalizationAPI: new PersonalizationAPI(),
    };
  },
  • Apollo Server calls this function for every incoming operation.
  • the function should create a new instance of each data source for each operation.

I am not really sure what is the benefit of this? why can’t it initialize once and use these data sources as singleton?

To answer myself, oftentimes, data source will use data from context, which is really per-request scoped. Thus we need a per-request scoped datasource to match that.

1 Like

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