{"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,"search_vec":"'-1.0':50 '0.21.0':32 '60':71 'abortcontrol':98 'activ':35 'advanc':111 'aim':117 'also':96 'api':28,149 'apollo':1,6,16,75,106,127,151 'apollo-datasource-http':5 'apollo-datasource-rest':74 'backend':109 'base':148 'behavior':143 'break':54 'cach':83,86,107,112,142 'cadenc':41 'cancel':102 'capabl':94 'chang':55 'claim':68 'client':67 'configur':84 'connect':126 'control':137 'current':29 'data':3,13,25,115,153 'data-sourc':152 'datasourc':7,76 'dedupl':80 'design':18 'develop':36 'differenti':58 'effici':123 'enhanc':20 'error':93 'explicit':44 'extern':130 'faster':72 'fetch':23 'fine':135 'fine-grain':134 'frequent':39 'grain':136 'graphql':157 'high':63 'high-perform':62 'hook':147 'hook-bas':146 'http':2,8,12,66,131 'includ':59 'integr':78,104 'introduc':53 'javascript':150 'json':24,156 'key':57 'leverag':60 'lifecycl':140 'lru':82 'manual':100 'may':52 'offer':133 'optim':11 'perform':21,64 'pre':49 'provid':119 'releas':40,47 'request':79,85,101,139 'rest':27,77,155 'robust':121 'server':17,128 'servic':132 'solut':124 'sourc':4,14,116,154 'stale':91 'stale-if-error':90 'storag':108 'strategi':113 'support':97 'though':42 'ttl':88 'typescript':158 'undici':65 'version':31 'via':81 'warn':45","created_at":"2026-04-21T19:17:31.553882+00:00","updated_at":"2026-04-21T19:17:31.553882+00:00","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","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":"https://www.apollographql.com/docs/apollo-server/data/http-data-source/","github":"https://github.com/StarpTech/apollo-datasource-http","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/apollo-datasource-http","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","web-framework","data","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-20","install_tag":null}}