Registry / database / graphql-scalar-objectid

graphql-scalar-objectid

JSON →
library0.1.2jsnpmunverified

A GraphQL scalar type for MongoDB ObjectId, packaged as a simple npm module. Current version 0.1.2 (no recent releases). It provides serialize, parseValue, and parseLiteral functions to handle ObjectId in GraphQL schemas. Lightweight with no dependencies beyond graphql peer dependency. Differentiator: minimal implementation focused solely on ObjectId, unlike graphql-type-json which is broader. Suitable for Node.js >=6 with any GraphQL version.

npm install graphql-scalar-objectid
INSTALL
IMPORT
SIG · GRAPHQL-SCALAR-OBJ
G
graphql-scalar-objectid
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.

GraphQLObjectId
const GraphQLObjectId = require('graphql-scalar-objectid')
const { GraphQLObjectId } = require('graphql-scalar-objectid')
Package exposes a single default export (CommonJS). Do not destructure.
GraphQLObjectId (ESM)
import GraphQLObjectId from 'graphql-scalar-objectid'
import { GraphQLObjectId } from 'graphql-scalar-objectid'
No named export; default import only. Package does not ship ESM; this requires transpilation.
GraphQLObjectId in TypeScript
const GraphQLObjectId = require('graphql-scalar-objectid')
import GraphQLObjectId from 'graphql-scalar-objectid' (may fail without esModuleInterop)
No type definitions included. Use @types/graphql for GraphQL types. May need esModuleInterop for default import.

Defines a GraphQL object type with _id field using GraphQLObjectId scalar and runs a simple query.

const { GraphQLObjectType, GraphQLString, GraphQLSchema, graphql } = require('graphql'); const GraphQLObjectId = require('graphql-scalar-objectid'); const UserType = new GraphQLObjectType({ name: 'User', fields: { _id: { type: GraphQLObjectId }, name: { type: GraphQLString } } }); const schema = new GraphQLSchema({ query: UserType }); // Example query (requires a resolver) graphql(schema, '{ _id name }', null, { User: { _id: new (require('mongodb').ObjectId)('507f1f77bcf86cd799439011'), name: 'John' } }).then(console.log);
Debug
Known issues
gotchaPackage uses CommonJS only; no ESM support or type definitions.
fix
Use require() or transpile with Babel/TypeScript. Install @types if needed.
affects: >=0.0.0
gotchaGraphQLObjectId expects string input or a valid ObjectId hex string; non-hex strings or numbers will throw.
fix
Ensure input values are 24-character hex strings or ObjectId instances.
affects: >=0.0.0
gotchaNo validation for incorrect ObjectId format; GraphQL errors may be cryptic.
fix
Validate ObjectId strings before passing to GraphQL, or use a custom scalar with error messages.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: ObjectId is not a function
Attempted to use GraphQLObjectId as a constructor or function instead of a GraphQL scalar type.
fix
Use it as a type in GraphQL schema definitions, e.g., type: GraphQLObjectId, not new GraphQLObjectId().
Expected value of type "ObjectId" but got: ...
Input value is not a valid ObjectId string (e.g., wrong length, non-hex characters).
fix
Provide a 24-character hex string or a MongoDB ObjectId instance (serialized to string).
Cannot find module 'graphql-scalar-objectid'
Package not installed or import path incorrect.
fix
Run npm install graphql-scalar-objectid or yarn add graphql-scalar-objectid.
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency; required for GraphQL type definitions
Agent activity
4 hits · last 30 days
node
4
Resources
graphql-scalar-objectid — npm install graphql-scalar-objectid · libregistry