In the last post, we have seen how to create and use the serverless function.
We have created SMSfunction
that sends SMS to a mobile number. We can execute the same function inside an after
function of a trigger.
In the trigger related post, we have created addStudentMarks
function that updates the marks of the students. Now we will replace the after
function of this trigger (formNotification) with another function that calls SMSfunction
. It means when the record gets updated, an SMS will be sent to the student’s phone number. We have used gql
to call the serverless function.
type Mutation {
#after function
sendMessage(sub1:Float,sub2:Float,sub3:Float, hypiId: ID,message: String,SMSto: String,SMSfrom: String):Json @tan(type:Groovy, inline: """
return gql(\"""
{
SMSfunction(to:"$SMSto",from:"$SMSfrom",body:"$message")
}
\""")
""" )
#serverless function
SMSfunction(to: String,from: String,body:String): Json @tan(type:OpenWhisk, name:"sendSMS")
# Trigger Function
addStudentMarks(sub1:Float,sub2:Float,sub3:Float, hypiId: ID,message: String,SMSto: String,SMSfrom: String):Json @tan(type:Groovy,
inline: """
return gql(\"""
mutation {
upsert(
values: {
StudentInformation: [
{
hypi: {id: "$hypiId"},
marks: { subject1: $sub1, subject2: $sub2, subject3: $sub3 }
}
]
}
) {
id
}
}
\"""
)
""") @trigger(config: {
after: {type: Mutation, field: "sendMessage"}
})
}
Let’s execute the trigger now.
mutation{
addStudentMarks(sub1:10.0,sub2:10.0,sub3:10.0,hypiId:"01FSA2NZRQK7M2SABRRNZ7E65X",message:"Record Updated",SMSto:"+61411111111",SMSfrom:"mycompany")
}
{
"data": {
"addStudentMarks": {
"data": {
"upsert": [
{
"id": "01FSA2NZRQK7M2SABRRNZ7E65X"
}
]
}
}
}
}
On ClickSend account, we can check if the after function was successfully executed.