how to return new record from graphql mutation instead of null
I needed to have mongoose return a promise for the mutation. So my MongooseModel.create() changes from
const record = new this.collection(args)return record.save(cb)
to using Promise
const record = new this.collection(args)return new Promise((resolve, reject) => { record.save((err, res) => { err ? reject(err): resolve(res) });});
to using Async/Await
async create(obj = {}) { const args = { ...this.defaultRecord(), ...obj } const record = new this.Collection(args) try { const savedRecord = await record.save() return savedRecord } catch (err) { handleError(err) } }
My mutation doesn't have to change at all, but my fixtures generator also has to be updated to use the Promise.then() chain.