serverless-appsync-plugin icon indicating copy to clipboard operation
serverless-appsync-plugin copied to clipboard

Explicit flag for direct lambda resolvers

Open MarcusJones opened this issue 5 years ago • 2 comments

My goal was to avoid VTL request/response templates and go direct lambda. Finally I got an MVP running, thanks for this great plugin! It wasn't clear how to get it working, below solution for anyone else who is looking, and then some thoughts about making it more explicit.

service: mvp2

provider:
  name: aws
  runtime: nodejs10.x

plugins:
  - serverless-appsync-plugin
  - serverless-appsync-simulator
  - serverless-offline

functions:
  hello:
    handler: hello.handler

custom:
  # APPSYNC
  appSync:
    authenticationType: API_KEY
    dataSources:
      - type: AWS_LAMBDA
        name: myLambdaSource # data source name
        description: 'Lambda DataSource'
        config:
          functionName: hello
    defaultMappingTemplates:
      request: false
      response: false
    mappingTemplates:
      - type: Query
        dataSource: myLambdaSource
        field: getMessage

To my understanding, direct lambda resolver requires the defaultMappingTemplates request/response to be false? Isn't this acting as a global setting like;

appSync:
  directLambdaEnabled: true # If set, this causes defaultMappingTemplates to be disabled
  ...

This would be better as it least to make this explicit. Or even better to apply to each resolver somehow like;

    mappingTemplates:
      - type: Query
        directLambda: true # Default false
        dataSource: myLambdaSource
        field: getMessage

This could furthermore allow a defaultMappingTemplate to be defined for all other resolvers.

Thanks for the plugin!

MarcusJones avatar Jan 30 '21 21:01 MarcusJones

@MarcusJones There are 2 ways to use Direct Lambda currently.

  1. set defaultMappingTemplates.request and/or defaultMappingTemplates.response to false. In this case, it will apply to all lambda resolvers that do not specifically declare the request or response mapping templates.

  2. set request and/or response to false for specific Lambda resolvers.

mappingTemplates:
  - type: Query
    request: false
    response: false
    dataSource: myLambdaSource
    field: getMessage

Basically, what specifies if a Lambda resolver is a direct Lambda is if the request and/or response mapping templates are false (as in "don't use mapping template) Using defaultMappingTemplates is just a global value that allows you to specify a template for ALL resolvers (including non Lambda ones).

Note that Direct Lambda can be enabled separately for the request and response templates (It's not an all or nothing feature).

On a side note, if you are using Lambda to avoid VTL, you might want to have a look at this RFC

bboure avatar Jan 30 '21 21:01 bboure

Ok great, I had no idea this was an option, and it solves our use case - we want to have a mix of direct lambdas and VTL. Direct lambdas take care of complex cases, and default VTL handles simple DynamoDB access. I submitted #384 with an update to readme. Thanks for RFC link, will follow!

MarcusJones avatar Jan 31 '21 09:01 MarcusJones