1

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
salt-pepper
  • 115
  • 3
  • 12
  • Also experiencing issues with these JS resolvers and I am not sure if there are some bugs that mean it is actually still trying to interpret the JS as vtl for some reason. If we put dollar signs in the JS in front of util like we do in vtl sometimes it interprets it. – Graham Hesketh Jan 25 '23 at 11:14
  • Think maybe you need to specify the runtime variable as JS – Graham Hesketh Jan 25 '23 at 13:14
  • The cloudformation seems to mention a runtime variable but doesn't mention JS https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-resolver.html But CDK does https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_appsync/FunctionRuntime.html#aws_cdk.aws_appsync.FunctionRuntime – Graham Hesketh Jan 25 '23 at 13:17

1 Answers1

1

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

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
Graham Hesketh
  • 317
  • 3
  • 16
  • you are right, JS resolvers only work with Pipeline resolvers. I updated my code and replaced RequestMappingTemplate with Code & added Runtime. It is now working – salt-pepper Jan 26 '23 at 20:00
  • @salt-pepper, can you please into this question also and suggest possible solution for it https://stackoverflow.com/questions/75422633/awsappsyncfunctionconfiguration-cloudformation-error-using-javascript-resolve – Cloudformation Feb 11 '23 at 21:54