Registry / database / graphql-to-mongodb

graphql-to-mongodb

JSON →
library1.6.5jsnpmunverified

A library that generates MongoDB query filters, projections, and sort/pagination arguments from GraphQL types at runtime. Version 1.6.5, last updated in 2023, with no active release cadence. It provides helper functions like getGraphQLQueryArgs and getMongoDbQueryResolver to automatically create input filter types and resolve them into MongoDb find operations. Unlike code-first ORMs, it works with existing GraphQL schema types and supports nested field filtering, logical operators (AND, OR, NOR), and sort/pagination. Requires graphql >=14.2.1. Ships TypeScript definitions.

npm install graphql-to-mongodb
INSTALL
IMPORT
SIG · GRAPHQL-TO-MONGODB
G
graphql-to-mongodb
databasejavascriptv1.6.5
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.

getMongoDbQueryResolver
import { getMongoDbQueryResolver } from 'graphql-to-mongodb'
const getMongoDbQueryResolver = require('graphql-to-mongodb').getMongoDbQueryResolver
Package is ESM-only from v1.0.0; named export.
getGraphQLQueryArgs
import { getGraphQLQueryArgs } from 'graphql-to-mongodb'
import getGraphQLQueryArgs from 'graphql-to-mongodb'
Named export, not default.
Types (e.g. GetMongoDbQueryResolverOptions)
import type { GetMongoDbQueryResolverOptions } from 'graphql-to-mongodb'
TypeScript types are exported; can be imported using type-only imports.

Creates a GraphQL query that uses auto-generated filter and sort args, then resolves with MongoDB find.

import { getGraphQLQueryArgs, getMongoDbQueryResolver } from 'graphql-to-mongodb'; import { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLList, GraphQLSchema, GraphQLNonNull } from 'graphql'; const PersonType = new GraphQLObjectType({ name: 'PersonType', fields: () => ({ age: { type: GraphQLInt }, name: { type: GraphQLString }, }) }); const queryType = new GraphQLObjectType({ name: 'Query', fields: () => ({ people: { type: new GraphQLList(PersonType), args: getGraphQLQueryArgs(PersonType), resolve: getMongoDbQueryResolver(PersonType, async (filter, projection, options, obj, args, context) => { const db = context.db; return await db.collection('people').find(filter, options).toArray(); }) } }) }); export const schema = new GraphQLSchema({ query: queryType });
Debug
Known issues
breakingMongoDB driver v3+ requires using options object instead of projection parameter in find().
fix
Use db.collection('people').find(filter, options) instead of .find(filter, projection, options).
affects: >=1.0.0
gotchaResolve fields (like fullName) require explicit 'dependencies' array in the type definition to avoid missing fields in projection.
fix
Add dependencies: ['name'] or specific nested fields to the field definition.
affects: >=1.0.0
deprecatedThe package is in maintenance mode; no active development.
fix
Consider forking or using alternatives like graphql-compose-mongoose for active support.
affects: >=1.6.0
Errors
Common errors & fixes
Cannot find module 'graphql-to-mongodb'
Package not installed or import path incorrect when using ESM.
fix
Run npm install graphql-to-mongodb --save and ensure import uses ES module syntax.
TypeError: (0 , getMongoDbQueryResolver) is not a function
Using default import instead of named import.
fix
Use import { getMongoDbQueryResolver } from 'graphql-to-mongodb'.
ValidationError: Expected type "PersonFilterType" but got value
Mismatch between GraphQL type and filter data; often due to nested field name typos.
fix
Ensure filter keys match the auto-generated filter type exactly (case-sensitive).
Upgrade
Version history
1.6.5latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for GraphQL schema type definitions and runtime.
Agent activity
2 hits · last 30 days
node
2
Resources
graphql-to-mongodb — npm install graphql-to-mongodb · libregistry