graphql-compose-mongoose
Version:
Plugin for `graphql-compose` which derive a graphql types from a mongoose model.
27 lines • 1.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongoose_1 = __importDefault(require("mongoose"));
const graphql_1 = require("graphql-compose/lib/graphql");
const ObjectId = mongoose_1.default.Types.ObjectId;
const GraphQLMongoID = new graphql_1.GraphQLScalarType({
name: 'MongoID',
description: 'The `ID` scalar type represents a unique MongoDB identifier in collection. ' +
'MongoDB by default use 12-byte ObjectId value ' +
'(https://docs.mongodb.com/manual/reference/bson-types/#objectid). ' +
'But MongoDB also may accepts string or integer as correct values for _id field.',
serialize: String,
parseValue(value) {
if (!ObjectId.isValid(value) && typeof value !== 'string' && typeof value !== 'number') {
throw new TypeError('Field error: value is an invalid ObjectId');
}
return value;
},
parseLiteral(ast) {
return ast.kind === graphql_1.Kind.STRING || ast.kind === graphql_1.Kind.INT ? ast.value : null;
},
});
exports.default = GraphQLMongoID;
//# sourceMappingURL=MongoID.js.map