Registry / database / apollo-connector-mongodb

apollo-connector-mongodb

JSON →
library3.1.1jsnpmunverified

Apollo connector for MongoDB, version 3.1.1, providing seamless integration with GraphQL (Apollo). It facilitates MongoDB connection, result caching via Facebook's DataLoader for batch processing and caching, and unit testing with a handy `withEntity` function. Built in ES6 with CommonJS modules, requires Node 6+. Key differentiators include built-in caching for database queries and easy unit testing support, distinguishing it from raw MongoDB drivers or other GraphQL connectors. Release cadence appears low, with maintenance mode likely.

npm install apollo-connector-mongodb
INSTALL
IMPORT
SIG · APOLLO-CONNECTOR-M
A
apollo-connector-mongodb
databasejavascriptv3.1.1
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.

MongoConnector
import { MongoConnector } from 'apollo-connector-mongodb'
const MongoConnector = require('apollo-connector-mongodb').MongoConnector
ESM import works; CommonJS require also supported but named import is preferred.
MongoEntity
import { MongoEntity } from 'apollo-connector-mongodb'
import MongoEntity from 'apollo-connector-mongodb'
Default export does not exist; must use named import.
withEntity
import { withEntity } from 'apollo-connector-mongodb'
const { withEntity } = require('apollo-connector-mongodb')
Both ESM and CJS work; ensure named export.
MongoConnector
const { MongoConnector } = require('apollo-connector-mongodb')
const MongoConnector = require('apollo-connector-mongodb')
CommonJS destructured import is correct; default require does not return the class.

Shows how to create a MongoConnector and MongoEntity, set up caching with lru-cache, and use findOneCachedById.

import { MongoConnector, MongoEntity } from 'apollo-connector-mongodb'; import lruCache from 'lru-cache'; const mongoURL = process.env.MONGO_URL || 'mongodb://localhost:27017/test'; const conn = new MongoConnector(mongoURL, () => { const users = new MongoEntity(conn, 'users', { cacheMap: lruCache }); const context = { users }; // Init Apollo Server with schema and context // startExpress(config); }); // Example usage in resolver async function getUser(id: string) { const user = await users.findOneCachedById(id); return user; }
Debug
Known issues
breakingVersion 3.0.0 switched to mongodb 4.0 driver. The find function now returns a cursor instead of array, and the API has changed.
fix
Update code to use cursor-based access or call .toArray(). Check mongodb 4.0 migration guide.
affects: >=3.0.0
deprecatedThe package uses CommonJS and requires Node 6+. Modern Node versions support ESM, but the package does not ship ESM.
fix
If using Node 16+, consider using a more modern driver such as 'mongodb' directly or a GraphQL datasource.
affects: *
gotchaMongoEntity caching relies on DataLoader. If cacheMap is not provided, caching may not work as expected.
fix
Always pass a cacheMap (e.g., lru-cache) to MongoEntity constructor for caching to function.
affects: *
gotchaThe withEntity function for testing creates a real MongoDB connection. Ensure a test database is configured.
fix
Use environment variables or a test MongoDB instance (e.g., mongod in-memory) to avoid polluting dev databases.
affects: *
deprecatedThe package has not seen updates since 2019. It may have unpatched vulnerabilities.
fix
Consider migrating to a maintained alternative such as 'datasource-mongodb' or using the MongoDB driver directly.
affects: <4.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'find' of undefined
MongoEntity instance not connected or MongoDB URL invalid.
fix
Ensure MongoConnector callback is invoked after successful connection, and check MongoDB URI.
Cannot find module 'apollo-connector-mongodb'
Package not installed or import path incorrect.
fix
Run 'npm install apollo-connector-mongodb' and ensure correct import syntax.
DataLoader is not a constructor
Missing dataloader peer dependency.
fix
Install dataloader: 'npm install dataloader'.
lruCache is not defined
lru-cache not imported or imported incorrectly.
fix
Import lru-cache: 'import lruCache from 'lru-cache';'
Upgrade
Version history
3.1.1latest on npm
Audit
Dependencies
mongodbrequiredCore database driver for MongoDB operations.
dataloaderrequiredUsed for batching and caching database query results.
lru-cacheoptionalUsed for caching if provided; optional but recommended.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
apollo-connector-mongodb — npm install apollo-connector-mongodb · libregistry