'Mongo document not updating with graphQL and mongoose
Menu App prototype, very very new to graphql so I am having issues with updating a document, sending document data is fine so Ill have an ID I can use to target the document I want. Not even sure how to ask this question but updating isnt working and is returning Null. I am using the findbyidandupdate method for mongoose. updateMenu in the root value is the function with issues.
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv').config();
const cors = require('cors');
const bcrypt = require('bcrypt')
const bodyParser = require('body-parser')
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql')
const port = 8081;
const app = express();
app.use(cors());
app.use(express.json());
app.use(bodyParser.json())
const menuCollection = require('./models/menu')
app.use('/graphql', graphqlHTTP({
schema: buildSchema(`
type Menu {
_id: ID!
number: String!
title: String!
description: String!
priceSML: String!
priceMED: String!
priceLGE: String!
vegetarian: Boolean!
vegan: Boolean!
veganOption: Boolean!
gluetenFreeOption: Boolean!
}
input MenuInput {
_id: ID
number: String
title: String
description: String
priceSML: String
priceMED: String
priceLGE: String
vegetarian: Boolean
vegan: Boolean
veganOption: Boolean
gluetenFreeOption:Boolean
}
type RootQuery {
menus: [Menu!]!
}
type RootMutation {
createMenu(menuInput: MenuInput): Menu
updateMenu(menuInput: MenuInput): Menu
}
schema {
query: RootQuery
mutation: RootMutation
}
`),
rootValue: {
//get all menu documents
menus: () => {
return menuCollection.find()
.then(menus => {
return menus.map(menu => {
return { ...menu._doc };
});
})
.catch(err => {
throw err
})
},
//add new document to menu collection
createMenu: (args) => {
const menu = new menuCollection({
number: args.menuInput.number,
title: args.menuInput.title,
description: args.menuInput.description,
priceSML: args.menuInput.priceSML,
priceMED: args.menuInput.priceMED,
priceLGE: args.menuInput.priceLGE,
vegetarian: args.menuInput.vegetarian,
vegan: args.menuInput.vegan,
veganOption: args.menuInput.veganOption,
gluetenFreeOption: args.menuInput.gluetenFreeOption
})
return menu
.save()
.then(result => {
console.log(result);
return { ...result._doc };
}).catch(err => {
console.log(err);
throw err;
});
},
updateMenu: (args) => {
return menuCollection.findByIdAndUpdate(args.menuInput._id, args)
.then(test => {
console.log(args.menuInput.id)
return test
})
.catch(err => {
throw err
})
}
},
graphiql: true
}))
//ENV variables for mongo connection auth
const mongoDbData = {
mongoUser: process.env.DB_USER,
mongoPassword: process.env.DB_PASSWORD,
mongoCluster: process.env.DB_CLUSTER,
mongoDatabase: process.env.DB_NAME
}
//connect to our mongoDB with mongoose and env varaibles
//server port listener
//server only starts if connection to mongoDB is successful
mongoose.connect(`mongodb+srv://${mongoDbData.mongoUser}:${mongoDbData.mongoPassword}@${mongoDbData.mongoCluster}.c0l8o.mongodb.net/${mongoDbData.mongoDatabase}?retryWrites=true&w=majority`).then(() => {
console.log('Successful connection to database')
app.listen(port, function (err) {
if (err) console.log("Error in server setup")
console.log(`Server listening on Port ${port}`);
})
}).catch(err => {
console.log(err)
})
Here is the query I am using on graphiQl
mutation {
updateMenu(menuInput: {_id: "625e8c085ac1c160d9790223",title: "new title"}){
title
}
}
the id is the one from the document I am trying to update. Big thanks, Im new enough to graphql to barely understand how to even ask the question, unfortunately ive got some time pressure or else id be burning through tutorials lmao
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
