Skip to content

proton0210/CallAppsyncAPI

Repository files navigation

CallAppsyncAPI

Package to call APPSYNCAPI using IAM

Simple function to aws appsync api inside aws lambda,PS: Lambda should have approriate IAM role to call the Appsync API

Operation

Example usage:

import callAppSyncAPI from "callappsyncapi"

const response = await callAppSyncAPI(APIURL, apiRegion, mutation, "notifyLiked", variables);

Detail Example

constAPIURL="";constapiRegion="";exportconsthandler=async(event: any)=>{for(constrecordofevent.Records){if(record.eventName==="INSERT"){constlike: any=DynamoDB.Converter.unmarshall(record.dynamodb.NewImage);awaitnotifyLiked(like);}}};asyncfunctionnotifyLiked(like: any){try{lettweet=awaitgetTweetById(like.tweetId);tweet=DynamoDB.Converter.unmarshall(tweetasRecord<string,AttributeValue>);console.log("TWEET",tweet);console.log("LIKE",like);if(!tweet){thrownewError(`Tweet with id ${like.tweetId} not found`);}constmutation=print(graphql` mutation notifyLiked( $id: ID! $userId: ID! $tweetId: ID! $likedBy: ID! ){ notifyLiked( id: $id userId: $userId tweetId: $tweetId likedBy: $likedBy ){ __typename ... on Liked{ id type userId tweetId likedBy createdAt } } } `);constid=ulid();constvariables={id: id,userId: tweet.creator,tweetId: tweet.id,likedBy: like.userId,};// calling our appsync functionconstresponse=awaitcallAppSyncAPI(APIURL,apiRegion,mutation,"notifyLiked",variables);console.log("Notification response:",response);}catch(error){console.error("Error while notifying like:",error);}}

About

Package to call APPSYNCAPI using IAM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published