Search code examples
node.jsmongodbgraphqlhapi

mongo db data not returned in resolver function for graphql but working in routes of my nodejs server


I am setting up a node js environment based on hapi, graphql and mongodb. I am able to make connection to mongodb and retrieve and display data in the GET/POST routes using db mongoose schemas. But data is not getting retrieved when the model is passed to the resolver function of graphql. Please find below my graphql resolver function

const resolvers = (shows)=>({
myQuery:{
    testFunction(){
        return "returned from custom test function";
    },

    getShowByName: function(_,args){    

        var out= shows.findOne();

        console.log(out);         //returning a huge json response instead of proper data

        return out={
            _id:"5349b4ddd2781d08c09890f3",
            title: "test",
            version: "test",
            showDetails: [{
                name: args.showSchemaName,
                genre: "test",
                lead_actor: "test"
            }]
        }

        ;
    },


},
myMutation: {
    createShow: function(_,args){
        return args.showTypeInputDetails.title+","+args.showTypeInputDetails.version;

    }

}

});

module.exports = resolvers;

Console.log(out) is letting out a huge json response which is not coming from the mongo db. The json response is really huge and also has my connection parameters, credentials and other details, hence, i am posting the beginning of the response here

Query {
  _mongooseOptions: {},
  _transforms: [],
  mongooseCollection:
   NativeCollection {
     collection: Collection { s: [Object] },
     opts:
      { bufferCommands: true,
        capped: false,
        '$wasForceClosed': undefined },
     name: 'shows_details',
     collectionName: 'shows_coll',
     conn:
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,

Please help me in understanding why this response in coming when the findOne() is triggered from resolvers functions and proper results when the same function is triggered from the routes function.

[shows is my mongoose db model]


Solution

  • i made my resolver function async, and returned the data with await and that solved the problem