In many databases and some other systems, it is possible to define a trigger that is executed before or after an event has happened. A trigger is used to maintain the integrity of the data. Or it can be used to automate a response to any event. For example, sending a welcome email when a new customer adds to the database, validating the data before creating an entry, etc.
In Hypi, triggers are supported with both a
after semantics by adding the
@trigger directive to a function.
before function gets executed before the function being called. It can be used for validation or calling other services. If it fails, it prevents the execution of the function being called.
after function gets executed after the called function. It cannot affect the results of the function being called. It can be used to notify another service of an operation being compelted.
Important points to remember
Triggers can break existing functions if you decide to completely replace the output. You may override the existing functions using Trigger.
beforefunction is executed synchronously. i.e. it will complete before the function it is attached to.
afterfunction is executed asynchronously. i.e. the function it is attached to will return before the trigger is completed.
If a trigger's
beforefunction returns false, the function it is attached to will not be executed, nor will the
Let’s go through the below schema to create a trigger and before/after functions:
In the above schema, three functions are declared to which trigger is attached using the @trigger directive. (functiontrigger1,functiontrigger2,functiontrigger3).
falsereturn functions work as before/after functions. Trigger functions and before/after functions are implemented just like User Defined Functions.
For functiontrigger1, before and after functions return ‘true’. For functiontrigger2, before function fails. And for functiontrigger3 before function returns true, but after function fails.
Let’s execute the functions!
- GraphQL Query
functiontrigger1is successfully executed as
truereturn) returns true.
truereturn) also returns true.
functiontrigger2fails as before functions (
falsereturn) fails. You can see the error message here
Pre-condition given by Mutation.falsereturn failed. As
beforefunction returns false, the function does not get executed.
functiontrigger3is successfully executed as
truereturn) returns true. However,
afterfunction returns false. But this does not affect the execution of the trigger function
AfterResult object holds the name of the function (before/after) which failed. Let's verify the objects using find function.
- GraphQL Query
You can see that 'Before Function' of
functiontrigger2 got executed and it returned false. Hence the Trigger was unsuccessful.
'After Function' of
functiontrigger3 failed. It indicates that After function got executed and failed but still Trigger was successful.
We have added
AfterResult just for demonstration puropse. It indicates how After/Before function can be implemented.
The in-built function of Hypi can be overriden using Trigger. Function overriding is allowed so long as all parameters of the new function exactly match the original parameters. This can be used to arbitrarily allow or deny access to a functionality depending upon the event to be handled by your App.
Care must be taken when overriding functions.
upsert for example is used everywhere - breaking it will cause many things to fail.
Let's override built-in
upsert function using
@trigger . You may allow or disallow insertion of data by overriding this function. Please check below schema.
If you insert the data, the upsert function works as before. The data gets added into the object successfully. However, if you set
before function to
disallow, the insertion of data fails as the disallow function returns false.
- GraphQL Query