Answer the question
In order to leave comments, you need to log in
GraphQL update embedded Document?
In the MongoDB documentation It is written To update a nested document You need to apply the Concatenation operator ( dot.notation ) Well, how to Make such a query (mutation) in GraphQL?
Here is the document from the database
{
"_id" : ObjectId("5c13bab2a51af024f0c7a237"),
"interfaceSettings" : {
"dark" : true
"toolbarColor" : "#fff"
},
"username" : "Lena",
}
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,
}
},
})
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
}
mutation: {
update: async (_, { _id, input }) => {
const user = await User.findOneAndUpdate({ _id }, { $set: input }, { new: true })
return user
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question