Make cookie logic work on lambda
@@ -34,21 +34,25 @@ const server = new ApolloServer({
},
});
-let serverHandler: any = null;
-
const handleManyCookies = (headers: any = {}) => {
if (headers["set-cookie"]) {
try {
- const value = JSON.parse(headers["set-cookie"]);
+ const setCookie = headers["set-cookie"];
+ headers["set-cookie"] = null;
+ const value = JSON.parse(setCookie);
if (Array.isArray(value)) {
return {
+ headers,
multiValueHeaders: {
- ...headers,
- "set-cookie": value,
+ "Set-Cookie": value,
},
};
}
} catch (err) {
+ console.error(
+ 'updating cookies to "multiValueHeaders" raised an error',
+ err,
+ );
return { headers };
}
}
@@ -57,11 +61,20 @@ const handleManyCookies = (headers: any = {}) => {
};
exports.graphqlHandler = async (event: any, context: any) => {
- if (serverHandler === null) {
- serverHandler = server.createHandler();
+ const serverHandler = server.createHandler();
+
+ try {
+ const response: any = await new Promise((resolve, reject) => {
+ serverHandler(event, context, (err: any, response: any = {}) => {
+ if (err) {
+ reject(err);
+ return;
}
- const response = await serverHandler(event, context);
+ resolve(response);
+ });
+ });
+
const { headers, ...responseData } = response;
const newHeaders = handleManyCookies(headers);
@@ -69,4 +82,7 @@ exports.graphqlHandler = async (event: any, context: any) => {
...responseData,
...newHeaders,
};
+ } catch (e) {
+ console.error("server handler error:", e);
+ }
};
