import express, { Application, Request, Response, NextFunction } from "express";
import connectToDB, { getMongoClient } from "./utils/db";
import dotenv from "dotenv";
import {ApolloServer} from 'apollo-server-express'
import {typeDefs} from './schemas/todo'
import UserData, {UserDocument} from './datasources/userdatasource'
import {UserResolver} from './resolvers/userResolver'
import { Collection } from "mongodb";
import {buildSchema} from 'type-graphql'
import {customAuthChecker} from './auth/authChecker'
import { validateAcessToken } from "./middleware/checkToken";
import http from 'http'
dotenv.config();
const app: Application = express();
const path:string = "/graphql";
const httpServer = http.createServer(app);
// let userCollection:Collection<UserDocument>;
(async () => {
try {
await connectToDB();
console.log("Database connected Welcome")
} catch (error) {
console.log(error)
}
})();
async function startServer(){
try {
const schema = await buildSchema({
resolvers:[UserResolver],
dateScalarMode: "isoDate",
authChecker:customAuthChecker,
})
const server = new ApolloServer({schema,
context: ({req, res}) => {
return {req, res}
}
})
app.use(validateAcessToken)
await server.start()
server.applyMiddleware({app, path})
await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve));
console.log(`๐ Server ready at http://localhost:4000${server.graphqlPath}`);
} catch (error:any) {
console.log(error.message)
}
}
startServer()
I am writing the above code to get my apollo-server-express to startup. I even get the message that my server has started in the terminal but when I navigate to the url the server is not running any idea why this might be the case. Will be helpful if someone could help me out