pcharbon

pcharbon

I just fell in love with Elixir and currently in the process of refactoring our API from hapijs into a phoenix/absinthe one.
I am using the excellent Decimal library to represent monetary values for business logic inside the API as floating-point arithmetic is imprecise.

Unfortunately Absinthe serializes the Decimal custom type as string instead of float. The angular front-end expect those decimal values to be Float as defined by the standard graphql types.

Refactoring the front-end to transform all those strings to javascript numbers would be quite an undertaking.

I am currently looking at how to implement this at the field resolver level but being pretty new to Elixir any suggestions on how to solve this would be appreciated.

Thanks in advance

Showing Posts 1 to 8

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @pbcharbon. The reason that :decimal types get serialized as a string is that Floats to not preserve precision.

decimal values to be Float as defined by the standard graphql types

Float is indeed a standard GraphQL type, but if you use :decimal in your schema you aren’t using a standard GraphQL type, you’re using a custom scalar. Custom scalars are free to serialize however they want.

Unless you’re doing math on the front end with the javascript I wouldn’t convert the strings to numbers at all. You risk losing precision.

pcharbon

pcharbon OP

There is some math involved on the front end for some of the fields.
It seems the only solution in my case would be to transform them during
calculation on the front end.
Hopefully this should not impact performance.

Thanks for the quick and helpful response.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What do the values represent? money?

pcharbon

pcharbon OP

Mostly prices and quantities. I moved some of the calculation to the server
side which actually made sense. The ones left I will handle on the
front-end inside the calculation functions and return the results as
strings.

Thanks again and great book btw, that’s how I introduced myself to Absinthe.
Looking forward to the Apollo client section, just to see if I am doing it
the “proper” way.

Thanks again

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I would very much consider a javascript decimal library in that case then, javascript floats will lose precision just like every other language, and this is generally very bad for money. Thanks for the kind words about the book!

OvermindDL1

OvermindDL1

I may be sounding repetitive, but as always Never Serialize Money As A Float! ^.^;
So a string is entirely fine, or an object of a numerator and denominator or so.

Then you need to be using a Good javascript library that handles money right (a good indicator is if it even accepts a float as input then it is ‘probably’ broken).

Er, yes this! ^.^;

pcharbon

pcharbon OP

Thanks again, will explore the JS ecosystem for sure.

conradwt

conradwt

I was experiencing what I thought was an issue and your answer made it clear that the :decimal scalar not being a standard GraphQL type in Absinthe. In any case, I can definitely work string because the values represent total gross and there’s no additional calculation with them.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews