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

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement