Skip to content

Instantly share code, notes, and snippets.

@alsoamit
Last active March 13, 2023 08:00
Show Gist options
  • Save alsoamit/24bb3f55fb2942d8f4328e9da9884ed4 to your computer and use it in GitHub Desktop.
Save alsoamit/24bb3f55fb2942d8f4328e9da9884ed4 to your computer and use it in GitHub Desktop.
Populate nested fields in aws appsync vtl resolvers
## Problem: How to populate all authors in a post in Appsync using Vtl resovlers?!
## Post(Parent Query) Structure:
## {
## id: ID!
## authorID: ID!
## author: User
## title: String!
## }
## Solution:
## We can populate author details in the 'author' field.
## We'll read authorID from '$ctx.source.author' and pass it as primary_key to the Nested Query.
## Steps:
## 1. Attach a resolver to author field.
## 2. Paste in the following code in the resolver.
{
"version": "2017-02-28",
"operation": "GetItem",
"key": {
"sub": $util.dynamodb.toDynamoDBJson($ctx.source.author),
}
}
## Conclusion: Nested fields can be resolved by attaching a resolver against that field.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment