Skip to content

GraphQL type for Prisma's Decimal scalar, wrapper around decimal.js

Notifications You must be signed in to change notification settings

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