Decimal numbers in Optimizely Graph
Storing prices as decimal numbers on a commerce website and planning to expose them through Optimizely Graph? It might not be as straightforward as it seems.
When working with a commerce website, having a price concept is a must-have. There is no commerce without the price. In the world of .Net, the price or rather the amount of price is represented by the type of decimal. This is also how it is done in Optimizely Commerce. Decimal numbers can be seen everywhere in the catalog content types.
Hence, it’s completely natural to assume that we also should use decimal type when exposing commerce content through Optimizely Graph. But when actually doing it one can be unpleasantly surprised.
This is exactly the case I was facing recently. After pushing decimal numbers to the Graph, what by the way was done without any problem, it happened after trying to fetch the data. The response to the GraphQL query showed an error:
"message": "Float cannot represent non numeric value: { Scale: 9 }"
This was completely strange. After hours of investigation, I finally found a reason. It appears that Optimizely Graph before sending data to the GraphQL server and before serialization is changing data by deconstructing properties into a dictionary - unless the type is primitive. Since decimal is not a primitive type and contains one property named “Scale”, it was deconstructed into the dictionary with one entry, hence the error.
But the site still needs to work. What are the alternatives?
Alternative number one:
Convert decimals into double types. This is how Optimizely does it in its Graph properties. It might be okay, but there will always be a question of whether there is no problem with precision that might be hard to find in the software's future cycle.
Alternative number two:
Return the decimal number as a string. This approach is good for showing data, but it might not be efficient enough when the same property must be used as a facet or sort order factor.
And finally, number three:
Use a concept of Financial Integers. It’s about multiplying the decimal number by a hundred and storing the result as integer type. For example, $10 becomes 1000, and $20.25 converts into 2025. This way we can have all the filtering/sorting done with numbers. The frontend application, however, needs to remember that this integer number must be converted into a decimal substitute in FE technology by dividing it by a hundred before showing it to the end user.
Which one is the best option? As always, it depends on the actual use case. I’m pretty sure though, that one day, support for decimals will be part of Optimizely Graph.
More articles
Optimizely SaaS CMS + Coveo Search Page
Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data from the Optimizely SaaS CMS - all during just a break between coffee refills.
Block type selection doesn't work
Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of available blocks. Instead, you're greeted with an empty view and a console error. What's going on?
Custom payment in Optimizely Commerce 14
The Commerce platform offers a few of the most common payment methods. However, they are not always enough, and it is absolutely natural to need a fully customized approach.
SEO redirects in .NET + Optimizely
Nice and easy way to add necessary SEO redirects
Optimizely Autocomplete (Statistics)
A user starts typing in the search input, and it returns suggestions for phrases they might be searching for. How to achieve this?
Global Components Builders
Implementing global common components every site consists of