Registry / database / graphql-projection

graphql-projection

JSON →
library0.1.2jsnpmunverified

A utility that creates a MongoDB projection object from a GraphQL query's resolve info, enabling efficient MongoDB queries by selecting only the fields requested in the GraphQL query. Version 0.1.2 is the latest and likely final release, with no recent updates. It supports custom projections for computed fields via a `projection` property in GraphQL type definitions. Differentiators include a simple API (one function), support for custom projections as arrays or space-separated strings, and no runtime dependencies aside from graphql peer. However, it is not actively maintained and lacks TypeScript definitions or modern ESM support.

npm install graphql-projection
INSTALL
IMPORT
SIG · GRAPHQL-PROJECTION
G
graphql-projection
databasejavascriptv0.1.2
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.

makeProjector
✓ import makeProjector from 'graphql-projection'
✗ const makeProjector = require('graphql-projection');
Default export; CommonJS require works but TypeScript may need esModuleInterop.
✓ import makeProjector from 'graphql-projection'
✗ import { makeProjector } from 'graphql-projection'
There is no named export; only a default export.
✓ const projector = makeProjector(schema);
✗ const projector = makeProjector();
makeProjector requires a GraphQL schema argument; omitting it will throw.

Demonstrates creating a projector from a schema and using it in a resolve function to obtain a MongoDB projection object.

import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLNonNull, GraphQLInt, graphql } from 'graphql'; import makeProjector from 'graphql-projection'; const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ user: { type: new GraphQLObjectType({ name: 'User', fields: () => ({ id: { type: new GraphQLNonNull(GraphQLInt) }, name: { type: new GraphQLNonNull(GraphQLString) }, }), }), args: { id: { type: new GraphQLNonNull(GraphQLInt) } }, resolve: (root, args, context, info) => { const projector = makeProjector(schema); const projection = projector(info); console.log(projection); // e.g., { id: 1, name: 1 } return { id: args.id, name: 'Alice' }; }, }, }), }), }); graphql(schema, '{ user(id: 1) { name } }').then(console.log);
Debug
Known issues
gotchamakeProjector must be called with a GraphQL schema; calling it without arguments will throw an error.
fix
Always pass a valid GraphQLSchema instance: const projector = makeProjector(schema);
affects: >=0.0.0
deprecatedPackage appears unmaintained; last release in 2016, no repository activity.
fix
Consider alternatives like graphql-parse-project or implement custom logic.
affects: >=0.0.0
gotchaCustom projections defined as space-separated strings may have subtle bugs if field names contain spaces.
fix
Use array syntax for clarity: projection: ['field1', 'field2']
affects: >=0.0.0
gotchaDoes not support fragments or inline fragments for nested types; projections may be incomplete.
fix
Manually extend projection logic or use a more modern library.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: projector is not a function
Forgetting to call makeProjector with the schema before using it as a function.
fix
Ensure you call makeProjector(schema) and assign the result: const projector = makeProjector(schema);
Cannot read property 'fieldNodes' of undefined
Passing an undefined or malformed resolveInfo object to the projector.
fix
Always pass the 4th argument (info) from the resolve function: projector(resolveInfo);
Expected schema to be a GraphQLSchema
Passing a non-schema or invalid object to makeProjector.
fix
Pass a valid GraphQLSchema instance: const schema = new GraphQLSchema({...});
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for parsing GraphQL schemas and queries
Agent activity
16 hits · last 30 days
node
14
Resources