-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
37 lines (29 loc) · 1.03 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { MongoClient } from 'mongodb';
import express from 'express';
import cors from 'cors';
const { ApolloServer } = require('apollo-server-express');
import { typeDefs, resolversWithMongoDb } from './graphql/schema';
const PORT = 4000;
const MONGO_URL = 'mongodb://localhost:27017';
const MONGO_DB_NAME = 'tv-shows-that-ended';
const app = express();
// Allow requests from 'client':
app.use('*', cors({ origin: 'http://localhost:3000' }));
const start = async () => {
try {
// Load MongoDB:
const db = (await MongoClient.connect(MONGO_URL)).db(MONGO_DB_NAME);
// console.log(db);
const seriesesDb = db.collection('shows');
const resolvers = resolversWithMongoDb(seriesesDb);
const apolloServer = new ApolloServer({ typeDefs, resolvers });
apolloServer.applyMiddleware({ app });
// Server listen:
app.listen({ port: PORT }, () =>
console.log(`🚀 Server ready at http://localhost:${PORT}${apolloServer.graphqlPath}`)
);
} catch (e) {
console.log("Error catched:", e);
}
}
start();