Registry / database / graphql-advanced-projection

graphql-advanced-projection

JSON →
library2.0.0jsnpmunverified

GraphQL Advanced Projection (v2.0.0) is a fully customizable Mongoose/MongoDB projection generator for GraphQL resolvers. It decouples GraphQL schema definitions from MongoDB projection configurations, allowing independent changes. Supports interfaces, fragments, and inline fragments. Provides automatic resolver generation for simple field mappings. Released under MIT license. Active maintenance with occasional updates. Differentiates from alternatives by offering separate schema/projection config, easy customization via simple field mapping, and automatic resolver creation.

npm install graphql-advanced-projection
INSTALL
IMPORT
SIG · GRAPHQL-ADVANCED-P
G
graphql-advanced-projection
databasejavascriptv2.0.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.

gqlProjection
import { gqlProjection } from 'graphql-advanced-projection'
const gqlProjection = require('graphql-advanced-projection')
Default export named gqlProjection, but package is ESM-compatible; CommonJS require() works but is not recommended.
project
const { project } = gqlProjection({...})
project is extracted from the returned object; no named import for project itself.
resolvers
const { resolvers } = gqlProjection({...})
resolvers is a side product of gqlProjection; used to merge with custom resolvers.

Setup and usage of graphql-advanced-projection with Mongoose and GraphQL-Tools.

import mongoose from 'mongoose'; import { makeExecutableSchema } from '@graphql-tools/schema'; import { gqlProjection } from 'graphql-advanced-projection'; const UserSchema = new mongoose.Schema({ _id: String, mongoA: String, }); const User = mongoose.model('users', UserSchema); const typeDefs = ` type Query { user(id: ID!): User } type User { userId: ID field1: String field2: String } `; const { project, resolvers } = gqlProjection({ User: { proj: { userId: '_id', field1: 'mongoA', field2: null, }, }, }); const schema = makeExecutableSchema({ typeDefs, resolvers: { ...resolvers, Query: { async user(parent, args, context, info) { const proj = project(info); const result = await User.findById(args.id, proj); return result.toObject(); }, }, User: { field2: () => 'Hello World', }, }, });
Debug
Known issues
gotchaIf a field in proj is set to null, it will not be projected, but if you omit the field entirely, it may be included. Be explicit about null mappings.
fix
Set field to null explicitly to exclude it, or use empty object {} to include all fields.
affects: >=2.0.0
gotchaThe 'project' function must be called with the 'info' object from the resolver context. Passing an empty object or undefined will throw an error.
fix
Always pass info to project(info) inside resolver.
affects: >=1.0.0
breakingIn version 2.0.0, the API changed from requiring a 'projection' option to a nested 'proj' object. Old configuration with 'projection' is not supported.
fix
Migrate to v2: use { proj: { field: 'mongoField' } } instead of { projection: { field: 'mongoField' } }.
affects: <2.0.0
gotchaAutomatic resolver generation for fields like 'userId: "_id"' may conflict if you also manually define a resolver for the same field. Both will be merged.
fix
Avoid defining resolvers for fields already mapped in proj, or override them after merging resolvers.
affects: >=1.0.0
deprecatedUsing CommonJS require() may still work but is deprecated; package is ESM-first.
fix
Use import syntax.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'fieldName')
Calling project(info) without providing the GraphQL info object.
fix
Ensure you pass the info argument from the resolver: project(info).
Error: Invalid projection config for type User: must have a 'proj' property
Using old API where 'proj' is missing or using 'projection' instead.
fix
Use { User: { proj: { ... } } } as config.
Error: Field "field2" has no resolver and is not in projection config
Field is not part of proj or resolver; if it's a computed field, add a resolver.
fix
Either add 'field2' to proj or define a resolver for it.
Error: Duplicate field mapping for User.field1
Defining the same field in both auto-resolver and manual resolver.
fix
Remove manual resolver for auto-mapped fields or use override.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
graphql-advanced-projection — npm install graphql-advanced-projection · libregistry