-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
39 lines (35 loc) · 1.03 KB
/
index.js
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
38
39
const { ApolloServer } = require("@apollo/server");
const { startStandaloneServer } = require("@apollo/server/standalone");
const mongoose = require("mongoose");
require("dotenv").config();
const { readJWT } = require("./util/readJWT")
const typeDefs = require("./graphql/typeDefs.js");
const resolvers = require("./graphql/resolvers");
const server = new ApolloServer({
typeDefs,
resolvers
});
async function startApolloServer() {
const port = process.env.PORT || 5000;
const { url } = await startStandaloneServer((server), {
listen: { port },
context: async ({ req, res }) => {
const authHeader = req.headers.authorization || '';
const user = readJWT(authHeader);
return { user };
},
});
console.log(`
🚀 Server is running!
📭 Query at ${url}
`);
}
mongoose
.connect(process.env.MONGODB, {})
.then(() => {
console.log("\nSUCCESS: CONNECTED TO DATABASE");
startApolloServer();
})
.catch((err) => {
console.error(err);
});