How to use gql function inside User Defined Function

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

Let’s check how to use gql function inside User Defined Function.

  • Using gql, execute mutation or query operations inside the user defined function.
  • The result will be dependent upon the type of operation and identical to normal operation. For upsert mutation, it will return id, createdBy etc. For find query, it will return a list.

Example

We will rewrite addStudentData function from this post using gql function.

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  [gql(\"""
    mutation {
        upsert(
            values: { 
                StudentInformation: [
                    { 
                        name: "$a",
                        dob: "$b",
                        standard: "$c",
                        division:"$d",
                        contact: $e,
                        address : {
                            door:"$f",
                            street:"$g",
                            city:"$h"
                        }
                    }
                ] 
            }
        ) {
            id
        }
    }\"""
    )
  ]r
  """)
}

Please note the use of quotations " for string variables inside gql.
Let’s execute the function!

mutation{
  addStudentData(a:"ABC", b: "2011-05-07", c:"VII", d: "A", e:897675786, f: "23", g: "3A Old Street",h:"New Delhi")
}
#result
{
  "data": {
    "addStudentData": [
      {
        "data": {
          "upsert": [
            {
              "id": "01FS4E5580W7STRY38KVBRTJH8"
            }
          ]
        }
      }
    ]
  }
}

In the next post, we will see how to use gql without String interpolation.