pcharbon

pcharbon

Absinthe: decimal and float

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

Most Liked

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.

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! ^.^;

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!

Last Post!

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.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement