I am developing appsync resolvers with Cognito userpools and default Auth mode. The resolver mapping context doesn't seem to understand the $context. identity within the AppSync resolvers.
Error
I'm getting the error TS2339: Property username does not exist in type Identity. Property 'username does not exist on type 'AppSyncIdentityOIDC.
when I use the ctx.identity within the Resolvers.
AppSync resolver
import { util } from '@aws-appsync/utils';
/**
* Puts an item into the DynamoDB table.
* using @aws_cognito_user_pools in schema
* @param {import('@aws-appsync/utils').Context<{input: any}>} ctx the context
* @returns {import('@aws-appsync/utils').DynamoDBPutItemRequest} the request
*/
export function request(ctx) {
const { email, name } = ctx.args.input;
const values = {
email,
name
}
return {
"operation": "PutItem",
"key": {
"Id" : util.dynamodb.toDynamoDB(ctx.identity.username)
},
"attributeValues" : util.dynamodb.toMapValues(values)
}
}
I can see the cognito userName and claims in the cloudwatch Logs
Logs
{
"logType": "RequestFunctionEvaluation",
"path": [
"createUser"
],
"fieldName": "createUser",
"resolverArn": "arn:aws:appsync:xxxxx/types/Mutation/resolvers/createUser",
"requestId": "xxxx-8c7ca",
"context": {
"arguments": {
"input": {
"email": "testUser@az.com",
"name": "testUser"
}
},
"stash": {},
"outErrors": []
},
"fieldInError": true,
"evaluationResult": {
"operation": "PutItem",
"key": {
"Id": {
"claims": {
"sub": "xxxxxx",
"email_verified": true,
"iss": "https://cognito-idp.us-east-1.amazonaws.com/xxxxxx",
"cognito:username": "testUser10",
.....
"email": "testUser@az.com"
},
"defaultAuthStrategy": "ALLOW",
"issuer": "https://cognito-idp.us-east-1.amazonaws.com/xxxxxx",
"sourceIp": [
"xxxxxx"
],
"sub": "c478c4f8-4011-70bf-3ac5-03f79e6546ee",
"username": "testUser10"
}
},
"attributeValues": {
...
},
"errors": [
"Runtime Error"
],
"parentType": "Mutation",
"graphQLAPIId": "xxxxxx"
}
I just want to get the email and username from the context and populate my datasource(Dynamo).
What am I doing wrong here?