ontotext-platform-custom-scalars
Version:
A library of customized GraphQL scalars for Ontotext Platform
62 lines (49 loc) • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _graphql = require("graphql");
var _Utilities = require("../Utilities");
var RFC_3339_DATETIME_REGEX = /^(\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60))(\.\d+)?(([Z])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
function validateDateTime(dateTime) {
if (!RFC_3339_DATETIME_REGEX.test(dateTime)) {
throw new _graphql.GraphQLError("Invalid value for 'Date' - '".concat(dateTime, "'"));
}
}
/**
* Defines custom GraphQLScalarType for RFC-3339 compliant DateTime values.
*/
var _default = new _graphql.GraphQLScalarType({
name: "DateTime",
description: "An RFC-3339 compliant DateTime Scalar with an optional timezone.",
serialize: function serialize(value) {
if (value instanceof Date) {
return value.toISOString();
} else if (typeof value === 'string' || value instanceof String) {
if (!isNaN(Date.parse(value))) {
return new Date(value).toISOString();
}
}
throw new _graphql.GraphQLError("Expected '".concat(this.name, "' value, but got '").concat(JSON.stringify(value), "'."));
},
parseValue: function parseValue(value) {
if (typeof value === 'string' || value instanceof String) {
validateDateTime(value);
if (!isNaN(Date.parse(value))) {
return new Date(value);
}
}
throw new _graphql.GraphQLError("Expected value in '".concat(this.name, "' format, but got '").concat(JSON.stringify(value), "'."));
},
parseLiteral: function parseLiteral(node) {
var type = node.kind;
if (type !== _graphql.Kind.STRING) {
(0, _Utilities.throwConversionError)(type, this.name);
}
var value = node.value;
validateDateTime(value);
return new Date(value);
}
});
exports["default"] = _default;