the-road-to-graphql / fullstack-apollo-express-postgresql-boilerplate

💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
https://roadtoreact.com
MIT License
1.2k stars 261 forks source link

Server returns deprecated format of 'createdAt' timestamps #19

Closed Ragyal closed 5 years ago

Ragyal commented 5 years ago

Playground somehow displays another representation of the timestamps then expected.

With this query: query { message(id: "1") { text createdAt } }

I get this as result: { "data": { "message": { "text": "Published the Road to learn React", "createdAt": "1539693730039" } } }

rwieruch commented 5 years ago

What kind of representation would you expect?

Ragyal commented 5 years ago

Something like this which is the representation in your tutorial

{ "data": { "message": { "text": "Published the Road to learn React", "createdAt": "Tue Oct 17 2018 22:45:35 GMT+0200 (CEST)" } } }

Because passing the milliseconds representation as cursor for pagination causes an error.

rwieruch commented 5 years ago

What versions of Sequelize and PostgreSQL are you using? Not sure why people get a different time representation suddenly (see http://disq.us/p/1wjbdqd)

Maybe there is an option/configuration in Sequelize to change the time format. Otherwise, why isn't it possible to hash the milliseconds instead?

PS.: Are you the same person from the linked Disqus comment?

Ragyal commented 5 years ago

Yeah I am the same person.

I am using these packages: "apollo-server": "2.1.0", "apollo-server-express": "2.1.0", "sequelize": "version": "4.39.0", "pg": "version": "7.4.3",

and psql version 10.5.

What do you mean by hasing the milliseconds? Converting them back to an ISO format on the client?

I don´t think the problem is with sequelize since I logged the result of models.Message.findById(id) and it contains the right format:

message { dataValues: { id: 1, text: 'Published the Road to learn React (3)', createdAt: 2018-10-17T01:52:06.286Z, updatedAt: 2018-10-17T01:52:05.379Z, userId: 1 }, ...

rwieruch commented 5 years ago

Are you using PostgreSQL version 10 or 11? I think they have released 11 recently, but the tutorial uses 10.

Alternative: Are you on Windows? Could it be that depending on the OS PostgreSQL prefers a different format?

So far I have found this to configure the Date, but I could only find the Sequelize.DATE and Sequelize.DATE(6) types for it. Nothing to configure it.

Otherwise I have found this q&a on StackOverflow: https://stackoverflow.com/questions/16847672/is-there-a-simple-way-to-make-sequelize-return-its-date-time-fields-in-a-partic. Maybe there is something useful for configuring the initial format for createdAt and updatedAt or at least to alter the return value when getting it from the database.

Keep me up to date on this. Would be great to find the reason for this bug, because other people may run into this too.

Ragyal commented 5 years ago

I am using PostgreSQL version 10.5 on a MAC.

Can you try to set both of these types explicitly and tell what's happening with your formatting then?

Going to try that later this day.

Ragyal commented 5 years ago

I think I found what´s causing the different representation.

GraphQL scalars have changed a little bit over time. You updated your package-lock.json 28 days ago including an update of graphql from v14.0.0-rc.2 to v14.0.2. GraphQLs string scalar definition changed a little bit with as you can see here.

Before that commit a string was serialized with String(value) (your working format) but now it uses value.valueOf() (format as ms which is not working as cursor) in case of Date.

rwieruch commented 5 years ago

Thanks for the update! I will try to check it the next days on how to make it work with cursors.

Ragyal commented 5 years ago

Replacing [Sequelize.Op.lt]: cursor, by [Sequelize.Op.lt]: new Date(Number(cursor)).toISOString(), hotfixes the issue for testing with playground.

For a real fix I would prefer a custom date scalar (there are already packages for that) since dates are something very common to deal with when handling data.

rwieruch commented 5 years ago

Good hotfix! What are the packages for the custom date scalars? I will check these out.

Thanks for your help on this!

Ragyal commented 5 years ago

It´s called graphql-iso-date you can install it with

npm install --save graphql-iso-date or yarn add graphql-iso-date

and here is a link to their repository.

rwieruch commented 5 years ago

Fixed in https://github.com/the-road-to-graphql/fullstack-apollo-react-express-boilerplate-project/commit/acfde4fc846acac7ed913ec296cbca3a5552b92e but I keep the issue open for others who run into it. Thanks for helping out @Ragyal 💯