Registry / database / apollo-datasource-mongodb

apollo-datasource-mongodb

JSON →
library0.6.0jsnpmunverified

Apollo data source for MongoDB that integrates with Apollo Server. Version 0.6.0 supports Apollo Server 4.x; versions ≤0.5.4 support Apollo Server 3.x. Uses DataLoader for batching and per-request memoization caching, with optional shared application-level caching via Apollo cache backends. Provides methods: findOneById, findManyByIds, findByFields, and cache deletion helpers. Ships TypeScript types. Compatible with native MongoDB driver and Mongoose models.

npm install apollo-datasource-mongodb
INSTALL
IMPORT
SIG · APOLLO-DATASOURCE-
A
apollo-datasource-mongodb
databasejavascriptv0.6.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

MongoDataSource
import { MongoDataSource } from 'apollo-datasource-mongodb'
const MongoDataSource = require('apollo-datasource-mongodb').MongoDataSource
ESM-only; CommonJS require with .MongoDataSource property also works but preferred usage is ESM.
Users extends MongoDataSource
class Users extends MongoDataSource { constructor(options) { super(options); } }
class Users extends MongoDataSource { constructor(options) { this.collection = options.collection; } }
Must call super(options) to initialize internal state and DataLoader; directly setting collection will break caching.
findOneById
this.findOneById(id, { ttl: 60 })
this.findOneById(id, 60)
Second argument is an options object, not a number. Common mistake: passing ttl directly as second param.

Shows basic setup: extend MongoDataSource, pass collection to constructor, use findOneById with TTL, and integrate with Apollo Server 4.

import { MongoDataSource } from 'apollo-datasource-mongodb'; import { MongoClient } from 'mongodb'; import { ApolloServer } from '@apollo/server'; import { startStandaloneServer } from '@apollo/server/standalone'; class Users extends MongoDataSource { async getUser(id) { return this.findOneById(id, { ttl: 60 }); } } const client = new MongoClient(process.env.MONGO_URI ?? 'mongodb://localhost:27017'); await client.connect(); const server = new ApolloServer({ typeDefs: `type Query { user(id: ID!): User } type User { id: ID! name: String }`, resolvers: { Query: { user: async (_, { id }, { dataSources }) => dataSources.users.getUser(id), }, }, }); const { url } = await startStandaloneServer(server, { context: async () => ({ dataSources: { users: new Users({ modelOrCollection: client.db('test').collection('users') }), }, }), }); console.log(`🚀 Server ready at ${url}`);
Debug
Known issues
breakingVersion 0.6.0 drops support for Apollo Server 3.x. Use <=0.5.4 for Apollo Server 3.
fix
Upgrade to Apollo Server 4 or pin to apollo-datasource-mongodb@0.5.4.
affects: >=0.6.0
deprecatedConstructor option 'collection' is deprecated; use 'modelOrCollection' instead.
fix
Replace { collection: ... } with { modelOrCollection: ... }.
affects: >=0.6.0
gotchaIf you pass a Mongoose model as modelOrCollection, the data source uses the model's collection; calling findOneById returns a Mongoose document, not a plain object.
fix
Use .toObject() or .lean() if you need a plain object, or stick with native MongoDB driver.
affects: all
gotchaThe caching TTL is per method call, not global. Each call to findOneById, findManyByIds, findByFields can have a different TTL.
fix
Consistently pass the same TTL across calls if you want uniform caching behavior.
affects: all
gotchaThe package does not automatically clear cache on mutations; you must manually call deleteFromCacheById or deleteFromCacheByFields.
fix
After a mutation (e.g., update or delete), invoke the appropriate cache deletion method with the affected IDs or fields.
affects: all
Errors
Common errors & fixes
TypeError: Cannot destructure property 'modelOrCollection' of 'options' as it is undefined.
Calling new Users() without passing options object.
fix
new Users({ modelOrCollection: collection })
Error: You must pass a MongoDB collection or Mongoose model.
modelOrCollection is null or not provided.
fix
Ensure modelOrCollection is a valid collection or model instance.
TypeError: this.findOneById is not a function
Data source class does not extend MongoDataSource, or super() not called.
fix
class Users extends MongoDataSource { constructor(options) { super(options); } }
Error: Cannot find module 'apollo-datasource'
Missing peer dependency apollo-datasource.
fix
npm install apollo-datasource
Upgrade
Version history
0.6.0latest on npm
Audit
Dependencies
apollo-datasourcerequiredRequired to extend the base DataSource class from Apollo
dataloaderrequiredUsed internally for batching and caching
Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
1
Resources
apollo-datasource-mongodb — npm install apollo-datasource-mongodb · libregistry