-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
55 lines (51 loc) · 1.43 KB
/
handler.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
51
52
53
54
55
const { Pool } = require("pg");
const {
createPostGraphileSchema,
withPostGraphileContext,
} = require("postgraphile");
const { graphql } = require("graphql");
let pgPool;
let schema;
module.exports.graphile = async (event, context) => {
if (!pgPool) {
pgPool = new Pool({
connectionString: process.env.DATABASE_URL,
// this ensures the pool can drop a connection if it has gone bad
min: 0,
// no need for more since we are in Lambda-land
max: 1,
});
}
if (!schema) {
schema = await createPostGraphileSchema(pgPool, ["public"], {
dynamicJson: true,
ignoreRBAC: false,
ignoreIndexes: false,
...(process.env.APPEND_PLUGINS
? {
appendPlugins: process.env.APPEND_PLUGINS.split(" ")
.map(require)
.map((module) => (module.default ? module.default : module)),
}
: {}),
});
}
const { query, variables, operationName } = JSON.parse(event.body);
const result = await withPostGraphileContext(
{
pgPool,
// TODO - test JWT
// jwtSecret: process.env.JWT_SECRET,
// jwtToken: event.headers.Authorization.split(" ")[1],
},
(context) =>
graphql(schema, query, null, { ...context }, variables, operationName)
);
return {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify(result),
};
};