我正在实现一个 React Typescript 应用程序,它使用 GraphQL(前端的 Apollo)与 Kotlin 后端进行通信。
我的突变是:
updateProfile(input: UpdateProfileInput)
input UpdateProfileInput {
firstName: String
lastName: String
email: String
}
我应该仅发送已更改的字段作为输入。因此,如果电子邮件与我们数据库中当前的内容相同,我就不应该发送电子邮件。
我的问题是,在前端,如果我将值设置为空,它会起作用吗?或者它仍然会在后端检测到该字段已发送吗?
例如,如果我发送
input1
和 input2
是否相同(因为字段可以为空):
const input1 = {
firstName: null,
lastName: null,
email: "[email protected]" //<- updated email
}
const input2 = {
email: "[email protected]"
}
在 Graphql 突变中,不发送字段与将字段显式设置为 null 不同。
您应该避免为您不希望更新的字段发送任何值。在您的情况下,发送 null 将覆盖已存在的电子邮件的值。