Created
October 17, 2021 07:38
-
-
Save loicnestler/ce2e1672763308176d2d673be75f2c78 to your computer and use it in GitHub Desktop.
NestJS GraphQL Date Scalar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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