Registry / database / apollov4-datasource-mongodb

apollov4-datasource-mongodb

JSON →
library0.1.2jsnpmunverified

A data source for Apollo Server v4 that integrates MongoDB via native driver collections or Mongoose models. Version 0.1.2 (stable) provides DataLoader-based batching and per-request memoization caching, with optional shared application-level caching (ttl). Key differentiators: supports both native MongoDB driver and Mongoose, provides caching for findOneById, findManyByIds, and findByFields methods, and includes a deleteFromCacheById utility. This is a fork of the original apollo-datasource-mongodb adapted for Apollo Server v4. Peer dependency on mongoose (*) required if using Mongoose models. Ships TypeScript types. Release cadence: low.

npm install apollov4-datasource-mongodb
INSTALL
IMPORT
SIG · APOLLOV4-DATASOURC
A
apollov4-datasource-mongodb
databasejavascriptv0.1.2
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 'apollov4-datasource-mongodb'
const MongoDataSource = require('apollov4-datasource-mongodb')
ESM-only; no default export. CommonJS require will not work because this package has 'type': 'module' or uses ESM syntax.
MongoDataSource class instantiation
class Users extends MongoDataSource { constructor(options) { super(options); } }
const users = new MongoDataSource({ modelOrCollection: ... })
Do not instantiate MongoDataSource directly; always subclass and implement custom methods.
findOneById
this.findOneById(id, { ttl: 60 })
this.findOneById({ _id: id })
First argument must be the id (string or ObjectId), not a query object. Options object is optional second argument.

Sets up an Apollo Server v4 with MongoDB data source: defines a Users subclass, connects to MongoDB, and uses findOneById in a resolver.

import { MongoDataSource } from 'apollov4-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); } } const client = new MongoClient('mongodb://localhost:27017', { useUnifiedTopology: true }); await client.connect(); const collection = client.db('test').collection('users'); const server = new ApolloServer({ typeDefs: `type User { id: ID! name: String } type Query { user(id: ID!): User }`, resolvers: { Query: { user: async (_, { id }, { dataSources }) => dataSources.users.getUser(id), }, }, }); const { url } = await startStandaloneServer(server, { context: async () => ({ dataSources: { users: new Users({ modelOrCollection: collection }), }, }), listen: { port: 4000 }, }); console.log(`Server running at ${url}`);
Debug
Known issues
breakingApollo Server v4 requires the @apollo/server package; this data source is not compatible with Apollo Server v2/v3.
fix
Upgrade to Apollo Server v4 (>=4.0.0) and use apollov4-datasource-mongodb instead of the original apollo-datasource-mongodb.
affects: <0.1.0
breakingThe constructor expects 'modelOrCollection' as a single object; passing both 'model' and 'collection' separately will cause an error.
fix
Provide a single object with property 'modelOrCollection' set to either a Mongoose model or a MongoDB Collection instance.
affects: >=0.1.0
gotchaWhen using findOneById with Mongoose, the id must be the _id field; using a different field will not work.
fix
Use findByFields for queries by non-_id fields, or ensure your Mongoose model uses _id as the primary key.
affects: >=0.1.0
gotchaCaching is only activated if a 'ttl' option is provided to findOneById, findManyByIds, or findByFields; otherwise, only per-request memoization (DataLoader) is used.
fix
Provide ttl in seconds (e.g., { ttl: 60 }) to enable application-level caching via Apollo's cache backend.
affects: >=0.1.0
deprecatedThe original apollo-datasource-mongodb is not updated for Apollo Server v4; this fork is maintained separately.
fix
Use apollov4-datasource-mongodb instead of apollo-datasource-mongodb with Apollo Server v4.
affects: >=0.1.0
Errors
Common errors & fixes
Cannot find module 'apollov4-datasource-mongodb'
Package not installed or incorrect import path.
fix
Run 'npm install apollov4-datasource-mongodb' and ensure import path matches.
TypeError: Class extends value undefined is not a constructor or null
Circular dependency or missing import of MongoDataSource.
fix
Verify import statement: 'import { MongoDataSource } from 'apollov4-datasource-mongodb''.
TypeError: this.findOneById is not a function
MongoDataSource not properly extended; subclass missing super(options) call.
fix
Add super(options) in the constructor of your subclass.
Property 'modelOrCollection' is missing in type '{}'
TypeScript error due to missing or incorrect options object.
fix
Provide an options object with 'modelOrCollection' property (e.g., { modelOrCollection: collection }).
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies
mongooseoptionalRequired when using Mongoose models instead of native MongoDB collections
dataloaderoptionalUsed internally for batching and caching, but may be installed as a transitive dependency via this package
Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
2
Resources
apollov4-datasource-mongodb — npm install apollov4-datasource-mongodb · libregistry