UNPKG

ontotext-platform-custom-scalars

Version:
64 lines (50 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _graphql = require("graphql"); var _Utilities = require("../Utilities"); var TIME_REGEX = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(\.\d+)?(([Z])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/; function getTimePart(dateTime) { return dateTime.substr(dateTime.indexOf('T') + 1); } function validateTime(time) { if (!TIME_REGEX.test(time)) { throw new _graphql.GraphQLError("Invalid value for 'Time' - '".concat(time, "'")); } } /** * Defines custom GraphQLScalarType for RFC-3339 compliant time values. */ var _default = new _graphql.GraphQLScalarType({ name: "Time", description: "An RFC-3339 compliant time scalar with an optional timezone.", serialize: function serialize(value) { if (value instanceof Date) { return getTimePart(value.toISOString()); } else if (typeof value === 'string' || value instanceof String) { if (!isNaN(Date.parse(value))) { return getTimePart(new Date(value).toISOString()); } validateTime(value); return value; } 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) { validateTime(value); return 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); } return this.parseValue(node.value); } }); exports["default"] = _default;