'use strict'

import { graphql } from 'graphql'
import { Schema } from './data/schema'

const createResponse = function (statusCode, body) {
  return {
    statusCode: statusCode,
    headers: {
      'Access-Control-Allow-Origin': '*' // Required for CORS
    },
    body: JSON.stringify(body)
  }
}

export const handler = function (event) {
  var body
  try {
    body = JSON.parse(event.body)
  } catch (e) {
    return Promise.resolve(createResponse(500, e))
  }

  return graphql(Schema, body.query, null, {}, body.variables)
    .then(data => createResponse(200, data))
    .catch(err => createResponse(500, err))
}

export const serverlessHandler = function (event, context, callback) {
  handler(event)
    .then(response => callback(null, response))
}

@@ -333,20 +333,29 @@
 n (event
+, context
 ) %7B%0A
-
   var bo
@@ -471,16 +471,284 @@
 ))%0A  %7D%0A%0A
+  console.log('----- Cognito Authorizer claims -----')%0A  console.log('sub', context.authorizer.claims%5B'sub'%5D)%0A  console.log('email_verified', context.authorizer.claims%5B'email_verified'%5D)%0A  console.log('cognito:username', context.authorizer.claims%5B'cognito:username'%5D)%0A
   return
@@ -965,16 +965,16 @@
 back) %7B%0A
-
   handle
@@ -980,16 +980,25 @@
 er(event
+, context
 )%0A    .t

Test Cognito authorizer claims.