UNPKG

shyft

Version:

Model driven GraphQL API framework

118 lines (117 loc) 5.45 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLUpload = exports.GraphQLTime = exports.GraphQLDate = exports.GraphQLDateTime = exports.GraphQLBigInt = exports.GraphQLCursor = exports.GraphQLJSON = void 0; var graphql_1 = require("graphql"); var util_1 = require("./util"); var dateFns = __importStar(require("date-fns")); var dateTimeRegex = /^\d+-[0-9]{2}-[0-9]{2}[\sT]([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?(\s?[+-]([0-1][0-9]|2[0-3])(:[0-5][0-9])?)?$/; var timeRegex = /^([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?(\s?[+-]([0-1][0-9]|2[0-3])(:[0-5][0-9])?)?$/; var graphql_type_json_1 = require("graphql-type-json"); Object.defineProperty(exports, "GraphQLJSON", { enumerable: true, get: function () { return graphql_type_json_1.GraphQLJSON; } }); exports.GraphQLCursor = new graphql_1.GraphQLScalarType({ name: 'Cursor', description: 'A cursor to define a location in a data set used for pagination.', serialize: util_1.serializeCursor, parseValue: util_1.deserializeCursor, parseLiteral: function (ast) { return ast.kind === graphql_1.Kind.STRING ? util_1.deserializeCursor(ast.value) : null; }, }); exports.GraphQLBigInt = new graphql_1.GraphQLScalarType({ name: 'BigInt', description: 'The `BigInt` scalar type represents a 64-bit integer. As JavaScript ' + 'is limited to a precision of 53 bits on integer values, all `BigInt` ' + 'values will be output as strings.', serialize: String, parseValue: String, parseLiteral: function (ast) { return ast.kind === graphql_1.Kind.STRING ? ast.value : null; }, }); exports.GraphQLDateTime = new graphql_1.GraphQLScalarType({ name: 'DateTime', description: 'The `DateTime` scalar type represents a date and time string.', serialize: function (value) { return value; }, parseValue: String, parseLiteral: function (ast) { if (ast.kind !== graphql_1.Kind.STRING) { throw new graphql_1.GraphQLError("Query error: Can only parse strings but got a: " + ast.kind, [ast]); } if (!dateTimeRegex.test(ast.value)) { throw new graphql_1.GraphQLError('Query error: String is not a valid date time string', [ast]); } var dateString = ast.value.substring(0, 10); var asDate = dateFns.parse(dateString, 'yyyy-MM-dd', new Date()); if (!dateFns.isValid(asDate) || dateString !== dateFns.format(asDate, 'yyyy-MM-dd')) { throw new graphql_1.GraphQLError('Query error: String is not a valid date time string', [ast]); } return ast.value; }, }); exports.GraphQLDate = new graphql_1.GraphQLScalarType({ name: 'Date', description: 'The `Date` scalar type represents a date string.', serialize: function (value) { return value; }, parseValue: String, parseLiteral: function (ast) { if (ast.kind !== graphql_1.Kind.STRING) { throw new graphql_1.GraphQLError("Query error: Can only parse strings but got a: " + ast.kind, [ast]); } var asDate = dateFns.parse(ast.value, 'yyyy-MM-dd', new Date()); if (!dateFns.isValid(asDate) || ast.value !== dateFns.format(asDate, 'yyyy-MM-dd')) { throw new graphql_1.GraphQLError('Query error: String is not a valid date string', [ ast, ]); } return ast.value; }, }); exports.GraphQLTime = new graphql_1.GraphQLScalarType({ name: 'Time', description: 'The `Time` scalar type represents a time string.', serialize: function (value) { return value; }, parseValue: String, parseLiteral: function (ast) { if (ast.kind !== graphql_1.Kind.STRING) { throw new graphql_1.GraphQLError("Query error: Can only parse strings but got a: " + ast.kind, [ast]); } if (!timeRegex.test(ast.value)) { throw new graphql_1.GraphQLError('Query error: String is not a valid time string', [ ast, ]); } return ast.value; }, }); exports.GraphQLUpload = new graphql_1.GraphQLScalarType({ name: 'Upload', description: 'The `Upload` scalar type represents a file upload promise', parseValue: function (value) { return value; }, parseLiteral: function () { throw new graphql_1.GraphQLError('Upload scalar literal not supported'); }, serialize: function () { throw new graphql_1.GraphQLError('Upload scalar serialization not supported. Please upload a file via a multipart request.'); }, });