Supporting decimals in prisma schema
My schema has fields like so:
Then when I write an operation to fetch Payment rules, I get the following error:
How to resolve??
4 Replies
Hi @Noah. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The issue you're encountering is related to how Wasp handles Prisma's Decimal type when serializing data. The Decimal type is not directly serializable to JSON, which is causing the type incompatibility error you're seeing. To resolve this issue, you have a couple of options:
1. Convert Decimal to string or number:
You can modify your query to convert the Decimal fields to strings or numbers before returning them. This approach ensures that the data is serializable.
2. Use Prisma's toJSON method:
Prisma provides a
toJSON
method that can be used to convert the entire result to a JSON-serializable format. This method automatically handles Decimal fields.
Both of these approaches should resolve the type incompatibility error you're experiencing. The first option gives you more control over how the Decimal fields are converted, while the second option is more straightforward but converts all fields to JSON-serializable formats.
It's worth noting that this issue arises because Wasp uses SuperJSON for serialization, which doesn't natively support Prisma's Decimal type. The solutions provided above work around this limitation by converting the data to a format that SuperJSON can handle.
For more information on Prisma configuration in Wasp, you can refer to the Wasp documentation on Prisma Configuration.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @Noah, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Did that work @Noah
?
This might be related to how we handle operation results serialisation - we might not support
Decimal
as a Prisma type. We should probably implement a custom serializer for the Prisma.Decimal type: https://github.com/flightcontrolhq/superjson?tab=readme-ov-file#decimaljs--prismadecimal
@Noah in the meantime, I'd recommend transforming that field into a string on the server and then sending it to the clientGitHub
GitHub - flightcontrolhq/superjson: Safely serialize JavaScript exp...
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more. - flightcontrolhq/superjson
Yeah I just did the string conversion and it worked fine, thanks!
I did like
type TypesafeResult = Omit<TypeUnsafeType, 'decimalField'> & { decimalField: string}
in the operation