1

I'm attempting to create a node.js app that encompasses both grapgql with MongoDB for databasing, though I'm fairly new to graphql.

I'm using GraphQLPlayground in the browser. The error encountered seems to be about promises, I'm not much familiar with the concept.

const { GraphQLServer} = require('graphql-yoga');

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/librarydb')


const Book = mongoose.model("Book", {
    author:  String,
    title: String,
    available: Boolean
});


//initialization of grapgql schema
const typeDefs = `
 type Query {
   bookDetails(title: String): String!
 }

 type Book{
     id: ID!       
    title: String!
    available: Boolean!
 }

 type Mutation{
     createBook(title: String!): Book
 }`;
    
    //reflects the structure of the schema
const resolvers = {
    Query: {
     bookDetails: (_, {title}) => `Book ${title || "not available"}`,
    },
    Mutation: {
        createBook: async (_, { title }) => {
            const book = new Book({ title, available: false});
            await book.save();
            return book;
        }
    }
};

const server = new GraphQLServer({typeDefs, resolvers});
mongoose.connection.once("open", function() {
    server.start(() => console.log('Connection to server created successfully'));
});

Initially a connection was made but when I started including a mongodb to my code I encountered the error below:

(node:480) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): 
MongoNetworkError: failed to connect to server [localhost:27017] on first
 connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

(node:480) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.
js process with a non-zero exit code.

I have explored similar questions but they all seem to be variations of the error and none seem to address this specific issue, thanks in advance.

Tebogo Khanye
  • 380
  • 1
  • 7
  • 18
  • Possible duplicate of https://stackoverflow.com/questions/50173080/mongonetworkerror-failed-to-connect-to-server-localhost27017-on-first-connec?rq=1 – Benjamin Gruenbaum Apr 03 '19 at 07:15

0 Answers0