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

RFC: Preprocessor for request/response mapping templates?

Open lightsofapollo opened this issue 7 years ago โ€ข 8 comments

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 ?

lightsofapollo avatar Jun 16 '18 16:06 lightsofapollo

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

On Sat, Jun 16, 2018 at 12:17 PM James Lal [email protected] 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 .

jpstrikesback avatar Jun 16 '18 21:06 jpstrikesback

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 avatar Jun 17 '18 15:06 lightsofapollo

@sid88in bump ^

lightsofapollo avatar Jun 22 '18 00:06 lightsofapollo

@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.

lightsofapollo avatar Jun 22 '18 21:06 lightsofapollo

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!

sid88in avatar Jun 22 '18 21:06 sid88in

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?

jpstrikesback avatar Jun 23 '18 22:06 jpstrikesback

@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 ?

lightsofapollo avatar Jun 24 '18 00:06 lightsofapollo

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

cameroncf avatar Apr 10 '19 16:04 cameroncf