Showing
1 changed file
with
43 additions
and
8 deletions
1 | import dotenv from "dotenv"; | 1 | import dotenv from "dotenv"; |
2 | dotenv.config(); | 2 | dotenv.config(); |
3 | -import { GraphQLServer, PubSub } from "graphql-yoga"; | 3 | +import express from "express"; |
4 | +import bodyParser from "body-parser"; | ||
5 | +import cors from "cors"; | ||
6 | +import helmet from "helmet"; | ||
7 | +import csp from "helmet-csp"; | ||
8 | +import expressPlayground from "graphql-playground-middleware-express"; | ||
9 | +import { ApolloServer } from "apollo-server-express"; | ||
4 | import morgan from "morgan"; | 10 | import morgan from "morgan"; |
5 | import "./passport"; | 11 | import "./passport"; |
6 | import { authenticateJWT } from "./passport"; | 12 | import { authenticateJWT } from "./passport"; |
... | @@ -8,14 +14,43 @@ import schema from "./schema"; | ... | @@ -8,14 +14,43 @@ import schema from "./schema"; |
8 | 14 | ||
9 | const PORT = process.env.PORT; | 15 | const PORT = process.env.PORT; |
10 | 16 | ||
11 | -const pubsub = new PubSub(); | 17 | +const server = new ApolloServer({ |
12 | - | ||
13 | -const server = new GraphQLServer({ | ||
14 | schema, | 18 | schema, |
15 | - context: ({ request }) => ({ request, pubsub }), | ||
16 | }); | 19 | }); |
17 | 20 | ||
18 | -server.express.use(morgan("dev")); | 21 | +const app = express(); |
19 | -server.express.use(authenticateJWT); | 22 | +app.use(helmet()); |
23 | + | ||
24 | +app.use( | ||
25 | + csp({ | ||
26 | + directives: { | ||
27 | + defaultSrc: ["'self'"], | ||
28 | + styleSrc: ["'self'", "'unsafe-inline'"], | ||
29 | + styleSrcElem: [ | ||
30 | + "'self'", | ||
31 | + "fonts.googleapis.com", | ||
32 | + "cdn.jsdelivr.net", | ||
33 | + "'unsafe-inline'", | ||
34 | + ], | ||
35 | + imgSrc: ["'self'", "cdn.jsdelivr.net"], | ||
36 | + scriptSrcElem: ["'self'", "cdn.jsdelivr.net", "'unsafe-inline'"], | ||
37 | + fontSrc: ["'self'", "'unsafe-inline'", "fonts.gstatic.com"], | ||
38 | + }, | ||
39 | + }) | ||
40 | +); | ||
41 | + | ||
42 | +app.use(bodyParser.json(), cors()); | ||
43 | +app.use(bodyParser.urlencoded({ extended: true })); | ||
44 | +app.use(morgan("dev")); | ||
45 | + | ||
46 | +app.use(authenticateJWT); | ||
47 | + | ||
48 | +app.get("/", expressPlayground({ endpoint: "/graphql" })); | ||
49 | + | ||
50 | +server.applyMiddleware({ app }); | ||
51 | + | ||
52 | +const handleListening = () => { | ||
53 | + console.log(`Server ready at http://localhost:${PORT}`); | ||
54 | +}; | ||
20 | 55 | ||
21 | -server.start(() => console.log(`server is running : http://localhost:${PORT}`)); | 56 | +app.listen(PORT, handleListening); | ... | ... |
-
Please register or login to post a comment