Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using custom function template with AWS Amplify

Using custom function template with AWS Amplify

MURAKAMI Masahiko

November 20, 2021
Tweet

More Decks by MURAKAMI Masahiko

Other Decks in Programming

Transcript

  1. Using custom function template
    with AWS Amplify
    Masahiko Murakami (@fossamagna) ESM, Inc. | 2021-11-20


    Amplify Japan User Grou
    p

    View full-size slide

  2. Introductions
    Masahiko Murakam
    i

    • Software Developer - ESM, Inc
    .

    • Amplify Japan User Group Core Membe
    r

    • Background
    :

    • TypeScript/JavaScrip
    t

    • Node.j
    s

    • Jav
    a

    • Twitter: https://twitter.com/fossamagna
    • GitHub: https://github.com/fossamagna

    View full-size slide

  3. Agenda
    •What is AWS Amplify function template
    ?

    •Authoring a custom function templat
    e

    •Third-party function templates

    View full-size slide

  4. What is AWS Amplify
    function template?

    View full-size slide

  5. What is AWS Amplify function template?
    • Function template is resources
    that are added by executing the
    amplify function add
    comman
    d

    • Lambda source code (ex: *.js,
    *.py, *.go *.java, *cs
    )

    • Exclude CloudFormation
    template

    View full-size slide

  6. What is AWS Amplify function template?
    • Amplify function template is
    plugin for amplify cl
    i

    • util plugi
    n

    • Plugin name is named
    functionTemplate
    • Template meta information is
    defined in functionTemplate of
    amplify-plugin.json


    • Implements
    functionTemplateContributo
    rFactory function

    View full-size slide

  7. Authoring a custom
    function template

    View full-size slide

  8. Authoring a custom function template
    The specification of custom function template plugi
    n

    • Generate AppSync Lambda Resolve
    r

    • Supported Lambda runtime is Node.j
    s

    • User can select GraphQL fields in schema.graphql to
    associate resolver

    View full-size slide

  9. Authoring a custom function template
    Implementation step
    s

    1.Run amplify plugin ini
    t

    2.Edit amplify-plugin.jso
    n

    3.Implements functionTemplateContributorFactory functio
    n

    4.Implements source code templates with EJS templat
    e

    5.Implements additional questions for user (Optional)

    View full-size slide

  10. Run amplify plugin init
    • Run amplify plugin init


    • Input name of the custom function template plugin (ex: amplify-appsync-
    resolver-nodejs-function-template-provider
    )

    • Select util plugin as typ
    e

    • Clear all selections for event handle

    View full-size slide

  11. Edit amplify-plugin.json
    • Set the name attribute to
    functionTemplate


    • Define the functionTemplate
    attribut
    e

    • The conditions attribute defines the
    conditions to which the template
    applie
    s

    • The templates attribute defines a list
    of user-selectable templates on the CLI
    {


    "name": "functionTemplate",


    "type": “util",


    "commands": [],


    "eventHandlers": [],


    "functionTemplate": {


    "conditions": {


    "provider": "awscloudformation",


    "services": ["Lambda"],


    "runtime": "nodejs"


    },


    "templates": [


    {


    "name": "AppSync Lambda Resolver",


    "value": "resolver"


    }


    ]


    }


    }


    View full-size slide

  12. Implements
    functionTemplateContributorFactory function
    • Implement a function named
    functionTemplateContributor
    Factory in index.ts


    • functionTemplateContributor
    Factory function takes a context
    as an argument and returns an
    object with contribute functio
    n

    • contribute function is a function
    that takes a
    TemplateContributionRequest
    as an argument and returns a
    Partial

    export const functionTemplateContributorFactory: FunctionTemplateContributorFactory =
    (context) => {


    return {


    contribute: (request) => {


    switch (request.selection) {


    case 'resolver': {


    const resolvers = await askGraphQLFieldsQuestions(context);


    const files = fs.readdirSync(pathToTemplateFiles);


    return Promise.resolve({


    functionTemplate: {


    sourceRoot: pathToTemplateFiles,


    sourceFiles: files,


    defaultEditorFile: path.join('src', 'index.js'),


    destMap: getDstMap(files),


    parameters: { resolvers }


    },


    });


    }


    default: {


    throw new Error(`Unknown template selection [${request.selection}]`);


    }


    }


    }


    }


    };

    View full-size slide

  13. Implements source code templates with
    EJS template
    • Templates can be written in EJ
    S

    • In the EJS template, the return
    value of the contribute
    function can be accessed with
    the props variabl
    e

    • This can be used to query the
    user and reflect the entered
    value in a file that outputs
    /**


    * Using this as the entry point, you can use a single function to handle many
    resolvers.


    */


    const resolvers = {<% for(var resolver of
    props.functionTemplate.parameters.resolvers) { %>


    <%= resolver.name %>: {<% for(var fieldResolver of resolver.fieldResolvers)
    { %>


    <%= fieldResolver.name %>: ctx => {


    // Add your code here


    },<% } %>


    },<% } %>


    }


    exports.handler = async (event) => {


    const typeHandler = resolvers[event.typeName];


    if (typeHandler) {


    const resolver = typeHandler[event.fieldName];


    if (resolver) {


    return await resolver(event);


    }


    }


    throw new Error("Resolver not found.");


    };

    View full-size slide

  14. Using custom function template plugin

    View full-size slide

  15. Third-party function
    templates

    View full-size slide

  16. Third-party function templates
    • https://github.com/thorsten-stripe/amplify-stripe-
    nodejs-function-template-provider
    • https://github.com/fossamagna/amplify-appsync-
    resolver-nodejs-function-template-provider

    View full-size slide