sid88in / serverless-appsync-plugin

serverless plugin for appsync
MIT License
951 stars 189 forks source link

RFC: Preprocessor for request/response mapping templates? #108

Open lightsofapollo opened 6 years ago

lightsofapollo commented 6 years ago

I go back and forth on this one but I am considering it would be useful to allow providing a "preprocessor" function which would be responsible for loading the VTL files and doing things to them.

This could be useful for example by allowing mapping templates to be a JS file and this preprocessor function could load the JS files and execute them to generate VTL. We run into many cases where it is easier to codegen our VTL and this could be a better alternative.

WDYT ?

jpstrikesback commented 6 years ago

Iโ€™ve been thinking about this as well ๐Ÿ˜€๐Ÿ‘ especially useful for batchitem processing

On Sat, Jun 16, 2018 at 12:17 PM James Lal notifications@github.com wrote:

I go back and forth on this one but I am considering it would be useful to allow providing a "preprocessor" function which would be responsible for loading the VTL files and doing things to them.

This could be useful for example by allowing mapping templates to be a JS file and this preprocessor function could load the JS files and execute them to generate VTL. We run into many cases where it is easier to codegen our VTL and this could be a better alternative.

WDYT ?

โ€” You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sid88in/serverless-appsync-plugin/issues/108, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbMe7aRdRLz_AvJj-Na3BrKKFPMGnMHks5t9S-MgaJpZM4UqiDG .

lightsofapollo commented 6 years ago

Something like this?

custom:
  appSync:
    name: appsync-bootstrap-test
    vtl:
      preprocessor: ./path-to-node-module.js

Then in the node module

module.exports = (mappingTemplatePath) => {
  // load and do stuff to the mapping template.
  return 'THE VTL'
};

I personally want to pass a path instead of the content of the VTL so we could write our VTL templates in some kind of language (like js template strings)

lightsofapollo commented 6 years ago

@sid88in bump ^

lightsofapollo commented 6 years ago

@jpstrikesback ^^ mind taking a look ? I am thinking it would also be nice to allow passing arguments into the preprocessor function. I am not quite sure yet what the api would look like. We're tempted to try to build something in alpha quality for us to use for awhile before we contribute it back.

sid88in commented 6 years ago

sounds like a good idea. maybe you can create a PR with an example and how it would be beneficial. We can then iterate on it / merge it!

jpstrikesback commented 6 years ago

Hey @lightsofapollo & @sid88in!! It would nice to be able to gracefully handle both template literals and the current strings. That could perhaps be as โ€˜simpleโ€™ (I have no clue how simple they are ๐Ÿ˜€) as a webpack loader based on the extension?

What do you both think of providing variables at a global and local level? We could even leverage serverless a bunch to hydrate the keys:

 custom:
  appSync:
    name: appsync-bootstrap-test
    vtl:
      preprocessor: ./path-to-node-module.js
      substitutions:
        someVar: $env.thing
        someListVar:
          - 1
          - 2

And then the same syntax for individual mapping templates?

lightsofapollo commented 6 years ago

@jpstrikesback I was thinking that variables or "substitutions" as you reference above would be first resolved by serverless configs and then passed as an argument into the preprocessor module. So in your example preprocessor would look like this...

module.exports = (path, args) => {
 /*
args: {
  "someVar": "...",
  "someListVar": [1, 2]
}
*/
}

What do you think ?

cameroncf commented 5 years ago

I came here searching for something like this. Curious - has anyone made any progress on any of the above suggested solutions yet?