async function startApolloServer() {
const server = new ApolloServer({ typeDefs, resolvers, context: async({req})=>{
const {isAuth} = await auth(req)
if(!isAuth)throw new AuthenticationError('operation is not allowed')
return {
db,
pubsub,
isAuth
}
} });
await server.start();
server.applyMiddleware({app})
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
mongoose.connect('mongodb://127.0.0.1:27017/users', {
useNewUrlParser: true,useUnifiedTopology: true
})
.then(()=>{
console.log('connect to database successfully');
}).catch(e=> console.log(e.message) )
await new Promise(resolve => httpServer.listen(PORT, resolve));
console.log(`๐ Server ready at http://localhost:${PORT}${server.graphqlPath}`);
console.log(`๐ Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
return { server, app, httpServer };
}
startApolloServer()