Saturday, 11 May 2019

Is this the right way to use Mongodb and Graphql (or any express app) ?

I just started learning graphql and mongo and this is my first project.At first I had a lot of trouble as the connection would close as soon as I made a graphql query and mongo client would keep throwing "Topology was destroyed" error.So I worked around stuffs myself and came to this. But I'm not sure if it's the right way.I couldn't find examples that used the native mongodb driver, most of them were using mongoose.https://gitlab.com/shivamsingha/todo-graphql-mongodb-js``js (async function () { try { await client.connect() const db = client.db(dbName) const server = new GraphQLServer({ typeDefs: config.schema, resolvers, context: (req) => ({ ...req, db }) }) server.express.use(cookieParser()) server.start(config.options, ( { port }) => console.log(Server is running on http://localhost:${port}`)) } catch (err) { console.error(err.stack) } })()function exitHandler(code) { client.close() console.warn(Exiting with code ${code}) }process.on('exit', exitHandler) process.on('SIGINT', ()=>process.exit(0)) ```Is this the right way to do it, or is there a better way?

Submitted May 11, 2019 at 06:52PM by shivamsingha

No comments:

Post a Comment