Registry / database / graphql-compose-mongoose

graphql-compose-mongoose

JSON →
library10.1.0jsnpmunverified

Plugin for graphql-compose that derives GraphQL types from Mongoose models, generating CRUD resolvers, connection types, and filtering/sorting helpers. Current stable version is 10.1.0, maintained actively with TypeScript support. Requires Node >=20, peer dependencies on graphql-compose ^7.21.4 || ^8.0.0 || ^9.0.0 and Mongoose ^6.0.0 - ^9.0.0. Differentiates from manual type definitions by auto-syncing Mongoose schemas (including indexes, virtuals, discriminators) to GraphQL, and providing a rich set of resolvers like findMany, createOne, removeById, pagination, and dataLoader. Supports query operators ($lt, $gt, etc.) and relational mappings via graphql-compose's schema stitching. Breaking changes in v9.0.0 removed deprecated resolver fields and updated resolver options naming conventions.

npm install graphql-compose-mongoose
INSTALL
IMPORT
SIG · GRAPHQL-COMPOSE-MO
G
graphql-compose-mongoose
databasejavascriptv10.1.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.

composeMongoose
import { composeMongoose } from 'graphql-compose-mongoose'
const composeMongoose = require('graphql-compose-mongoose')
ESM-only since v9; CommonJS require() fails in Node >=20 with package type: module
convertSchemaToGraphQL
import { convertSchemaToGraphQL } from 'graphql-compose-mongoose'
Alternative function for converting Mongoose schema directly, without model instance
removeField
import { removeField } from 'graphql-compose-mongoose'
Utility to remove fields from generated types; works with TC (TypeComposer)

Creates a Mongoose model, uses composeMongoose to generate GraphQL types, adds custom resolver and CRUD resolvers to schemaComposer.

import { composeMongoose } from 'graphql-compose-mongoose'; import { schemaComposer } from 'graphql-compose'; import mongoose from 'mongoose'; const UserSchema = new mongoose.Schema({ name: String, email: { type: String, index: true }, }); const UserModel = mongoose.model('User', UserSchema); const customizationOptions = {}; // optional const UserTC = composeMongoose(UserModel, customizationOptions); UserTC.addResolver({ name: 'findByEmail', type: UserTC, args: { email: 'String!' }, resolve: async ({ args, context }) => { return UserModel.findOne({ email: args.email }); }, }); schemaComposer.Query.addFields({ userById: UserTC.getResolver('findById'), userByEmail: UserTC.getResolver('findByEmail'), userConnection: UserTC.getResolver('connection'), users: UserTC.getResolver('findMany'), }); schemaComposer.Mutation.addFields({ userCreate: UserTC.getResolver('createOne'), userUpdate: UserTC.getResolver('updateById'), userRemove: UserTC.getResolver('removeById'), }); const graphqlSchema = schemaComposer.buildSchema(); export default graphqlSchema;
Debug
Known issues
breakingVersion 9.0.0 removed deprecated resolver fields and changed resolver options structure.
fix
Update resolver options to new format; see migration guide at https://github.com/graphql-compose/graphql-compose-mongoose/blob/alpha/docs/releases/9.0.0.md
affects: >=9.0.0 <10.0.0
breakingVersion 10.0.0 requires Node >=20 and Mongoose >=6.
fix
Upgrade Node.js to >=20 and Mongoose to ^6.0.0 | ^7.0.0 | ^8.0.0 | ^9.0.0. If using older Mongoose 5, stay on v9.
affects: >=10.0.0
deprecatedThe `composeWithMongoose` function (alias) has been deprecated in favor of `composeMongoose`.
fix
Use `composeMongoose` instead of `composeWithMongoose`.
affects: >=9.0.0
gotchaUsing TypeScript with `strict: true` may require explicit type annotations for resolver context or args.
fix
Pass generic types to resolvers, e.g., `getResolver('findOne').resolve({ context: MyContext })`
affects: >=10.0.0
Errors
Common errors & fixes
Cannot find module 'graphql-compose-mongoose'
Missing dependency or wrong import syntax in ESM context.
fix
npm install graphql-compose-mongoose graphql-compose mongoose; use import statement, not require.
TypeError: composeMongoose is not a function
CommonJS require() used with ESM-only package (v9+).
fix
Change to import { composeMongoose } from 'graphql-compose-mongoose'
MongooseError: `uri` must be a string
Not connecting Mongoose to MongoDB before running GraphQL queries.
fix
Call mongoose.connect('mongodb://localhost/mydb') at startup.
Upgrade
Version history
10.1.0latest on npm
Audit
Dependencies
graphql-composerequiredCore dependency: provides schema composition and resolver infrastructure
mongooserequiredUsed to infer GraphQL types from Mongoose schemas and models
Agent activity
13 hits · last 30 days
node
10
Resources
graphql-compose-mongoose — npm install graphql-compose-mongoose · libregistry