I’m having an issue where it seems queries remove ...
# orm-help
m
I’m having an issue where it seems queries remove trailing zeroes after the decimal. I haven’t used GraphQL enough outside of Prisma to know where in the ecosystem the problem lies but it’s turning out to be a major pain point. Happens right in GraphQL playground as well, so shouldn’t be anything outside of Prisma causing this, any ideas or suggestions? Using a toFixed() everywhere a query is called on the frontend just doesn’t seem sustainable when we know we always want the returned value to be a specific format.
k
Can't you do that on the backend (via middleware/transform/etc) if you know the number of zeros you want? Because, really, 12.40000 is the same as 12.4 and that's perfectly valid.
m
One example is we have review information we’re looking to round to 1 decimal place we would prefer to avoid handling inconsistencies multiple times in the frontend. Another is how we display prices where we want 2 decimal places. If we foresaw this pricing could’ve been a bit different but using 2 numbers as a structure to display all floats seems a bit verbose especially for something less critical like reviews.
In fact pricing should be 2 integer values this was more for testing purposes but unsure how I’d handle other use cases like our reviews
k
Store both as floats, and in the query resolver, return .toFixed(2) for prices, .toFixed(1) for reviews. You could use a return Transform, I believe. https://www.apollographql.com/docs/apollo-server/api/graphql-tools/#transform
👍 1
a
Intl API to format currency when displayed in the front end? Instead of toFixed()
👍 1
k
sure, you can do that.
The reviews and prices are different queries, right? So that logic would be part of two different transforms
a
Also if youre storing floats, use a library like dinero or currency to perform operations
👍 1
m
Thanks, I will check out these options. @Kyron What I was doing right now was using toFixed in a resolver actually and even though the server will log the correct number of trailing zeroes the GraphQL playground removes the trailing zeroes but I will take a look into that link. Will also checkout the libs Alberto mentioned