Long question short, should I create a Decimal
scalar? And if so is there a way to preserve it as an INT with trailing zeros instead of needing to convert it to a string?
There is a need from my consumers to always receive decimals even if the trailing values are 0.
For example, instead of receiving 10
they want to receive 10.0
. This isn’t a schema-wide rule at the moment, it’s mostly specific to a handful of data-point fields in an isolated query. However, it’s not to say it won’t transcend the specific fields/query in the future. Here is additional info:
Schema:
type Query {
getValues: Values
}
type Values {
valueOne: Float
valueTwo: Float
}
Query:
query GetValues {
getValues {
valueOne
valueTwo
}
}
Response:
{
"data": {
"getValues": {
"valueOne": 10 // value returned from resolver is "10.0"
"valueTwo": 10.4 // value returned from resolver is "10.4"
}
}
}
Resolver helper logic:
function applyValueDecimalPlaces(number: number): string {
return number.toLocaleString('en-us', {
maximumFractionDigits: 1,
minimumFractionDigits: 1,
});
}
applyValueDecimalPlaces(9.96) // returns "10.0"
applyValueDecimalPlaces(10.36) // returns "10.4"
So, the logic seems to work as expected all the way up to the resolver-schema hand off.. the resolver is providing 10.0
in this case but the response is 10
. How do I ensure the consumer receives 10.0
and not 10
?