简体   繁体   中英

Graphql `Parameter \“obj\” to Document() must be an object, got `

When i test my graphql mutation i have an issue it says that i do pass wrong parameters

here what i am trying to do in graphiql

mutation{
  sendMessage(senderId:"sender" receiverId:"receiver" messageText:"some text in here"){
    messageText
  }
}

typeDefs

...
type Mutation{
....
    sendMessage( senderId: String!, receiverId: String!, messageText: String! ): Message
}

resolvers.js

Mutation:{
        sendMessage: async (root, { senderId, receiverId, messageText }, { User, Message }) => {
            const newMessage = await new Message(senderId, receiverId, messageText).save();
            return newMessage;
        },
}

message model

const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const MessageSchema = new Schema({
    senderId: {
        type: String,
        required: true,
    },
    receiverId: {
        type: String,
        required: true
    },
    messageText: {
        type: String,
        required: true
    }
});

module.exports = mongoose.model("Message", MessageSchema);

i expect to be some text in here

the problem was that i was no passing an object;

sendMessage: async (root, { senderId, receiverId, messageText }, { User, Message }) => {
            const newMessage = await new Message({senderId, receiverId, messageText}).save();
            return newMessage;
        },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM