How to implement User Defined Functions

We have seen most of the APIs/ functions offered by Hypi!
But what if you want to implement a functionality which is outside the scope of those functions?

Answer is to implement User Defined Functions!

  • User Defined functions are the lightweight way to implement custom behaviour outside of what Hypi offers.
  • The functions are executed on the same server where the query gets processed.

After the post on Webhooks, let’s check how to implement User Defined Functions…

Currently, you may use two programming languages for the user defined functions: Groovy and Velocity. In this post, we will see a demonstration of using Groovy in a User Defined function.

Hypi platform offers the entire Groovy syntax. It is easy, quick, and performant to add custom behaviour using Groovy.

Example

Let’s say you have a functionality that requires looping through the data and implementing control logic. (for loop, while loop). In such a case, it is advisable to use Groovy’s statements in a User Defined function.
Here is a simple schema to implement looping.

type Query {
    upsertList(a:[String!]):Boolean @tan(type:Groovy, inline: """
        for(fld in a) {
            gql(\"""
                mutation {
                    upsert(
                        values: { 
                            Object1: [
                                { 
                                    field: "${fld}"                                    
                                }
                            ] 
                        }
                    ) {
                        id
                    }
            }\""")
        }
        return true
    """)
}

type Object1 {
    field: String
}

User defined upsertList function uses for-in loop to access the items in the GraphQL String List. Each list item gets upserted in the field of Object1 objects.

Execute the function like a GraphQL query and cross check if the list items have been stored in Object1 objects!

query{
  upsertList(a:["a","b","c","d"])
}
#result
{
  "data": {
    "upsertList": true
  }
}
#find Object1 data
{
  find(type: Object1, arcql: "*") {
    edges {
      node {
        ... on Object1 {
          hypi {
            id
          }
          field
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "hypi": {
              "id": "01FQEFHC37X683QT8KHB326K8K"
            },
            "field": "a"
          },
          "cursor": "01FQEFHC37X683QT8KHB326K8K"
        },
        {
          "node": {
            "hypi": {
              "id": "01FQEFHC39HF2KEDY9MC7PBTVW"
            },
            "field": "b"
          },
          "cursor": "01FQEFHC39HF2KEDY9MC7PBTVW"
        },
        {
          "node": {
            "hypi": {
              "id": "01FQEFHC3BMQB9BF9M9JWXQG8X"
            },
            "field": "c"
          },
          "cursor": "01FQEFHC3BMQB9BF9M9JWXQG8X"
        },
        {
          "node": {
            "hypi": {
              "id": "01FQEFHC3C5YJY081E815H3JT8"
            },
            "field": "d"
          },
          "cursor": "01FQEFHC3C5YJY081E815H3JT8"
        }
      ]
    }
  }
}