Skip to content

Instantly share code, notes, and snippets.

@c0debreaker
Forked from pszabop/create-function-point.ts
Created August 2, 2020 02:14
Show Gist options
  • Save c0debreaker/4e7bc94f698e57365c6b9e925ae46eb3 to your computer and use it in GitHub Desktop.
Save c0debreaker/4e7bc94f698e57365c6b9e925ae46eb3 to your computer and use it in GitHub Desktop.
//-----------------------------------------------------------------------------------------------------------------------
/*
* Create a lambda function handler for a graphQL typeName.fieldName
*/
//-----------------------------------------------------------------------------------------------------------------------
const createAppSyncLambdaHandler = function (stack: cdk.Stack, api: appsync.GraphQLApi, recipe: {
name: string, // XXX autogenerate frome type.field the name root of the name for the handler and data source
environment: { [key: string]: string }, // @see https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html
permissions: { obj: any, func: any }, // the currently singleton permissions for the handler to e.g. talk to dynamoDB
description: string, // text description attached to various created objects (can be undefined)
typeName: string, // the GraphQL name of the type that will be handled
fieldName: string, // the name of the field in the type that will be handled
respTemplate?: string // response template if the default won't work (might evolve to 'set vs singleton' as those are the main types)
timeout?: cdk.Duration // optional timeout
memorySize?: number // optional memory size
}): lambda.Function { // return function so can be referenced elsewhere e.g. for CFT Outputs
if (!recipe.memorySize) {
recipe.memorySize = 512; // default set to knee of perf curve for many examples found on web
}
const handler = new lambda.Function(stack, recipe.name, {
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromInline(defaultLambdaCode),
handler: 'index.handler',
environment: recipe.environment,
timeout: recipe.timeout,
logRetention: 7,
memorySize: recipe.memorySize,
});
// XXX permissions an array
recipe.permissions.func.apply(recipe.permissions.obj, [handler]);
const dataSource = api.addLambdaDataSource(
recipe.name + 'DataSource',
recipe.description,
handler,
);
dataSource.createResolver({
typeName: recipe.typeName,
fieldName: recipe.fieldName,
requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(`
{
"subscriber": $util.dynamodb.toDynamoDBJson($ctx.identity.sub),
"context": $utils.toJson($context.arguments)
}
`),
responseMappingTemplate: appsync.MappingTemplate.fromString( `$util.toJson($ctx.result)`),
});
return handler;
}
const setUserMfgrApiTokenHandler = createAppSyncLambdaHandler(this, api, {
typeName: 'Mutation',
fieldName: 'setUserMfgrApiToken',
description: 'Lambda resolver for setUserMfgrApiToken',
name: 'setUserMfgrApiTokenHandler',
environment: {
userDevicesTable: userTable.tableName,
firstTimeQueue: firstTimeQueue.queueUrl,
},
permissions: { obj: userTable, func: userTable.grantReadWriteData },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment