Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import get from 'lodash/get'
- import { createPostGraphileSchema, withPostGraphileContext } from 'postgraphile'
- import Pool from 'pg-pool'
- import { graphql } from 'graphql'
- const postgraphileSchemaPromise = createPostGraphileSchema(
- config.DB_ENDPOINT,
- 'public'
- )
- const pool = new Pool({
- user: config.DB_USER,
- password: config.DB_PASSWORD,
- host: config.DB_HOST,
- port: config.DB_PORT,
- database: config.DB_NAME,
- min: 0,
- max: 1
- })
- export default epsagon.lambdaWrapper(async (event) => {
- console.log(event)
- console.log(event.requestContext.authorizer)
- const userId = get(event, 'requestContext.authorizer.claims.sub')
- const roles = get(event, 'requestContext.authorizer.claims.roles', userId)
- const graphqlInput = JSON.parse(event.body)
- console.log(`Starting ${graphqlInput.operationName} for ${userId}`)
- console.time(`${userId}/${graphqlInput.operationName}`)
- try {
- const postgraphileSchema = await postgraphileSchemaPromise
- const result = await withPostGraphileContext(
- {
- pgPool: pool,
- pgDefaultRole: 'application_user',
- pgSettings: {
- 'jwt.claims.roles': roles
- }
- },
- async context => {
- return await graphql(
- postgraphileSchema,
- graphqlInput.query,
- null,
- { ...context },
- graphqlInput.variables,
- graphqlInput.operationName
- )
- }
- )
- console.log(`Finished ${graphqlInput.operationName} for ${userId}`)
- console.timeEnd(`${userId}/${graphqlInput.operationName}`)
- return {
- headers: {
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Credentials': true
- },
- body: JSON.stringify(result),
- statusCode: 200
- }
- } catch (error) {
- console.error(error)
- console.timeEnd(`${userId}/${graphqlInput.operationName}`)
- return {
- headers: {
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Credentials': true
- },
- body: JSON.stringify(error),
- statusCode: 500
- }
- }
- })
Add Comment
Please, Sign In to add comment