-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (45 loc) · 1.69 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
require("dotenv").config();
import express from "express";
import logger from "morgan";
import { ApolloServer } from "apollo-server-express";
import { graphqlUploadExpress } from "graphql-upload";
//import schema from "./schema";
import { typeDefs, resolvers } from "./schema";
import { getUser, protectResolver } from "./users/users.utils";
/* #4.14 File Upload part One
Upload scalar type 사용을 위해 ApolloServer에 makeExecutableSchema로 직접 만든 schema 대신
typeDefs와 resolvers를 넣어주면 Apollo Server에서 schema를 직접 만들어 제공한다.
*/
const PORT = process.env.PORT;
async function startApolloServer(typeDefs, resolvers) {
try {
const apollo = new ApolloServer({
//schema,
typeDefs,
resolvers,
context: async ({ req }) => {
//console.log(req.headers);
return {
//token: req.headers.token,
loggedInUser: await getUser(req.headers.token),
protectResolver,
};
},
});
const app = express();
app.use(logger("tiny"));
app.use("/static", express.static("uploads"));
app.use(graphqlUploadExpress());
// This middleware should be added before calling `applyMiddleware`.
await apollo.start();
apollo.applyMiddleware({ app }); //Apollo Server야. 너 이제부터 우리 app server랑 같이 작동해.
await new Promise((resolve) => app.listen({ port: PORT }, resolve));
console.log(`server is running on http://localhost:${PORT}/graphql`);
// server.listen({ port: PORT }, () => {
// console.log(`server is running on http://localhost:${PORT}/`);
// });
} catch (err) {
console.log("error:", err);
}
}
startApolloServer(typeDefs, resolvers);