-
Notifications
You must be signed in to change notification settings - Fork 588
/
lambda.js
117 lines (100 loc) · 3.76 KB
/
lambda.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
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
// @flow
import { ApolloServer } from "apollo-server-lambda";
import { applyMiddleware } from "graphql-middleware";
import { addSchemaLevelResolveFunction } from "graphql-tools";
import { Entity } from "webiny-entity";
import type { GraphQLMiddlewarePluginType } from "webiny-api/types";
import { prepareSchema } from "../graphql/prepareSchema";
import { getPlugins } from "webiny-plugins";
export const createHandler = async (config: Object) => {
await requestSetup(config);
let schema = await prepareSchema(config);
const registeredMiddleware: Array<GraphQLMiddlewarePluginType> = [];
const middlewarePlugins = getPlugins("graphql-middleware");
for (let i = 0; i < middlewarePlugins.length; i++) {
let plugin = middlewarePlugins[i];
const middleware =
typeof plugin.middleware === "function"
? await plugin.middleware({ config })
: plugin.middleware;
if (Array.isArray(middleware)) {
registeredMiddleware.push(...middleware);
} else {
registeredMiddleware.push(middleware);
}
}
config.middleware && registeredMiddleware.push(config.middleware);
if (registeredMiddleware.length) {
schema = applyMiddleware(schema, ...registeredMiddleware);
}
addSchemaLevelResolveFunction(schema, async (root, args, context, info) => {
// Make sure we do not block this resolver from processing subsequent requests!
// This is something that is baked into the graphql-tools and cannot be avoided another way.
delete info.operation["__runAtMostOnce"];
// Process `graphql-context` plugins
const ctxPlugins = getPlugins("graphql-context");
for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].preApply === "function") {
await ctxPlugins[i].preApply(context);
}
}
for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].apply === "function") {
await ctxPlugins[i].apply(context);
}
}
for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].postApply === "function") {
await ctxPlugins[i].postApply(context);
}
}
});
const apollo = new ApolloServer({
...(config.apollo || {}),
schema,
cors: {
origin: "*",
methods: "GET,HEAD,POST"
},
context: async ({ event }) => {
await requestSetup(config);
return {
event,
config,
getDatabase() {
return config.database.mongodb;
}
};
}
});
const handler = apollo.createHandler();
return (event: Object, context: Object): Promise<Object> => {
return new Promise((resolve, reject) => {
handler(event, context, (error, data) => {
if (error) {
return reject(error);
}
resolve(data);
});
});
};
};
const requestSetup = async (config: Object = {}) => {
// Configure Entity layer
if (config.entity) {
Entity.driver = config.entity.driver;
Entity.crud = config.entity.crud;
}
// Check if connection is valid and if Settings table exists - this will tell us if the system is installed.
if (process.env.NODE_ENV === "development") {
try {
await Entity.getDriver().test();
} catch (e) {
throw Error(
`The following error occurred while initializing Entity driver: "${
e.message
}". Did you enter the correct database information?`
);
}
}
};