Registry / database / graphql-type-object-id

graphql-type-object-id

JSON →
library1.0.0jsnpmunverified

A GraphQL scalar type that serializes, parses, and validates MongoDB ObjectId values. Version 1.0.0 is current. This package provides a simple way to use ObjectId in GraphQL schemas, supporting both default and named imports (ESM and CJS). It ships TypeScript types and has peer dependencies on graphql 16.x and mongodb 4.x. Key differentiator: minimal, focused; no extra configuration needed.

npm install graphql-type-object-id
INSTALL
IMPORT
SIG · GRAPHQL-TYPE-OBJEC
G
graphql-type-object-id
databasejavascriptv1.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.

GraphQLObjectId
import GraphQLObjectId from 'graphql-type-object-id'
const GraphQLObjectId = require('graphql-type-object-id')
Default import works in TypeScript and ESM; common mistake is using require (CJS) with a package that has no named default export in CJS.
GraphQLObjectId
import { GraphQLObjectId } from 'graphql-type-object-id'
import { GraphQLObjectId } from 'graphql-type-object-id' is actually correct, but note that the package also supports default import; no wrong pattern for named import aside from using CJS require.
Named import also works as the package exports both a default and a named export.
GraphQLObjectId
const { GraphQLObjectId } = require('graphql-type-object-id')
const GraphQLObjectId = require('graphql-type-object-id').default or require('graphql-type-object-id')
For CommonJS, destructure from the module (which has a named export). Direct require returns an object with GraphQLObjectId property.

Shows how to define a GraphQL schema with ObjectId scalar using graphql-type-object-id and @graphql-tools/schema.

import { makeExecutableSchema } from '@graphql-tools/schema'; import GraphQLObjectId from 'graphql-type-object-id'; const typeDefs = ` scalar ObjectId type User { id: ObjectId! name: String! } type Query { user(id: ObjectId!): User } `; const resolvers = { ObjectId: GraphQLObjectId, }; const schema = makeExecutableSchema({ typeDefs, resolvers });
Debug
Known issues
gotchaEnsure peer dependencies (graphql 16.x, mongodb 4.x) are installed; otherwise runtime errors for missing modules.
fix
Install matching versions: npm install graphql@16 mongodb@4
affects: >=1.0.0
gotchaIn CJS environments, using default import pattern (import GraphQLObjectId) may fail. Use destructured require.
fix
Use const { GraphQLObjectId } = require('graphql-type-object-id');
affects: >=1.0.0
breakingVersion 1.0.0 is the initial release; no known breaking changes yet.
fix
Not applicable for version 1.0.0.
affects: <1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'graphql'
Missing or incompatible peer dependency graphql.
fix
Install graphql@16: npm install graphql@16
TypeError: ObjectId is not a function
Incorrect import: using default import in CommonJS without destructuring.
fix
Use: const { GraphQLObjectId } = require('graphql-type-object-id');
Error: You must provide a MongoDB ObjectId instance
Passing a plain string instead of ObjectId to the scalar for serialization/validation.
fix
Ensure input is a valid ObjectId string (24 hex chars) or a MongoDB ObjectId instance.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for GraphQL scalar type
mongodbrequiredPeer dependency for MongoDB ObjectId class
Agent activity
4 hits · last 30 days
node
4
Resources
graphql-type-object-id — npm install graphql-type-object-id · libregistry