How to remove a reference with User Defined Function

In the previous post, we have seen creating a reference with User Defined Function.

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

Example

  • Use unlink function as a Groovy function to remove 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
}
  • unlinkStudentData function below removes the reference between StudentInformation object and StudentMedicalRecord object created via field medicalRecord.
type Mutation {
      unlinkStudentData( from: HypiMutationType! to: HypiMutationType! via: String! whereFromID: String! andToID: String! fromInstanceId: String toInstanceId: String ): Boolean! @tan(type:Groovy, inline: """return unlink(from, to, via, whereFromID, andToID, fromInstanceId, toInstanceId)""")
}

Let’s execute the function!

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