Skip to content

Instantly share code, notes, and snippets.

@thoroc
Created January 29, 2024 16:18
Show Gist options
  • Save thoroc/71ff200976b2f9ac81ceda8bdfcefefb to your computer and use it in GitHub Desktop.
Save thoroc/71ff200976b2f9ac81ceda8bdfcefefb to your computer and use it in GitHub Desktop.
Example on how to fetch all the functions resolver for an GraphQL API on AWS and output them per template (dealing with VTL)
#!/bin/sh
# set the table list for "DrawTable" "GameTable" "PlayroundTable"
TABLE_NAMES="DrawTable GameTable PlayroundTable"
# API_ID="asdasdadajsdljalskdjalksdj"
# use the following command to get the list of functions
# FUNCTION_FILE=$(aws appsync list-functions --api-id "$API_ID")
FUNCTION_FILE="list_functions_full.json"
rm -rf resolvers
mkdir -p resolvers
COUNT=0
# loop through the tables
for TABLE_NAME in $TABLE_NAMES
# print the table name
do printf "\nFinding all functions for %s table.\n" "$TABLE_NAME"
# use jq to find all the function names that use the table and store them in a variable
FUNCTION_NAMES=$(jq -r --arg TABLE_NAME "$TABLE_NAME" '.functions[] | select(.dataSourceName == $TABLE_NAME) | .name' $FUNCTION_FILE | sort | uniq)
# loop through the functions
for FUNCTION_NAME in $FUNCTION_NAMES
do
# define ignore list
IGNORE_LIST="auth0"
# check if the function name contains any of the terms in the ignore list
if [[ "$FUNCTION_NAME" == *"$IGNORE_LIST"* ]]; then
# if it does, skip it
echo " > Skipping $FUNCTION_NAME"
continue
fi
# print the function name
echo " $FUNCTION_NAME"
# rename the function name to be param case
KEBAB_CASE=$(echo "$FUNCTION_NAME" | sed 's/\(.\)\([A-Z]\)/\1-\2/g' | tr '[:upper:]' '[:lower:]')
# remove substring: "data-resolver-fn"
CLEANED_NAME=$(echo "$KEBAB_CASE" | sed 's/data-resolver-fn-//')
# for each function name extract the value for the following fields: requestMappingTemplate, responseMappingTemplate
REQUEST_MAPPING_TEMPLATE=$(jq -r --arg FUNCTION_NAME "$FUNCTION_NAME" '.functions[] | select(.name == $FUNCTION_NAME) | .requestMappingTemplate' $FUNCTION_FILE)
RESPONSE_MAPPING_TEMPLATE=$(jq -r --arg FUNCTION_NAME "$FUNCTION_NAME" '.functions[] | select(.name == $FUNCTION_NAME) | .responseMappingTemplate' $FUNCTION_FILE)
mkdir -p "resolvers/$CLEANED_NAME"
# Save those to 2 different files which names are the function names AND request or response
echo "$REQUEST_MAPPING_TEMPLATE" > "resolvers/$CLEANED_NAME/request.vtl"
echo "$RESPONSE_MAPPING_TEMPLATE" > "resolvers/$CLEANED_NAME/response.vtl"
# increment the count by 2
COUNT=$((COUNT+2))
done
# end the loop
done
# print the count
printf "\nTotal number of files created: %d\n" "$COUNT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment