如何在 GraphQL 模式中使用“十进制”作为字段类型?

Deb*_*Das 8 node.js express graphql apollo-server

我想Decimal在我的 GraphGL 模式中用作纬度和经度字段的类型。但是 GraphQL 仅提供FloatInt. 有没有解决这个问题的包?

Net*_*bby 3

我查看了该模块的源代码可以得出以下结论:




///  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);
  }
});
Run Code Online (Sandbox Code Playgroud)

然后,我将引用添加到我的解析器中,这样我就可以添加Decimal到我的 SDL 代码中。