As defined in the Apollo Server documentation, data sources are specific classes that encapsulate fetching data from particular services, with built-in support for caching, deduplication and error handling. A data source instance uses the cache of your GraphQL Server and is usually passed through your application context.
GraphQL Modules has built-in support for data sources in its own encapsulation-based modular dependency-injection system. GraphQL Modules considers data sources as providers and passes the cache logic of the module.
Let's assume you have a data source class for the communication between your external REST API.
All you need to do is add the
Injectable decorator for this class to let the class be part of GraphQL Modules dependency injection.
ProviderScope.Sessionis the recommended scope for data sources, it will ensure API responses are not cached between sessions. See Provider Scopes.
See also Data sources - Apollo Server.
REST Data Source Example
Accessing data sources from resolvers
Then add this data source provider to our module, and you can use that data source in your resolvers just like other kinds of providers.
Using Memcached/Redis as a cache storage backend
As described in Apollo Server docs, GraphQL Modules also uses in-memory caching mechanism by default. But you can also use other cache mechanisms in your GraphQL Modules application.
You can share GraphQL Modules cache mechanism with your GraphQL Server.