graphql-compose-mongoose
Version:
Plugin for `graphql-compose` which derive a graphql types from a mongoose model.
36 lines • 1.45 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 Decimal128 = mongoose_1.default.Types.Decimal128;
const GraphQLBSONDecimal = new graphql_1.GraphQLScalarType({
name: 'BSONDecimal',
description: 'The `Decimal` scalar type uses the IEEE 754 decimal128 ' +
'decimal-based floating-point numbering format. ' +
'Supports 34 decimal digits of precision, a max value of ' +
'approximately 10^6145, and min value of approximately -10^6145',
serialize: String,
parseValue(value) {
if (typeof value === 'string') {
return Decimal128.fromString(value);
}
if (typeof value === 'number') {
return Decimal128.fromString(value.toString());
}
if (value instanceof Decimal128) {
return value;
}
throw new TypeError('Field error: value is an invalid Decimal');
},
parseLiteral(ast) {
if (ast.kind === graphql_1.Kind.STRING || ast.kind === graphql_1.Kind.INT) {
return Decimal128.fromString(ast.value);
}
return null;
},
});
exports.default = GraphQLBSONDecimal;
//# sourceMappingURL=BSONDecimal.js.map