[英]Updating/editing user data in graphql(Cannot return null for non-nullable field Mutation.updateUser)
So I want a user to be able to edit his data (in this case the email)in graphql, but i get a "Cannot return null for non-nullable field Mutation.updateUser" error and i cant figure out whats wrong所以我希望用户能够在 graphql 中编辑他的数据(在本例中为电子邮件),但我收到“无法为不可空字段 Mutation.updateUser 返回 null”错误,我不知道出了什么问题
my defs:我的定义:
type Mutation{
updateUser(id: ID! email: String!): User!
}
type User{
id: ID!
email: String!
token: String!
createdAt: String!
username: String!
bio: String,
}
code:代码:
module.exports = {
Mutation: {
async updateUser(parent, args) {
return User.findOneAndUpdate({ id: args.id }, { email: args.email }, { new: true })
}
}
}
user schema:用户架构:
const userSchema = new Schema({
username: String,
password: String,
email: String,
createdAt: String,
bio: String,
});
Define your schema like this.像这样定义您的架构。 Suppose you need to make bio field
nullable = true
simply make it required = false
.假设您需要将bio字段设为
nullable = true
,只需将其设为required = false
。 It will solve your problem.它会解决你的问题。
const userSchema = new Schema({
username: {type: String, required: true},
password: {type: String, required: true},
email: {type: String, required: true},
bio: {type: String, required: false},
}, {timestamps: true});
Moreover, for better code practice, use {timestamps: true}
in your schema it will automatically generate your createdAt , updatedAT column You have no need to explicitly define these columns.此外,为了更好的代码实践,在您的架构中使用
{timestamps: true}
它将自动生成您的createdAt , updatedAT列您无需显式定义这些列。 These columns will be generated and updated automatically.这些列将自动生成和更新。
You can update the schema so that the mutation can return null without throwing errors.您可以更新架构,以便突变可以返回 null 而不会引发错误。
type Mutation{
updateUser(id: ID!, email: String!): User
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.