Registry / database / graphql-mongodb-projection

graphql-mongodb-projection

JSON →
library1.1.1jsnpmunverified

Generates MongoDB projection objects from GraphQL resolve info, enabling automatic field selection based on GraphQL queries. Version 1.1.1 (latest) is a mature, stable package with no recent releases. It supports fields, inline fragments, fragment spreads, and the Relay Edge Node pattern. Differentiators include lightweight zero-dependency implementation (only peer dependency on graphql) and simple integration with MongoDB find operations. The package is unmaintained since 2017 but remains functional for projects using older GraphQL versions.

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

graphqlMongodbProjection
import graphqlMongodbProjection from 'graphql-mongodb-projection'
const graphqlMongodbProjection = require('graphql-mongodb-projection')
Default export, ESM-only package (no CommonJS support). Use ES import syntax.
graphqlMongodbProjection
import gmp from 'graphql-mongodb-projection'
import { graphqlMongodbProjection } from 'graphql-mongodb-projection'
Default export, not named export. Use any name for the default import.
graphqlMongodbProjection
const graphqlMongodbProjection = require('graphql-mongodb-projection').default
const graphqlMongodbProjection = require('graphql-mongodb-projection')
In CommonJS (Node), require returns an ES module namespace object; access .default for the function.

Shows how to generate MongoDB projection from GraphQL info and pass it to findOne.

import graphqlMongodbProjection from 'graphql-mongodb-projection'; import { GraphQLObjectType, GraphQLString, GraphQLNonNull } from 'graphql'; const UserType = new GraphQLObjectType({ name: 'User', fields: () => ({ _id: { type: GraphQLString }, email: { type: GraphQLString }, firstname: { type: GraphQLString }, lastname: { type: GraphQLString }, }) }); const userQuery = { type: UserType, args: { _id: { type: new GraphQLNonNull(GraphQLString) } }, resolve(parent, args, context, info) { const projection = graphqlMongodbProjection(info); return db.collection('users').findOne({ _id: args._id }, projection); } };
Debug
Known issues
breakingPackage uses ES modules (import/export) only. Requiring via CommonJS requires accessing .default.
fix
Use import or destructure .default if using require.
affects: >=1.0.0
deprecatedPackage has not been updated since 2017 and may not work with modern GraphQL versions (>=15). GraphQL resolver signature changed; info parameter structure may differ.
fix
Test with your GraphQL version. Consider using alternatives like @nicokaiser/graphql-mongodb-projection or manual field extraction.
affects: >=1.0.0
gotchaThe projection function does not automatically transform field names (e.g., camelCase to snake_case). Must pass mapping object manually.
fix
Use the second parameter for field mappings: graphqlMongodbProjection(info, { 'firstName': 'first_name' }).
affects: >=1.0.0
gotchaThe module cannot project fields that are nested inside arrays or subdocuments automatically. Only top-level fields are included.
fix
Manually add nested fields via the mapping object or use a different library.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: graphqlMongodbProjection is not a function
In CommonJS, require returns an ES module namespace object, not the default export.
fix
Use const graphqlMongodbProjection = require('graphql-mongodb-projection').default;
Cannot find module 'graphql-mongodb-projection'
Package not installed or not in node_modules.
fix
Run npm install graphql-mongodb-projection (note: package name is all lowercase).
graphqlMongodbProjection(...) is returning an empty object
GraphQL info argument is undefined or the resolver's fourth parameter is not being passed correctly.
fix
Ensure the resolve function has four parameters: (parent, args, context, info) and info is the fourth.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency, uses GraphQL resolve info
Agent activity
7 hits · last 30 days
node
6
Resources
graphql-mongodb-projection — npm install graphql-mongodb-projection · libregistry