GraphQL type for Prisma's Decimal scalar, wrapper around decimal.js
Created because @prisma/client
has bundled decimal.js
npm install prisma-graphql-type-decimal
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;
}
MIT License (c) 2023