From 0660ee2d16ea4dd8fda1d87b9e44e4e0b4978cfa Mon Sep 17 00:00:00 2001 From: Srinath Reddy P Date: Wed, 29 May 2024 15:46:11 +0530 Subject: [PATCH 1/2] added SDK v3 code --- .../tools/cli/usage/lambda-triggers/index.mdx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/pages/gen1/[platform]/tools/cli/usage/lambda-triggers/index.mdx b/src/pages/gen1/[platform]/tools/cli/usage/lambda-triggers/index.mdx index e4ee7227dc3..63754784633 100644 --- a/src/pages/gen1/[platform]/tools/cli/usage/lambda-triggers/index.mdx +++ b/src/pages/gen1/[platform]/tools/cli/usage/lambda-triggers/index.mdx @@ -375,6 +375,10 @@ exports.handler = function (event, context) { `record.dynamodb` will contain a DynamoDB change json describing the item changed in DynamoDB table. Please note that it does not represent an original and new item as stored in DynamoDB table. To retrieve a original and new item you need to convert a DynamoDB json to original form: + + + + ```js const AWS = require('aws-sdk'); const records = event.Records.map((record) => ({ @@ -382,6 +386,20 @@ const records = event.Records.map((record) => ({ old: AWS.DynamoDB.Converter.unmarshall(record.dynamodb.OldImage) })); ``` + + + + +```js +const { unmarshall } = require("@aws-sdk/util-dynamodb"); +const records = event.Records.map((record) => ({ + new: unmarshall(record.dynamodb.NewImage), + old: unmarshall(record.dynamodb.OldImage), +})); +``` + + + ## Kinesis Stream Trigger From ed5a794da8d979d9ea1781bc5a41899d7efc3fe5 Mon Sep 17 00:00:00 2001 From: Srinath Reddy P Date: Wed, 29 May 2024 16:14:21 +0530 Subject: [PATCH 2/2] added SDK v3 code --- .../cli-legacy/function-directive/index.mdx | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/pages/gen1/[platform]/tools/cli-legacy/function-directive/index.mdx b/src/pages/gen1/[platform]/tools/cli-legacy/function-directive/index.mdx index 265e8c253dc..0f3c2e8d4f3 100644 --- a/src/pages/gen1/[platform]/tools/cli-legacy/function-directive/index.mdx +++ b/src/pages/gen1/[platform]/tools/cli-legacy/function-directive/index.mdx @@ -148,6 +148,10 @@ When building applications, it is often useful to fetch information for the curr For example purposes assume the function is named `GraphQLResolverFunction`: + + + + ```js /* Amplify Params - DO NOT EDIT You can access the following resource attributes as environment variables from your Lambda function @@ -217,6 +221,80 @@ exports.handler = async (event) => { throw new Error('Resolver not found.'); }; ``` + + + + +```js +/* Amplify Params - DO NOT EDIT +You can access the following resource attributes as environment variables from your Lambda function +var environment = process.env.ENV +var region = process.env.REGION +var authMyResourceNameUserPoolId = process.env.AUTH_MYRESOURCENAME_USERPOOLID + +Amplify Params - DO NOT EDIT */ + +const { CognitoIdentityProviderClient, AdminGetUserCommand } = require('@aws-sdk/client-cognito-identity-provider'); +const cognitoIdentityProviderClient = new CognitoIdentityProviderClient({ region: process.env.REGION }); + +/** + * Get user pool information from environment variables. + */ +const COGNITO_USERPOOL_ID = process.env.AUTH_MYRESOURCENAME_USERPOOLID; +if (!COGNITO_USERPOOL_ID) { + throw new Error( + `Function requires environment variable: 'COGNITO_USERPOOL_ID'` + ); +} +const COGNITO_USERNAME_CLAIM_KEY = 'cognito:username'; + +/** + * Using this as the entry point, you can use a single function to handle many resolvers. + */ +const resolvers = { + Query: { + echo: (ctx) => { + return ctx.arguments.msg; + }, + me: async (ctx) => { + const params = { + UserPoolId: COGNITO_USERPOOL_ID, + Username: ctx.identity.claims[COGNITO_USERNAME_CLAIM_KEY] + }; + try { + const response = await cognitoIdentityProviderClient.send(new AdminGetUserCommand(params)); + return response.UserAttributes; + } catch (e) { + throw new Error(`NOT FOUND`); + } + } + } +}; + +// event +// { +// "typeName": "Query", /* Filled dynamically based on @function usage location */ +// "fieldName": "me", /* Filled dynamically based on @function usage location */ +// "arguments": { /* GraphQL field arguments via $ctx.arguments */ }, +// "identity": { /* AppSync identity object via $ctx.identity */ }, +// "source": { /* The object returned by the parent resolver. E.G. if resolving field 'Post.comments', the source is the Post object. */ }, +// "request": { /* AppSync request object. Contains things like headers. */ }, +// "prev": { /* If using the built-in pipeline resolver support, this contains the object returned by the previous function. */ }, +// } +exports.handler = async (event) => { + const typeHandler = resolvers[event.typeName]; + if (typeHandler) { + const resolver = typeHandler[event.fieldName]; + if (resolver) { + return await resolver(event); + } + } + throw new Error('Resolver not found.'); +}; +``` + + + You can connect this function to your AppSync API deployed via Amplify using this schema: