-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
60 lines (49 loc) · 1.32 KB
/
index.ts
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
import "reflect-metadata";
import { ApolloServer } from "apollo-server-express";
import Express from "express";
import { AuthChecker, buildSchema } from "type-graphql";
import mongoose from "mongoose";
import "dotenv/config";
import jwt, { secretType } from "express-jwt";
import {
AuthenticationResolver,
UserResolver,
GroupChatResolver,
} from "./resolvers";
mongoose.connect(`${process.env.MONGO_URI}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const customAuthChecker: AuthChecker<any> = ({ context: { req } }, roles) => {
if (!req.user) {
return false;
}
return roles.some((role) => role === req.user.status);
};
const path = "/graphql";
const main = async () => {
const schema = await buildSchema({
resolvers: [AuthenticationResolver, UserResolver, GroupChatResolver],
authChecker: customAuthChecker
});
const apolloServer = new ApolloServer({
schema,
context: ({ req }) => ({ req }),
});
const app = Express();
app.use(
path,
jwt({
secret: process.env.SECRET as secretType,
credentialsRequired: false,
algorithms: ["HS256"],
})
);
apolloServer.applyMiddleware({ app, path });
app.listen(process.env.PORT || 4000, () => {
console.log(
`Server started on http://localhost:${process.env.PORT || 4000}/graphql`
);
});
};
main();