Skip to content

Instantly share code, notes, and snippets.

@pszabop
Created August 2, 2020 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pszabop/5c5d354e2e667b7af03f69e045666d26 to your computer and use it in GitHub Desktop.
Save pszabop/5c5d354e2e667b7af03f69e045666d26 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