When you write your own Resolvers for AWS AppSync, you are given a
$context object that contains a lot of helpful things. In my case I needed to grab a unique ID associated with the user from the Cognito User Pool I set up. But the
$ctx.identity.cognitoIdentityId just wasn’t there. I needed to find out what I could use there.
Normally my response template looks like this:
#if($ctx.error) $utils.error($ctx.error.message, $ctx.error.type) #end $utils.toJson($utils.rds.toJsonObject($ctx.result))
In my case it was telling me that a value was required. So I changed the template to spit out the whole context object instead of just the message:
#if($ctx.error) $utils.error($utils.toJson($ctx), $ctx.error.type) #end
Then I went to Queries and ran it again, this time getting the whole context spat out as the “error”. A trip through a JSON unescape tool and formatting with the JSON Viewer Notepad++ plugin gave me a complete picture of what’s available.
Turns out I need to use
username, and the
cognitoIdentityId field is nowhere to be found.