graphql-js
graphql-js copied to clipboard
Error when input Date in DateTime, db is postgres
I'm trying put a new Date() JS to type DateTime but get this error:
invalid input syntax for type timestamp with time zone: "Invalid date"
Using Postgress 9.1 in DB.
Here is my DateTimeType:
const isValidDate = dateObj => !isNaN(dateObj.getTime());
const DateTimeType = new GraphQLScalarType({
name: 'DateTime',
description: 'Represents a date with time',
serialize(dateObj) {
if (typeof dateObj === 'string') {
return dateObj;
}
if (isValidDate(dateObj)) return dateObj.toJSON();
throw new TypeError('Date cannot represent an invalid Date instance');
},
parseValue(dateString) {
if (typeof dateString === 'string') {
const dateObj = new Date(dateString);
if (isValidDate(dateObj)) return dateObj;
throw new TypeError('Date cannot represent an invalid Date instance');
}
throw new TypeError('It is not a string');
},
parseLiteral(ast) {
if (ast.kind === Kind.STRING) return this.parseValue(ast.value);
throw new TypeError('It is not a string');
},
});
export default {
DateTime: DateTimeType,
}
Sorry to tell you, but it looks like your error is closer to how you interact with Postgres, which is not something I'm very familiar with.
From the sounds of it, some value flowing through your system acting as a date is malformed.