MgFunction
If you like to create custom function such as on AWS Lambda or Google Cloud Function, Microgen also give you that super power. Every function run on its own node or even clustered server. Function in microgen is called MgFunction.
Creating a MgFunction
Go to MgFunction menu on microgen, then create a new function via plus fab button
There are four trigger type on MgFunction such as, http, pub-sub, responder-requester, and table. Every trigger has its own unique functionality. We'll discuss it later.
Function "handler" is the main function that directly called on server. You can make another private function but, thats not will be called directly when server running, but should be calling inside the handler function.
Http Trigger
MgFunction http is a simple http protocol, or you can simply make a REST API here. You can choose GET, POST, PATCH, or DELETE method via the dropdown.
Parameter References
In every method you'll noticed that there is a dependency injection parameter such as "app", "context", and "callback".
Creating Http Trigger
You can either choose GET, POST, PATCH, or DELETE method. In this example we'll make a simple GET method.
Because we choose Http trigger, and GET method, later you can access it directly via REST API. This function will return a response that consist of simple hello message, and context data. Click "Save & Deploy" to run this function, and you can access it via CURL or browser.
Creating Http Trigger (Legacy)
If you are an old user in microgen, you will be noticed that the API code hook is not there anymore. It's because we move the API code in the mgFunction, so you can use it as normal function, and not losing your old API.
On the method dropdown, choose legacy method. Then you can simply make any API that you need.
to be noticed, that you must Re-Run the microgen in order to deploy this function.
Pub-Sub Trigger
MgFunction pub-sub is a simple Subscriber to listen any Publisher that trigger an event. This can be useful for asynchronous call such as booking, transaction, etc. In MgFunction, you can choose trigger type "pub-sub", and set the topic as you need.
Parameter References
Creating pub-sub trigger
We'll make simple booking function that save to transaction db when success, and sending an email to the user.
save the function with any name, and set the topic to "booking"
Trigger the subscriptions function
You can call the subscriptions function either via GraphQL publish mutation or via http call. Here is the example how to trigger the subscriptions via GraphQL
or you can make simple http function to call this subscriptions.
then you can call it via CURL or API
Listening the Subscriptions (Client Side)
You can listen the subscriptions on client side using GraphQL like this
Responder-Requester Trigger
MgFunction responder-requester is a simple Responder to listen any Requester that trigger an event. This can be useful for asynchronous call such as booking, transaction, etc. In MgFunction, you can choose trigger type "responder-requester", and set the topic as you need. The difference with pub-sub is, you can return a response from the Responder.
Parameter References
Creating responder-requester trigger
We'll make simple booking function that save to transaction db when success, and sending an email to the user.
Save the file name with "bookingresponder", and set the type to "booking". As we can see, Its not like a subscriptions because we can actually send a response to the requester whenever any requester triggering this event.
Trigger the responder function
You can call the responder function either via GraphQL publish mutation or via http call. Here is the example how to trigger the responder via GraphQL
or you can make simple http function to call this subscriptions.
then you can call it via CURL or API
Table Trigger
Table Trigger is work like hook on table services. Whether it afterFind, beforeFind, afterCreate, beforeCreate etc. We can do something like:
modify data context
triggering event (pubsub or requester-responder)
calling another API
validation (user, form, etc)
change query
Parameter References
Creating Table Trigger
Choose a designated table and one event whether beforeFind, afterFind, beforeCreate, etc. Then we can modify the context, sending event, calling third party API, etc.
For example we'll make a beforeFind event on a table.
Creating Table Trigger (Legacy)
The legacy one actually almost the same with the normal table trigger function. The difference is we must Re-run the microgen in order to make it work, and all trigger function can be used on single place like this.
Trigger the Table Trigger Function
Whenever any table action triggered, it will automatically call the function that you already setting up. So it can be from anywhere, whether it an event, http call, or direct table access.
Last updated