'Expected variables json to be an object

I am trying to create a graphql server with mongodb. the server is up and running but the only problem I am facing is, I am not able to run my queries.every time i try to run a query it gives me the following error:"Expected variables json to be an object" mutations are execution fine.

when a run a mutation to create a new contact, the document gets added in the mongodb collection I think the mongodb objectId is not getting converted to the compatible type

index.js
const express = require('express')
const {ApolloServer,gql } = require('apollo-server-express')
const app = express()
const typeDefs = require('./typedefs');
const resolvers = require('./resolvers');
const mongoose = require('mongoose')



const startServer = async ()=>{
    const apolloServer = new ApolloServer({
        typeDefs,
        resolvers
    });

    await apolloServer.start();

    apolloServer.applyMiddleware({app:app});
    app.use((req,res)=>{
        res.send('express server');
    });

    await mongoose.connect('mongodb://localhost:27017/contact_db',{
        useUnifiedTopology:true,
        useNewUrlParser:true,
    });
    console.log("mongoose connected")

    app.listen(4000,()=>{console.log("server running on port 4000")})
}
startServer();


    typeDefs.js
const {gql} = require('apollo-server-express')


const typeDefs = gql`
type Contact {
    _id:ID!
    firstName:String!
    lastName:String!
    phone:String!
}

input ContactInput {
    firstName: String!
    lastName:String!
    phone:String!
}

type Query {
    getAllContacts:[Contact!]!
    getContact(id:ID!):Contact
}
type Mutation {
    createContact(contact:ContactInput): Contact
}
`
module.exports = typeDefs

    resolver.js
const Contact = require('./modals/contact.model')

const prepare = (o) => {
    o._id = o._id.toString()
    return o
  }

const resolvers = {
    Query:{
        getAllContacts:async ()=>{
            return (await Contact.find({}).toArray()).map(prepare)
        },
        getContact:async (parent,{id},context,info)=>{
                return await Contact.findById(id);
        }
    },
    Mutation:{
        createContact: async (parent,args,context,info)=>{
            const {firstName,lastName,phone} = args.contact;
            const contact = new Contact({firstName,lastName,phone});
            await contact.save();
            return contact;
        }
    }
}
module.exports = resolvers

    contect.model.js
const mongoose  = require('mongoose')

const contactSchema = new mongoose.Schema({
    _id: {
        type:mongoose.Schema.Types.ObjectId
    },
    firstName:{
        type:String,
        required:true
    },
    lastName:{
        type:String,
        required:true
    },
    phone:{
        type:String,
        required:true
    }
})

const Contact = mongoose.model('contact',contactSchema);
module.exports = Contact;


Solution 1:[1]

I had this problem when running locally and using the Apollo playground/studio.

The problem is the variable I was trying to pass into the query from the playground.

I was passing a single int "1" when I needed to pass in an object "{ "weightId": "1" }"

Look for the Variables box underneath the Operation box in the playground. Hope this helps out anyone else who's stuck!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kittyhawk