Reputation: 569
I want to use Decimal
as a type for latitude & longitude field in my GraphGL schema. But GraphQL provides only Float
and Int
.
Is there is any package to solve this?
Upvotes: 13
Views: 18671
Reputation: 1414
I looked at the source code for this module can came up with this:
/// I M P O R T
import Big from "big.js";
import { GraphQLScalarType, Kind } from "graphql";
/// E X P O R T
export default new GraphQLScalarType({
name: "Decimal",
description: "The `Decimal` scalar type to represent currency values",
serialize(value) {
return new Big(value);
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
// @ts-ignore | TS2339
throw new TypeError(`${String(ast.value)} is not a valid decimal value.`);
}
return Big(ast.value);
},
parseValue(value) {
return Big(value);
}
});
Then, I add the reference to my resolver so I can just add Decimal
to my SDL code.
Upvotes: 3