Reputation: 125
I have resolvers written in JS and not VTL since AWS added support for it. I am using AWS SAM template and this is how it looks
MyResolver:
Type: AWS::AppSync::Resolver
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: addUser
DataSourceName: !GetAtt UsersTableDataSource.Name
RequestMappingTemplate: |
{
"operation": "PutItem",
"key": util.dynamodb.toMapValues({"userId": ctx.userId, "sortKey": ctx.sortKey}),
"attributeValues": util.dynamodb.toMapValues(ctx),
}
ResponseMappingTemplate: "ctx.result"
But when I query the mutation on the Appsync console I get the following error
Unrecognized token 'util': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\
I tried a couple of variations where I passed the entire value as string but it didn't work.
What am I missing or doing wrong in this template mapping?
Edit - My Updated Answer :
MyResolver:
Type: AWS::AppSync::Resolver
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: addUser
DataSourceName: !GetAtt UsersTableDataSource.Name
Code: |
import { util } from '@aws-appsync/utils';
export function request(ctx) {
return {
operation: 'PutItem',
key: util.dynamodb.toMapValues({"userId":
ctx.userId, "sortkey": ctx.sortKey}),
attributeValues: util.dynamodb.toMapValues(ctx),
}
}
export function response(ctx) {
const { error, result } = ctx;
if (error) {
return util.appendError(error.message, error.type, result);
}
return ctx.result;
}
Runtime:
Name: APPSYNC_JS
RuntimeVersion: 1.0.0
Upvotes: 1
Views: 1249
Reputation: 432
Think JS resolvers are only available in Pipeline Resolvers currently and not Unit Resolvers. Also, in a Pipeline Resolver you may need to add the runtime in someway that is similar to below. Without it it may be defaulting to VTL. Note, not tested this in SAM just guessing based off of Cloudformation and CDK docs.
AWS::AppSync::Resolver Runtime
MyResolver:
Type: AWS::AppSync::Resolver
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: addUser
DataSourceName: !GetAtt UsersTableDataSource.Name
RequestMappingTemplate: |
{
"operation": "PutItem",
"key": util.dynamodb.toMapValues({"userId": ctx.noteId, "selection": sk}),
"attributeValues": util.dynamodb.toMapValues(ctx),
}
ResponseMappingTemplate: "ctx.result"
Runtime:
Name: APPSYNC_JS
RuntimeVersion: 1.0.0
Upvotes: 1