Guest User

Untitled

a guest
Nov 11th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. import get from 'lodash/get'
  2. import { createPostGraphileSchema, withPostGraphileContext } from 'postgraphile'
  3. import Pool from 'pg-pool'
  4. import { graphql } from 'graphql'
  5.  
  6. const postgraphileSchemaPromise = createPostGraphileSchema(
  7. config.DB_ENDPOINT,
  8. 'public'
  9. )
  10.  
  11. const pool = new Pool({
  12. user: config.DB_USER,
  13. password: config.DB_PASSWORD,
  14. host: config.DB_HOST,
  15. port: config.DB_PORT,
  16. database: config.DB_NAME,
  17. min: 0,
  18. max: 1
  19. })
  20.  
  21. export default epsagon.lambdaWrapper(async (event) => {
  22. console.log(event)
  23. console.log(event.requestContext.authorizer)
  24. const userId = get(event, 'requestContext.authorizer.claims.sub')
  25. const roles = get(event, 'requestContext.authorizer.claims.roles', userId)
  26. const graphqlInput = JSON.parse(event.body)
  27. console.log(`Starting ${graphqlInput.operationName} for ${userId}`)
  28. console.time(`${userId}/${graphqlInput.operationName}`)
  29.  
  30. try {
  31. const postgraphileSchema = await postgraphileSchemaPromise
  32. const result = await withPostGraphileContext(
  33. {
  34. pgPool: pool,
  35. pgDefaultRole: 'application_user',
  36. pgSettings: {
  37. 'jwt.claims.roles': roles
  38. }
  39. },
  40. async context => {
  41. return await graphql(
  42. postgraphileSchema,
  43. graphqlInput.query,
  44. null,
  45. { ...context },
  46. graphqlInput.variables,
  47. graphqlInput.operationName
  48. )
  49. }
  50. )
  51. console.log(`Finished ${graphqlInput.operationName} for ${userId}`)
  52. console.timeEnd(`${userId}/${graphqlInput.operationName}`)
  53. return {
  54. headers: {
  55. 'Access-Control-Allow-Origin': '*',
  56. 'Access-Control-Allow-Credentials': true
  57. },
  58. body: JSON.stringify(result),
  59. statusCode: 200
  60. }
  61. } catch (error) {
  62. console.error(error)
  63. console.timeEnd(`${userId}/${graphqlInput.operationName}`)
  64. return {
  65. headers: {
  66. 'Access-Control-Allow-Origin': '*',
  67. 'Access-Control-Allow-Credentials': true
  68. },
  69. body: JSON.stringify(error),
  70. statusCode: 500
  71. }
  72. }
  73. })
Add Comment
Please, Sign In to add comment