Zendro-dev / graphql-server-model-codegen

Command line utility to auto-generate the structure files for a graphql server
MIT License
1 stars 2 forks source link

Time type not compatible between graphql and sequelize. #141

Open vsuaste opened 4 years ago

vsuaste commented 4 years ago

GraphqlTime type from graphql-iso-date is not compatible with Sequelize time type. The problem is that when graphql parses a time type it actually creates a Date with the current day and the time provided by the user. The following code is taken from the graphql-iso-date library

// Parses an RFC 3339 compliant time-string into a Date.
// It does this by combining the current date with the time-string
// to create a new Date instance.
//
// Example:
// Suppose the current date is 2016-01-01, then
// parseTime('11:00:12Z') parses to a Date corresponding to
// 2016-01-01T11:00:12Z.
export const parseTime = (time: string): Date => {
  const currentDateString = new Date().toISOString()
  return new Date(currentDateString.substr(0, currentDateString.indexOf('T') + 1) + time)
}

When we pass to sequelize a time already parsed by graphql, then it will throw an error like this one:

SequelizeDatabaseError: invalid input syntax for type time: "2020-07-29T10:15:30.000+00:00"

And the error is because Sequelize expects only to receive a time type such as 10:15:30Z. The curious thing is that the same happens for Date type, graphql parses and automatically add the time extra info, but sequelize is capable to extract only the Date.

asishallab commented 4 years ago

It appears the library we use is buggy. I don't see where adding a date to a time is correct behavior. As the new repository is doing the same, let's not use that either. Luckily it seems to be easy to define and implement home rolled scalar types like Date, DateTime, Time, Aliens

Look at these resources:

How to solve this

We implement our own Date, DateTime, and Time scalars. We need to take into account AJV validation (see above).

asishallab commented 4 years ago

For now remove Time from our global schema, because it appears wrong (to @asishallab) that a ISO-Time is converted to an instance with a Date. The latter actually causes trouble when being fed into Sequelize.