strapi / strapi

🚀 Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.
https://strapi.io
Other
63.71k stars 8.11k forks source link

Decimal presented as Float in GQL schema #19327

Open Szer opened 9 months ago

Szer commented 9 months ago

Bug report

Create a field with "type": "decimal" in Strapi. It is represented as a Float Scalar in the GQL Schema. https://github.com/strapi/strapi/blob/6a58621932ad3d83bf9d6928c1871e7906adcd59/packages/plugins/graphql/server/src/services/constants.ts#L54

Required System information

Describe the bug

Decimals can't represented as numbers with floating point as they have arbitrary level of precision and prone to rounding errors. Strapi documentation states that Strapi supports both Floats and Decimals where Decimals should be used to avoid rounding errors.

Mapping decimal to float in GQL type system breaks that assumption as now all clients will use single or double floating numbers when talking to Strapi with such schema

Steps to reproduce the behavior

Create a field with "type": "decimal" in Strapi. It is represented as a Float Scalar in the GQL Schema.

Expected behavior

It should be its own custom scalar called Decimal (like JSON, Date or DateTime Strapi already uses for other types`

Screenshots

N/A

Code snippets

N/A

Additional context

It is not possible in general to intervene on the Nexus level and retype the OutputField because information that field had "decimal" type in Strapi already lost on Nexus level.

alexandrebodin commented 7 months ago

Hi @Szer,

Can you provide an example of invalid data you have been able to store ? the db data is supposed to be a decimal and should still be stored at the configured precision and then be usable correctly.

Considering decimals are not a native js type we will always handle numbers and the end user will have to either use a dedicated math lib or receive the data in another language to make a difference 🤔