How to create a reference with User Defined Function

In the previous posts, we have seen deleting Scalar Array values with User Defined Function.

Let’s check how to create a reference with User Defined Function.

Example

  • Use link function as a Groovy function to create one-to-one or one-to-many references between objects.
  • Suppose we have StudentInformation data type that holds a field medicalRecord of type StudentMedicalRecord.
type StudentInformation {
    name : String
    dob: DateTime
    address: Address
    standard: String
    division: String
    contact: Int
    yearlyMarks: [Float]
    medicalRecord : StudentMedicalRecord
}

type StudentMedicalRecord {
    height : Float
    weight : Float
    bloodGroup : String
}
  • linkStudentData function below creates one-to-one reference between StudentInformation object and StudentMedicalRecord object via field medicalRecord.
type Mutation {
      linkStudentData( from: HypiMutationType! to: HypiMutationType! via: String! whereFromID: String! andToID: String! fromInstanceId: String toInstanceId: String ): Boolean! @tan(type:Groovy, inline: """return link(from, to, via, whereFromID, andToID, fromInstanceId, toInstanceId)""")
}

Let’s execute the function!

mutation {
  linkStudentData(
    from: StudentInformation
    to: StudentInformation
    via: "medicalRecord"
    whereFromID: "01FRT6WSG6KX0W26T0V3EJQ5XC"
    andToID: "01FRT70DE1B4ZJGM6XT7E6NGXM"
  )
}
#result
{
  "data": {
    "linkStudentData": true
  }
}