我想用Decimal
作为我的 GraphGL 模式中纬度和经度字段的类型。但 GraphQL 仅提供 Float
和 Int
.
有没有解决这个问题的包?
最佳答案
我查看了 this module 的源代码可以想出这个:
/// 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);
}
});
然后,我添加对我的解析器的引用,这样我就可以添加 Decimal
到我的 SDL 代码。
关于node.js - 如何在 GraphQL 模式中使用 'Decimal' 作为字段类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57090030/