Registry / http-networking / apollo-datasource-http

apollo-datasource-http

JSON →
library0.21.0jsnpmunverified

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.

http-networkingweb-frameworkdataserialization
npm install apollo-datasource-http
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HTTPDataSource
import { HTTPDataSource } from 'apollo-datasource-http'
const HTTPDataSource = require('apollo-datasource-http')
Primarily designed for ES Modules. For CommonJS, dynamic import or a build-time transpilation strategy is typically required.
Pool
import { Pool } from 'undici'
import { Pool } from 'apollo-datasource-http'
The Undici `Pool` is a crucial dependency for performance, but it must be imported directly from 'undici' and instantiated separately, then passed to the `HTTPDataSource` constructor.

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.

import { ApolloServer } from '@apollo/server'; import { startStandaloneServer } from '@apollo/server/standalone'; import { Pool } from 'undici'; import { HTTPDataSource } from 'apollo-datasource-http'; const typeDefs = `#graphql type Movie { id: ID! name: String } type Query { movie(id: ID!): Movie } type Mutation { createMovie(name: String!): Movie } `; const baseURL = 'https://movies-api.example.com'; // Replace with your actual API base URL const pool = new Pool(baseURL); class MoviesAPI extends HTTPDataSource { constructor(baseURL: string, pool: Pool) { super(baseURL, { pool }); } async createMovie(name: string) { return this.post('/movies', { body: { name } }); } async getMovie(id: string) { return this.get(`/movies/${id}`, { context: { tracingName: 'getMovie' }, requestCache: { maxTtl: 10 * 60, // 10min, will respond for 10min with the cached result maxTtlIfError: 30 * 60 // 30min, will respond with cached response in case of error } }); } } const resolvers = { Query: { movie: async (_, { id }, { dataSources }) => { return dataSources.moviesAPI.getMovie(id); } }, Mutation: { createMovie: async (_, { name }, { dataSources }) => { return dataSources.moviesAPI.createMovie(name); } } }; const server = new ApolloServer({ typeDefs, resolvers, }); startStandaloneServer(server, { listen: { port: 4000 }, context: async ({ req, res }) => ({ dataSources: { moviesAPI: new MoviesAPI(baseURL, pool), }, }), }).then(({ url }) => { console.log(`🚀 Server ready at ${url}`); });
Debug
Known issues
breakingThis package is explicitly declared as pre-1.0 according to SemVer. This means that minor versions (e.g., 0.x.0 to 0.y.0) may introduce breaking changes without prior warning.
fix
Always review release notes for new versions before upgrading and be prepared for potential API changes even in minor updates.
affects: >=0.1.0
gotchaTo achieve the advertised performance benefits, you must explicitly initialize and pass an `undici.Pool` instance to the `HTTPDataSource` constructor. Failing to do so will result in each request creating a new Undici client, negating performance optimizations.
fix
Instantiate `new Pool(baseURL)` once per unique base URL (ideally outside of the request hotpath) and pass it to your `HTTPDataSource` subclass constructor.
affects: >=0.1.0
breakingVersion 0.18.0 introduced significant updates for compatibility with Apollo Server 3 and GraphQL 16. Older versions of Apollo Server or GraphQL might not function correctly with `apollo-datasource-http` versions 0.18.0 or newer.
fix
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.
affects: >=0.18.0
Errors
Common errors & fixes
Error: Cannot find module 'apollo-datasource-http'
The `apollo-datasource-http` package has not been installed or is not resolvable in the current environment.
fix
Run `npm install apollo-datasource-http` or `yarn add apollo-datasource-http` to install the package.
Error: dataSources must be a function which returns an object or a Promise which resolves to an object.
The `dataSources` property in the `ApolloServer` constructor was not provided as a function that returns the data source instances.
fix
Ensure your `dataSources` configuration is a function, e.g., `dataSources: () => ({ moviesAPI: new MoviesAPI(baseURL, pool) })`.
npm ERR! ERESOLVE unable to resolve dependency tree (or similar peer dependency warning for 'graphql')
Your project's `graphql` version conflicts with the peer dependency requirement of `apollo-datasource-http`.
fix
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.
Upgrade
Version history
0.21.0latest on PyPI
Audit
Dependencies
graphqlrequiredPeer dependency required for Apollo Server integration.
Agent activity
31 hits · last 30 days
node
8
claudebot
4
ahrefsbot
3
amazonbot
1
Resources