UNPKG

@nestjs/graphql

Version:

Nest - modern, fast, powerful node.js web framework (@graphql)

27 lines (26 loc) 803 B
import { GraphQLScalarType, Kind } from 'graphql'; export const GraphQLTimestamp = new GraphQLScalarType({ name: 'Timestamp', description: '`Date` type as integer. Type represents date and time as number of milliseconds from start of UNIX epoch.', serialize(value) { return value instanceof Date ? value.getTime() : null; }, parseValue(value) { try { return value !== null ? new Date(value) : null; } catch { return null; } }, parseLiteral(ast) { if (ast.kind === Kind.INT) { const num = parseInt(ast.value, 10); return new Date(num); } else if (ast.kind === Kind.STRING) { return this.parseValue(ast.value); } return null; }, });