MgFunction
Last updated
Last updated
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.
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.
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.
In every method you'll noticed that there is a dependency injection parameter such as "app", "context", and "callback".
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.
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.
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.
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"
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
You can listen the subscriptions on client side using GraphQL like this
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.
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.
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 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
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.
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.
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.
Parameter
Description
app.getRequester
to get Event sourcing topic from responder-requester trigger
app.getPublisher
to get Publisher and publish to its topic, to be called on subscriptions
context.body
get the http body
context.cookies
get the http cookies
context.method
get the http method type
context.params
get the http params
context.query
get the http query
context.headers
get the http headers
callback
return error and response data
Parameter
Description
app.getRequester
to get Event sourcing topic from responder-requester trigger
app.getPublisher
to get Publisher and publish to its topic, to be called on subscriptions
context
any
Parameter
Description
app.getRequester
to get Event sourcing topic from responder-requester trigger
app.getPublisher
to get Publisher and publish to its topic, to be called on subscriptions
context
any
callback
return error and response data
Parameter
Description
app.getRequester
to get Event sourcing topic from responder-requester trigger
app.getPublisher
to get Publisher and publish to its topic, to be called on subscriptions
app.env
get the env variables
context.data
get the user input data
context.result
get the table data
context.params.user
get the user data
context.params.query
get the table current query
context.params.headers
get the http header (Token etc)