Skip to content

unlight/prisma-graphql-type-decimal

Repository files navigation

prisma-graphql-type-decimal

GraphQL type for Prisma's Decimal scalar, wrapper around decimal.js
Created because @prisma/client has bundled decimal.js

Install

npm install prisma-graphql-type-decimal

Usage

Example usage with NestJS GraphQL code first approach:

import { Decimal } from '@prisma/client/runtime/library.js';
import { transformToDecimal } from 'prisma-graphql-type-decimal';
import { Type, Transform } from 'class-transformer';

@ObjectType()
export class User {
  /**
   * Trick to avoid error when using `@Field(() => GraphQLDecimal)`
   */
  @Field(() => GraphQLDecimal)
  @Type(() => Object)
  @Transform(transformToDecimal)
  money: Decimal;

  @Type(() => Object)
  @Transform(transformToDecimal)
  moneys!: Array<Decimal>;
}

// In nested object
class Transfers {
  @Type(() => Object)
  @Transform(transformToDecimal)
  moneys!: Array<Decimal>;
}
class Container {
  @Type(() => Transfers)
  set!: Transfers;
}

License

MIT License (c) 2023