node.js - 如何在 GraphQL 模式中使用 'Decimal' 作为字段类型?

标签 node.js express graphql apollo-server

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

最佳答案

我查看了 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/

相关文章:

node.js - 无法安装 Topojson

jquery - csurf AJAX 调用 - 无效的 CSRF token

javascript - 如何创建模式自定义以使用来自 gatsby 博客的可选字段 "featureImage"

node.js - 用户登录时重新连接套接字

javascript - 更少的 CSS 服务器端,但没有 node.js?

node.js - 如何从 NodeJS 中的 stdin 响应获取 Vertica 副本?

mysql - 为什么在 Node.js 中使用 escape() 时 MySQL 不显示错误?

javascript - 确定两个坐标是否在所需半径内

reactjs - 具有 Flux 模式的中继缓存?

graphql - 在 Strapi GraphQL 查询中获取图像格式