{"id":15958,"library":"apollo-datasource-http","title":"Apollo HTTP Data Source","description":"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.","status":"active","version":"0.21.0","language":"javascript","source_language":"en","source_url":"https://github.com/StarpTech/apollo-datasource-http","tags":["javascript","apollo","data-source","rest","json","graphql","typescript"],"install":[{"cmd":"npm install apollo-datasource-http","lang":"bash","label":"npm"},{"cmd":"yarn add apollo-datasource-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add apollo-datasource-http","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for Apollo Server integration.","package":"graphql","optional":false}],"imports":[{"note":"Primarily designed for ES Modules. For CommonJS, dynamic import or a build-time transpilation strategy is typically required.","wrong":"const HTTPDataSource = require('apollo-datasource-http')","symbol":"HTTPDataSource","correct":"import { HTTPDataSource } from 'apollo-datasource-http'"},{"note":"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.","wrong":"import { Pool } from 'apollo-datasource-http'","symbol":"Pool","correct":"import { Pool } from 'undici'"}],"quickstart":{"code":"import { ApolloServer } from '@apollo/server';\nimport { startStandaloneServer } from '@apollo/server/standalone';\nimport { Pool } from 'undici';\nimport { HTTPDataSource } from 'apollo-datasource-http';\n\nconst typeDefs = `#graphql\n  type Movie {\n    id: ID!\n    name: String\n  }\n\n  type Query {\n    movie(id: ID!): Movie\n  }\n\n  type Mutation {\n    createMovie(name: String!): Movie\n  }\n`;\n\nconst baseURL = 'https://movies-api.example.com'; // Replace with your actual API base URL\nconst pool = new Pool(baseURL);\n\nclass MoviesAPI extends HTTPDataSource {\n  constructor(baseURL: string, pool: Pool) {\n    super(baseURL, { pool });\n  }\n\n  async createMovie(name: string) {\n    return this.post('/movies', {\n      body: { name }\n    });\n  }\n\n  async getMovie(id: string) {\n    return this.get(`/movies/${id}`, {\n      context: { tracingName: 'getMovie' },\n      requestCache: {\n        maxTtl: 10 * 60, // 10min, will respond for 10min with the cached result\n        maxTtlIfError: 30 * 60 // 30min, will respond with cached response in case of error\n      }\n    });\n  }\n}\n\nconst resolvers = {\n  Query: {\n    movie: async (_, { id }, { dataSources }) => {\n      return dataSources.moviesAPI.getMovie(id);\n    }\n  },\n  Mutation: {\n    createMovie: async (_, { name }, { dataSources }) => {\n      return dataSources.moviesAPI.createMovie(name);\n    }\n  }\n};\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n});\n\nstartStandaloneServer(server, {\n  listen: { port: 4000 },\n  context: async ({ req, res }) => ({\n    dataSources: {\n      moviesAPI: new MoviesAPI(baseURL, pool),\n    },\n  }),\n}).then(({ url }) => {\n  console.log(`🚀 Server ready at ${url}`);\n});","lang":"typescript","description":"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."},"warnings":[{"fix":"Always review release notes for new versions before upgrading and be prepared for potential API changes even in minor updates.","message":"This 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.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Instantiate `new Pool(baseURL)` once per unique base URL (ideally outside of the request hotpath) and pass it to your `HTTPDataSource` subclass constructor.","message":"To 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=0.18.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Run `npm install apollo-datasource-http` or `yarn add apollo-datasource-http` to install the package.","cause":"The `apollo-datasource-http` package has not been installed or is not resolvable in the current environment.","error":"Error: Cannot find module 'apollo-datasource-http'"},{"fix":"Ensure your `dataSources` configuration is a function, e.g., `dataSources: () => ({ moviesAPI: new MoviesAPI(baseURL, pool) })`.","cause":"The `dataSources` property in the `ApolloServer` constructor was not provided as a function that returns the data source instances.","error":"Error: dataSources must be a function which returns an object or a Promise which resolves to an object."},{"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.","cause":"Your project's `graphql` version conflicts with the peer dependency requirement of `apollo-datasource-http`.","error":"npm ERR! ERESOLVE unable to resolve dependency tree (or similar peer dependency warning for 'graphql')"}],"ecosystem":"npm"}