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

1 Like

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.


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.

What do the values represent? money?

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

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

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!

1 Like

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


Thanks again, will explore the JS ecosystem for sure.

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.