Registry / database / graphql-mongoose-subscriptions

graphql-mongoose-subscriptions

JSON →
library1.0.18jsnpmunverified

Implements the PubSubEngine interface from graphql-subscriptions using mongoose as the underlying pub/sub mechanism. Version 1.0.18 is the latest stable release. Allows multiple subscription manager instances to communicate via a shared mongoose database by publishing and subscribing to topics through mongoose. Designed for Apollo GraphQL subscriptions in Node.js environments that already use mongoose as a database driver. Key differentiator: enables cross-instance subscription events without external message brokers or Redis, suitable for serverless or multi-process setups where all instances share a mongoose backend.

npm install graphql-mongoose-subscriptions
INSTALL
IMPORT
SIG · GRAPHQL-MONGOOSE-S
G
graphql-mongoose-subscriptions
databasejavascriptv1.0.18
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.

MongoosePubSub
import { MongoosePubSub } from 'graphql-mongoose-subscriptions';
const MongoosePubSub = require('graphql-mongoose-subscriptions').MongoosePubSub;
Package is ESM by default; CommonJS require fails unless using .default or specific bundler config.
default
import MongoosePubSub from 'graphql-mongoose-subscriptions';
import { default } from 'graphql-mongoose-subscriptions';
Default export is not available; use named import only.
withFilter
import { withFilter } from 'graphql-subscriptions';
import { withFilter } from 'graphql-mongoose-subscriptions';
withFilter is re-exported from graphql-subscriptions, not from this package.
PubSubEngine (type)
import type { PubSubEngine } from 'graphql-subscriptions';
import { PubSubEngine } from 'graphql-mongoose-subscriptions';
Type is only in graphql-subscriptions, not directly exported here.

Shows creating a MongoosePubSub instance and using it with Apollo GraphQL subscriptions (subscribe and publish).

import mongoose from 'mongoose'; import { MongoosePubSub } from 'graphql-mongoose-subscriptions'; import { withFilter } from 'graphql-subscriptions'; // Connect to mongoose (ensure mongoose is already connected) await mongoose.connect(process.env.MONGO_URI ?? 'mongodb://localhost:27017/test'); // Create pubsub instance const pubsub = new MongoosePubSub(); // In your GraphQL resolvers const SOMETHING_CHANGED_TOPIC = 'something_changed'; const resolvers = { Subscription: { somethingChanged: { subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC), }, }, Mutation: { changeSomething: (_, { id }) => { pubsub.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: { id } }); return { id }; }, }, }; export default resolvers;
Debug
Known issues
gotchaMongoosePubSub requires an active mongoose connection. It does not manage mongoose connections; you must call mongoose.connect() before creating an instance.
fix
Ensure mongoose.connect() or mongoose.createConnection() is called and resolved before new MongoosePubSub().
affects: >=1.0.0
gotchaThe publish method sends a message to other instances via mongoose, but the publishing instance does NOT automatically receive its own published events unless it also subscribes. For local delivery, you need to call asyncIterator after publish or use a different pattern.
fix
To receive local events, subscribe before publishing, or use a separate mechanism like EventEmitter.
affects: >=1.0.0
deprecatedThe package uses `graphql-subscriptions` v1 which is deprecated in favor of `@apollo/subgraph` and newer subscription patterns in Apollo Server 4.
fix
Consider upgrading to Apollo Server 4's built-in subscription support or using `@apollo/subgraph` with `graphql-subscriptions@2`.
affects: >=1.0.0
gotchaMongoosePubSub does not support pattern-based subscriptions (like wildcards) natively; the asyncIterator signature assumes exact topic match.
fix
Use a consistent topic naming scheme and subscribe to each topic individually, or implement custom filtering with withFilter.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'graphql-mongoose-subscriptions'
Package not installed or import path incorrect.
fix
Run `npm install graphql-mongoose-subscriptions` and ensure import path is correct: `import { MongoosePubSub } from 'graphql-mongoose-subscriptions';`
MongoosePubSub is not a constructor
Import might be using CommonJS require incorrectly.
fix
Use ES module import: `import { MongoosePubSub } from 'graphql-mongoose-subscriptions';`
TypeError: pubsub.asyncIterator is not a function
pubsub object is not properly initialized or may be from a re-export of graphql-subscriptions.
fix
Verify you imported `MongoosePubSub` from 'graphql-mongoose-subscriptions' and not from 'graphql-subscriptions'.
Upgrade
Version history
1.0.18latest on npm
Audit
Dependencies
graphql-subscriptionsrequiredpeer dependency - provides the PubSubEngine interface and AsyncIterator
Agent activity
10 hits · last 30 days
node
10
Resources
graphql-mongoose-subscriptions — npm install graphql-mongoose-subscriptions · libregistry