Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we handle Date in graphql and sequelize? #375

Closed
jvbianchi opened this issue Nov 17, 2016 · 7 comments
Closed

How can we handle Date in graphql and sequelize? #375

jvbianchi opened this issue Nov 17, 2016 · 7 comments

Comments

@jvbianchi
Copy link

No description provided.

@mickhansen
Copy link
Owner

However you like really.

@jvbianchi
Copy link
Author

I end up using this solution graphql/graphql-js#497

Thanks

@jedwards1211
Copy link
Contributor

@mickhansen seems to me this package should be using custom scalars for dates and timestamps. Would you accept a PR for that? It would be a breaking change...

@jedwards1211
Copy link
Contributor

jedwards1211 commented Jan 19, 2018

At the very least I'm surprised that graphql-sequelize doesn't return dates as ISO 8601 strings.

@jedwards1211
Copy link
Contributor

@mickhansen also considering that this package provides its own JSON scalar type, it's kind of odd that it doesn't provide types for all sequelize DateTypes.

@intellix
Copy link
Contributor

intellix commented May 13, 2018

We have this:

import { GraphQLScalarType, Kind } from 'graphql';

export const DateType = new GraphQLScalarType({
  name: 'Date',
  parseLiteral(ast) {
    if (ast.kind === Kind.INT) {
      return parseInt(ast.value, 10);
    }
    return null;
  },
  parseValue(value: string): Date {
    return new Date(value);
  },
  serialize(value: string | Date): string {
    return typeof value === 'string' ? value : value.toISOString();
  },
});
// ISO Dates for all Sequelize.Date fields
typeMapper.mapType(type => (type instanceof Sequelize.DATE ? DateType : false));

Don't remember where we got it, but the output is basically:

"user": {
  "createdAt": "2018-04-16T22:04:31.000Z"
}

Might be worth shoving into this library as SequelizeDate and automatically handling it. Don't think I can think of any reason to serialise a Sequelize.DATE into anything but an ISO String as it's supported in all browsers with new Date(user.createdAt).

@mickhansen
Copy link
Owner

GraphQL is handling the serialization at the moment, that seems consistent to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants