Skip to content

Instantly share code, notes, and snippets.

@noeljackson
Created March 11, 2021 18:29
Show Gist options
  • Save noeljackson/e390bd99b97aa2d159dec39f5b05f4f7 to your computer and use it in GitHub Desktop.
Save noeljackson/e390bd99b97aa2d159dec39f5b05f4f7 to your computer and use it in GitHub Desktop.
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