我是GraphQL的新手。我正在尝试更新嵌入式文档。他们在MongoDB的页面上写道,您必须使用点表示法(“ field.nestedField”)。但是,如何编写带有点的graphql查询以进行更新。
问题是我的查询覆盖“ toolbarColor”:“#fff”,我的意思是“ toolbarColor”将从数据库中删除。请帮助我
这是数据库中的现有文档
{
"_id" : ObjectId("5c13bab2a51af024f0c7a237"),
"interfaceSettings" : {
"dark" : true
"toolbarColor" : "#fff"
},
"username" : "Lena",
}
现在我想更改“ interfaceSetting.dark” = false
mutation {
user{
update(_id: "5c13bab2a51af024f0c7a237" input:{
interfaceSettings: {dark: false}
}
){
user {
username
interfaceSettings{
dark
toolbarColor
}
}
}
}}
结果数据库文档
{
"_id" : ObjectId("5c13bab2a51af024f0c7a237"),
"interfaceSettings" : {
"dark" : false
},
"username" : "Lena",
}
后端##############
他是猫鼬图式(精简版)---------------------------------------- -
const userSchema = new Schema({
username: {
type: String,
required: true
},
interfaceSettings: {
dark: {
type: Boolean,
},
toolbarColor: {
type: String,
}
},
})
GraphQL模式(精简)------------------------------------------ -
type User {
_id: ID,
username: String
interfaceSettings: InterfaceSettings
}
type InterfaceSettings {
dark: Boolean,
toolbarColor: String
}
type Mutation {
update( _id: ID, input: InputUser): User
}
input InputUser {
username: String
interfaceSettings: InputInterfaceSettings
}
input InputInterfaceSettings {
dark: Boolean,
toolbarColor: String
}
解析器{reduced}
Mutation: {
update: async (_, { _id, input }) => {
const user = await User.findOneAndUpdate({ _id }, { $set: input }, { new: true })
return user
}
}