Hey guys i’m facing a problem connecting mongoDB database and graphql server
together and mongodb is taking so long to connect.
here’s my server.ts code
import { createServer } from "@graphql-yoga/node";
import { MongoClient } from "mongodb";
import typeDefs from "./graphql/schemas";
import resolvers from "./graphql/resolvers";
const process = require("dotenv").config();
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri);
client.connect((err) => console.log("db is connected"));
const db = client.db("connect");
const users = db.collection("users");
const chats = db.collection("chats");
const messages = db.collection("messages");
const server = createServer({
schema: {
typeDefs,
resolvers,
},
context: {
chats,
users,
messages,
},
});
server.start();
i’m using chats, users and messages collections as context and those are derived after db is connected but db connection is taking much longer and the issue is my server is started before the db is connected
i wanna know why mongodb is taking so long to connect or is there any other way to connect db and start the server. i tried chaining .then
on client.connect()
but mongodb is taking about a minute to connect. any help would be appreciated.