DianaIonita / serverless-api-gateway-caching

A plugin for the Serverless framework which helps with configuring caching for API Gateway endpoints.
ISC License
136 stars 34 forks source link

added support for cache keys from header mappings, (e.g. method.request.body) #72

Closed mgyarmathy closed 4 years ago

mgyarmathy commented 4 years ago

HI @DianaIonita!

Similar to PR https://github.com/DianaIonita/serverless-api-gateway-caching/pull/65, I needed a way to configure API Gateway to cache responses from my GraphQL Lambda function using method.request.body as a cache key.

In order to prevent breaking changes, my solution was to branch off of the existing code if the cacheKeyParameter includes a value property, which indicates the cache key is from a mapped HTTP header. This new functionality also opens up the possibility of using other values like context.VARIABLE_NAME in mapping expressions as well.

I have updated the README and added additional tests to document this new functionality.

With the following configuration, I was able to achieve my desired configuration for a sample /graphql endpoint:

cats-graphql:
    handler: graphql/handler.handle
    events:
      - http:
          path: /graphql
          method: get
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.querystring.query
      - http:
          path: /graphql
          method: post
          integration: lambda
          caching:
            enabled: true
            cacheKeyParameters:
              - name: integration.request.header.bodyCacheHeader
                value: method.request.body

GET image

POST image