Skip to content

Instantly share code, notes, and snippets.

@loicnestler
Created October 17, 2021 07:38
Show Gist options
  • Save loicnestler/ce2e1672763308176d2d673be75f2c78 to your computer and use it in GitHub Desktop.
Save loicnestler/ce2e1672763308176d2d673be75f2c78 to your computer and use it in GitHub Desktop.
NestJS GraphQL Date Scalar
import { CustomScalar, Scalar } from '@nestjs/graphql';
import { Kind } from 'graphql';
@Scalar('Date', () => Date)
export class DateScalar implements CustomScalar<string, Date> {
description = 'Date custom scalar type';
parseValue(value: string): Date {
return new Date(value); // value from the client
}
serialize(value: Date): string {
return new Date(value).toISOString(); // value sent to the client
}
parseLiteral(ast: any): Date {
if (ast.kind === Kind.INT) {
return new Date(ast.value);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment