apollo-datasource-http is an optimized HTTP data source for Apollo Server, designed to enhance performance when fetching JSON data from REST APIs. Currently at version 0.21.0, it is actively developed with a frequent release cadence, though it explicitly warns that releases are pre-1.0 and may introduce breaking changes. Its key differentiators include leveraging the high-performance Undici HTTP client (claiming up to 60% faster than `apollo-datasource-rest`), integrated request deduplication via LRU caching, configurable request caching with TTL, and `stale-if-error` capabilities. It also supports `AbortController` for manual request cancellation and integrates with Apollo Cache Storage backends for advanced caching strategies. This data source aims to provide a robust and efficient solution for connecting Apollo Server to external HTTP services, offering fine-grained control over request lifecycle and caching behavior through a hook-based API.
npm install apollo-datasource-httpVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to extend `HTTPDataSource` to create a custom data source, configure it with `undici.Pool`, and implement methods for making cached GET and POST requests within an Apollo Server setup.
Always review release notes for new versions before upgrading and be prepared for potential API changes even in minor updates.
Instantiate `new Pool(baseURL)` once per unique base URL (ideally outside of the request hotpath) and pass it to your `HTTPDataSource` subclass constructor.
Ensure your project's `graphql` peer dependency is within the `^15.3.0 || ^16.0.0` range. If using older Apollo Server versions, you might need to stick to an older `apollo-datasource-http` release.
Run `npm install apollo-datasource-http` or `yarn add apollo-datasource-http` to install the package.
Ensure your `dataSources` configuration is a function, e.g., `dataSources: () => ({ moviesAPI: new MoviesAPI(baseURL, pool) })`.Update your `graphql` package to a compatible version (e.g., `npm install graphql@16`) or adjust your dependency resolution strategy to satisfy the `^15.3.0 || ^16.0.0` range.