Skip to content

Proposal: Date support #22

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

Closed
4F2E4A2E opened this issue Feb 5, 2018 · 4 comments
Closed

Proposal: Date support #22

4F2E4A2E opened this issue Feb 5, 2018 · 4 comments
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Milestone

Comments

@4F2E4A2E
Copy link
Contributor

4F2E4A2E commented Feb 5, 2018

I propose to add Date support using this implementation, found here graphql/graphql-js#550

@MichalLytek
Copy link
Owner

MichalLytek commented Feb 5, 2018

Honestly, I prefer the ISO version of the Date scalar, it's human readable and easier to debug:

import { GraphQLScalarType, Kind } from "graphql";

export const DateScalar = new GraphQLScalarType({
  name: "Date",
  description: "Date scalar type",
  parseValue(value: string) {
    return new Date(value); // value from the client input variables
  },
  serialize(value: Date) {
    return value.toISOString(); // value sent to the client
  },
  parseLiteral(ast) {
    if (ast.kind === Kind.STRING) {
      return new Date(ast.value); // value from the client query
    }
    return null;
  },
});

And because I don't want to make this framework so opinionated, I prefer to gave developers freedom to use their own version of Date and other scalars with #5 custom scalar support 😉

@4F2E4A2E
Copy link
Contributor Author

4F2E4A2E commented Feb 5, 2018 via email

@4F2E4A2E
Copy link
Contributor Author

4F2E4A2E commented Feb 5, 2018

A default date speeds up the getting started, but of course, passing in the custom scalar type as presented by you is just fine too:

import { GraphQLScalarType, Kind } from 'graphql';
import { Field, GraphQLObjectType } from 'type-graphql';

export const DateScalar = new GraphQLScalarType({
  name:        'Date',
  description: 'Date scalar type',
  parseValue(value: string) {
    return new Date(value); // value from the client input variables
  },
  serialize(value: Date) {
    return value.toISOString(); // value sent to the client
  },
  parseLiteral(ast) {
    if (ast.kind === Kind.STRING) {
      return new Date(ast.value); // value from the client query
    }
    return null;
  }
});

@GraphQLObjectType()
export class User {
  @Field(type => DateScalar)
  contributorsince: Date;
  @Field()
  alive: boolean;
}

@MichalLytek
Copy link
Owner

Closing - implemented in ba21848 😉

@MichalLytek MichalLytek added Enhancement 🆕 New feature or request Community 👨‍👧 Something initiated by a community labels Feb 6, 2018
@MichalLytek MichalLytek added this to the Beta release milestone Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants