我可以使用指令根据其他字段值计算字段值吗?

Can I use a directive to calculate field value based on other field value?

我有这样一个模型:

type Email {
  id: ID! @unique
  confirmed: Boolean! @default(value: "false")
  formatted: String! @unique
  raw: String!
}

我需要将 raw 字段的值小写并放入 formatted 字段,然后再保存到数据库。

问题是我已经有了三个用例(联系我们、订阅新闻、注册),我需要在三个不同的解析器中编写相同的代码。

为了避免重复,我认为以某种方式在指令中执行此操作会很好。

是否可以创建我自己的指令来处理这个问题?

更新:从 Prisma v1.18 开始,这是不可能的。

根据规范文档,我不认为这是可能的(不幸的是)因为这听起来像你需要一个计算字段

"GraphQL is not a programming language capable of arbitrary computation, but is instead a language used to query application servers that have capabilities defined in this specification."

https://facebook.github.io/graphql/June2018/

这在Prisma目前是做不到的,确实需要在应用层实现。不过,有一个开放的 feature request,如果您对此功能感兴趣,请加入 GitHub 上的讨论并添加您的 +1。