-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (50 loc) · 1.27 KB
/
index.js
File metadata and controls
55 lines (50 loc) · 1.27 KB
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
const express = require("express");
const { ApolloServer, gql } = require("apollo-server-express");
const cors = require("cors");
require("dotenv").config();
const { jwt_decode } = require("./services/jwt/jwt");
const { typedefs } = require("./graphql/typedefs");
const { mutaions, quary, typeResovers } = require("./graphql/reslovers");
const PORT = process.env.PORT || 4000;
const typeDefs = gql`
type Query {
hello: String
}
${typedefs}
`;
const resolvers = {
Query: {
hello: async () => "Hello world!",
...quary,
},
Mutation: {
...mutaions,
},
...typeResovers,
};
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => {
const token = req.headers.authorization;
var user = null;
try {
user = await jwt_decode(token);
} catch (ex) {
user = null;
}
const isAuthenticated = user ? true : false;
return { user, isAuthenticated };
},
});
const app = express();
app.use(cors());
app.get("/", (req, res) => {
res.status(200).send("hello, surprised to see you here!!");
});
server.start().then(() => {
server.applyMiddleware({ app, path: "/graphql", cors: false });
app.listen({ port: PORT }, () =>
console.log(`Server ready at http://localhost:${PORT}${server.graphqlPath}`)
);
});