Created
March 11, 2021 18:29
-
-
Save noeljackson/e390bd99b97aa2d159dec39f5b05f4f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Client } = require('pg') | |
const config = require('./config') | |
const generateServer = require('./server') | |
const server = generateServer() | |
const invokeHandler = (event, context, handler) => { | |
return new Promise((resolve, reject) => { | |
const callback = (error, body) => (error ? reject(error) : resolve(body)) | |
handler(event, context, callback) | |
}) | |
} | |
exports.graphqlHandler = async (event, context) => { | |
const graphqlHandler = server.createHandler({ | |
cors: { | |
origin: '*', | |
credentials: true | |
} | |
}) | |
const client = new Client(config.db) | |
let response | |
try { | |
await client.connect() | |
response = await invokeHandler( | |
event, | |
{ ...context, client }, | |
graphqlHandler | |
) | |
} catch (error) { | |
console.error(error) | |
throw new Error(error.message) | |
} finally { | |
await client.end() | |
} | |
return response | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment