-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (87 loc) · 2.56 KB
/
server.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require("express");
const { ApolloServer, AuthenticationError } = require("apollo-server-express");
const typeDefs = require("./schema");
const resolvers = require("./resolvers");
const mongoose = require("mongoose");
const jwt = require("jsonwebtoken");
//Import Environment Variables and Mongoose Models
require("dotenv").config();
const User = require("./models/User");
const Post = require("./models/Post");
//connect Alas Mongo Cloud Database
mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => {
console.log("Mongo DB Connected");
})
.catch((err) => {
console.error(err);
});
const getUser = async (token) => {
if (token) {
try {
let user = await jwt.verify(token, process.env.SECRET);
console.log(JSON.stringify(user));
return user;
} catch (err) {
throw new AuthenticationError(
"Your session has ended. Please sign in again."
);
}
}
};
//create Apollo/ GraphQL servers using typeDefs, resolvers, and context objects (mongo models)
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: true,
formatError: (error) => ({
name: error.name,
message: error.message.replace("Context creation failed:", ""),
}),
context: async ({ req }) => {
const token = req.headers["authorization"];
return { User, Post, currentUser: await getUser(token) };
},
});
const app = express();
server.applyMiddleware({ app });
// app.use(function (req, res, next) {
// res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
// res.header(
// "Access-Control-Allow-Headers",
// "Origin, X-Requested-With, Content-Type, Accept"
// );
// next();
// });
app.get("/", function (req, res, next) {
res.json("Hello World, Express GraphQL");
});
app.get("/configs", function (req, res, next) {
const envConfig = {
MONGO_URI: process.env.MONGO_URI,
SECRET: process.env.SECRET,
port: process.env.PORT,
};
res.json(envConfig);
});
app.get("/apolloServer", function (req, res, next) {
res.json(server);
});
//start server and listen to port 4000 (default graphql port)
app.listen({ port: process.env.PORT || 4000 }, () => {
//console.dir(app);
//console.dir(server);
console.log(
`Server ready at http://localhost:${process.env.PORT || 4000}${
server.graphqlPath
}`
);
});
// server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
// console.log(`Server listening ready at ${url}`);
// });