将INT参数传递给突变继电器和打字稿

发布于 2025-02-08 04:28:15 字数 1340 浏览 1 评论 0原文

我使用的架构第一种方法具有以下输入类型,用于

input VerifyBankAccountInput {
  bankAccountId: ID!
  amount1Cents: Int!
  amount2Cents: Int!
}

在我使用的tyspript中继电器上的前端突变。在生成的graphql.ts文件中,int类型被映射到号码。在将其传递给突变之前,我确保这两个金额为number(而不是String)。但是,在GraphQl的帖子查询中,我注意到这些值成为float和我的服务器错误。它在DEV和PROD之间或两个值之间并不是真正的一致性(如下所示)(都使用完全相同的代码)。

输入:{bankAccountid:“ somerelayid”,金额1cents:56.9999999999999999,量,金额2cents:61}

如何确保如何确保number作为int而不是突变中的float

我尝试在此处遵循建议 https:/https:/ /spin.atomicobject.com/2018/11/05/usish-an-in-int-type-in​​-typescript/ ,但由于graphql.ts中生成的代码而被卡住了号码。获取错误:键入'bigint'不能分配给'number'。

我可以去更改生成的graphql.ts中的类型,但是每次都会被覆盖我使用“ realay-compiler -src ./src -schema./schema.graphql -language tyspercript -customscalars.url = url = url -customscalars.localdate = localdate -customScalararrarys.customscalarars.s.instant = string = string string”

我也可以简单地围绕编号确保没有小数点位置,但对我来说似乎有些脆弱。

使用模式的第一种方法处理此操作的正确方法是什么?

I am using a schema first approach have the following Input type for a mutation

input VerifyBankAccountInput {
  bankAccountId: ID!
  amount1Cents: Int!
  amount2Cents: Int!
}

On the frontend I am using relay with typescript. In the generated graphql.ts file the Int type gets mapped to number. I make sure these two amounts are number (and not string) before I pass it to the mutation. However, in the POST query from graphql I notice that these values become Float and my server errors because of that. It's not really consistent across dev and prod or across the two values either as shown below (both use exact same code).

input:{bankAccountId: "SomeRelayId", amount1Cents: 56.99999999999999, amount2Cents: 61}

How do I make sure that the number is sent as an Int and not a Float in a mutation?

I tried following the suggestions here https://spin.atomicobject.com/2018/11/05/using-an-int-type-in-typescript/ but got stuck because the generated code in graphql.ts still expects a number. Getting error: Type 'BigInt' is not assignable to type 'number'.

I could go and change the type in the generated graphql.ts but that would be overwritten every time I recompile using "relay-compiler --src ./src --schema ./schema.graphql --language typescript --customScalars.URL=URL --customScalars.LocalDate=LocalDate --customScalars.Instant=string" .

I could also simply round the number to make sure there are no decimal places but that seems a bit brittle to me.

What is the right way to handle this with the schema first approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文