All files / graphql/scalars/date date.scalar.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38      1x 1x 5x               2x                 2x 1x   1x                 2x      
import { CustomScalar, Scalar } from '@nestjs/graphql';
import { ASTNode, Kind } from 'graphql';
 
@Scalar('Date', () => Date)
export class AppDateScalar implements CustomScalar<number, Date> {
  public description = 'Date custom scalar type';
 
  /**
   * Value parser.
   * @param value Input value.
   * @returns Parsed value.
   */
  public parseValue(value: number | unknown): Date {
    return typeof value === 'number' ? new Date(value) : new Date();
  }
 
  /**
   * Literal parser.
   * @param ast AST node.
   * @returns Parsed literal.
   */
  public parseLiteral(ast: ASTNode): Date {
    if (ast.kind === Kind.INT) {
      return new Date(ast.value);
    }
    return new Date();
  }
 
  /**
   * Value serializer.
   * @param value Input value.
   * @returns Serialized value.
   */
  public serialize(value: Date | unknown): number {
    return value instanceof Date ? value.getTime() : 0;
  }
}