-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
150 lines (143 loc) · 4.34 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import "source-map-support/register";
import "reflect-metadata";
import { Connection, createConnection, getConnectionManager } from "typeorm";
import { ApolloServer } from "apollo-server-lambda";
import httpHeadersPlugin from "apollo-server-plugin-http-headers";
import { buildSchemaSync } from "type-graphql";
import AWS from "aws-sdk";
import { ApolloContext, Context as MyApolloContext } from "./types";
import { UserResolver } from "./src/resolvers/UserResolver";
import { BudgetResolver } from "./src/resolvers/BudgetResolver";
import { TransactionResolver } from "./src/resolvers/TransactionResolver";
import { CategoryResolver } from "./src/resolvers/CategoryResolver";
import { DebtResolver } from "./src/resolvers/DebtResolver";
import { Account } from "./src/entities/Account";
import { Budget } from "./src/entities/Budget";
import { BudgetMembership } from "./src/entities/BudgetMembership";
import { Category } from "./src/entities/Category";
import { CycleTransaction } from "./src/entities/CycleTransaction";
import { Debt } from "./src/entities/Debt";
import { Merchant } from "./src/entities/Merchant";
import { Notification } from "./src/entities/Notification";
import { Plan } from "./src/entities/Plan";
import { Purchase } from "./src/entities/Purchase";
import { Transaction } from "./src/entities/Transaction";
import { User } from "./src/entities/User";
import { AccountResolver } from "./src/resolvers/AccountResolver";
import { CycleTransactionResolver } from "./src/resolvers/CycleTransactionResolver";
import { MerchantResolver } from "./src/resolvers/MerchantResolver";
import { NotificationResolver } from "./src/resolvers/NotificationResolver";
import { PurchaseResolver } from "./src/resolvers/PurchaseResolver";
import { getCookie } from "./src/utils/getCookie";
import { authChecker } from "./src/utils/authChecker";
const { STAGE, DB_HOST, DB_NAME, DB_PORT, DB_PASSWORD, DB_USERNAME } = process.env;
//! This code is important (be careful trying to remove it).
//! `global.schema` name is required because of some deep graphql schema shit
if (!(global as any).schema) {
(global as any).schema = buildSchemaSync({
resolvers: [
AccountResolver,
BudgetResolver,
CategoryResolver,
CycleTransactionResolver,
DebtResolver,
MerchantResolver,
NotificationResolver,
PurchaseResolver,
TransactionResolver,
UserResolver,
],
validate: true,
authChecker,
});
}
const schema = (global as any).schema;
/**
* Get database connection
* @returns either old or new connection to the database
*/
const getConnection = async () => {
const manager = getConnectionManager();
let connection: Connection;
if (manager.has("default")) {
connection = manager.get("default");
} else {
connection = await createConnection({
type: "postgres",
host: DB_HOST,
port: parseInt(DB_PORT),
username: DB_USERNAME,
password: DB_PASSWORD,
database: DB_NAME,
entities: [
Account,
Budget,
BudgetMembership,
Category,
CycleTransaction,
Debt,
Merchant,
Notification,
Plan,
Purchase,
Transaction,
User,
],
synchronize: false,
logging: "all",
});
}
if (!connection.isConnected) {
await connection.connect();
}
return connection;
};
let S3;
if (STAGE === "dev") {
S3 = new AWS.S3({
s3ForcePathStyle: true,
accessKeyId: "S3RVER",
secretAccessKey: "S3RVER",
endpoint: "http://localhost:4569",
});
} else {
S3 = new AWS.S3({
s3ForcePathStyle: true,
});
}
const server = new ApolloServer({
schema,
plugins: [httpHeadersPlugin],
playground: {
endpoint: `/${STAGE}/graphql`,
settings: {
"request.credentials": "include",
},
},
context: async ({ event, context }: ApolloContext): Promise<MyApolloContext> => {
const connection = await getConnection();
let userId = null;
const token = getCookie(event, "token");
if (token !== undefined) {
try {
const user = await User.findOne({ where: { token } });
userId = user?.id;
} catch {}
}
return {
connection,
s3: S3,
event,
context,
userId,
setCookies: [],
setHeaders: [],
};
},
});
exports.handler = server.createHandler({
cors: {
origin: true,
credentials: true,
},
});