In the previous post, we have seen implementing User defined functions using Velocity.
You may also use UDFs to call CRUD APIs, Authentication APIs, Maths APIs etc.
Let’s check how to use upsert
API to create/update data using User Defined Function.
Example
Here is a addStudentData
function to create data for a Student Information System.
type Mutation {
addStudentData(a: String, b: DateTime, c: String, d: String, e:Int, f: String,g: String, h:String):Json
@tan(type:Groovy, inline: """return upsert([
StudentInformation: [
[
name: a,
dob: b,
standard: c,
division: d,
contact: e,
address:[
door: f,
street: g,
city: h
]
]
]
]
)
"""
)
}
type StudentInformation {
name : String
dob: DateTime
address: Address
standard: String
division: String
contact: Int
}
*** Address is a core data type.
- There is a slight change of syntax for the upsert function. Since it is written in Groovy, we have used square brackets instead of curly brackets.
- StudentInformation field values are passed on as UDF parameters to create data objects.
- To update data, pass hypi.id as an additional parameter.
Let’s execute the function.
mutation{
addStudentData(a:"XYZ", b: "2011-05-07", c:"VII", d: "A", e:897675786, f: "23", g: "3A Old Street",h:"New Delhi")
}
#result
{
"data": {
"addStudentData": [
{
"__typename": "Hypi",
"id": "01FQK4H4SSPAGDB289PMB96SF1",
"impl": null,
"created": "2021-12-23T07:54:15Z",
"updated": "2021-12-23T07:54:15Z",
"trashed": null,
"createdBy": "01FQDY5XDRQPERKPCWAWYDFV7W",
"instanceId": "01FQDYDP8681299EXZBWJXRX2Y",
"tags": null
}
]
}
}