0

When I do a mutation on graphiql which is using my code for the GraphQL server, it returns null for all entries in the object.

I am using a Node and Express back end and it is using a MongoDB database that uses mongoose to access it.

updateTodo: {
  type: TodoType,
  args: {
    id: {
      type: new GraphQLNonNull(GraphQLID)
    },
    action: {
      type: new GraphQLNonNull(GraphQLString)
    }
  },
  resolve(parent, args) {
    return Todo.updateOne({ // updateOne is a MongoDB/mongoose function
      _id: args.id
    }, {
      $set: {
        action: args.action
      }
    });
  }
}

What I get

{
  "data": {
    "updateTodo": {
      "id": null,
      "action": null
    }
  }
}

from the following

mutation {
  updateTodo(id: "5c18590fa6cd6b3353e66b06", action: "A new Todo") {
    id
    action
}

I do this afterwards

{
  todos{
    id
    action
  }
}

and I get this

{
  "data": {
    "todos": [
      {
        "id": "5c18590fa6cd6b3353e66b06",
        "action": "A new Todo"
      }
    ]
  }
}

so I know it is working but would prefer to get the new data return.

More info

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  fields: () => ({
    id: {
      type: GraphQLID
    },
    action: {
      type: GraphQLString
    },
    isCompleted: {
      type: GraphQLBoolean
    },
    user: {
      type: UserType,
      resolve(parent, args) {
        return User.findById(parent.userId);
      }
    }
  })
});

Imported into the file.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const todoSchema = new Schema({
  action: String,
  isCompleted: Boolean,
  userId: String
})

module.exports = mongoose.model('Todo', todoSchema);

Here is the github repository so you can look at the code https://github.com/petersrule/graphql-node-express-boilerplate

Jeremy Scott Peters
  • 377
  • 1
  • 3
  • 15

1 Answers1

0

Please try to use the below updated code in your resolver function for update, "{new: true}" helps to return the updated object from mongoDB. i hope this will help.

updateTodo: {
  type: TodoType,
   args: {
     id: {
      type: new GraphQLNonNull(GraphQLID)
     },
     action: {
      type: new GraphQLNonNull(GraphQLString)
     }
   },
 resolve(parent, args) {
    return new Promise((resolve, reject) => {
        Todo.findOneAndUpdate({ // updateOne is a MongoDB/mongoose function
            "_id": args.id
          }, {
            $set: {
              "action": args.action
            }
          }, {
            new: true // This makes sure the return result is the updated information
          })
          .then((result) => {
            return resolve(result);
          })
          .catch((err) => {
            return reject(err);
          })
      })
      .then((finalResult) => {
        return finalResult;
      })
      .catch((err) => {
        return err;
      })
  }
}

Please do let me know the result.

Jeremy Scott Peters
  • 377
  • 1
  • 3
  • 15
Gautam Malik
  • 146
  • 1
  • 9
  • I really thought that was going to work. But it didn't even change the entry in the database. Thanks for the help despite our lack of success yet. I also considered doing a Promise but never tried it. I'm going to try it again and change your code a little. I think your code didn't specify an update. – Jeremy Scott Peters Dec 23 '18 at 20:26
  • Gautam Mailik, I have one question that I hope you'll be able to answer. Even though you used the findOneAndUpdate, function, it gives me this warning in the terminal when I do the updateTodo call in Graphiql, "(node:13964) DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead." How do I fix things so I don't get that warning? – Jeremy Scott Peters Dec 23 '18 at 21:25
  • @petersrule to fix the warning pass additionally {new: true, useFindAndModify: false}. in the meantime i will also try to replicate the issue for update at my end. i hope this will help you a bit more. – Gautam Malik Dec 24 '18 at 07:30