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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews