Code with passion
Mar 4, 2022 3 min read

Hasura - Actions and Declarative transformations

The Latest updates in Hasura graphql engine lets us create custom graphql endpoints with custom transformations without writing any code. If there are any transformations required, it can be defined with Hasura’s JSON templating language called Kriti. In this blog post, we’ll see how we can use it for a real-world use case.

Tryout Hasura Actions with Declarative Transformation

In the previous blog post regarding the platform, we created API to understand natural language inputs from user for creating todo tasks. This post we’ll see how we can integrate API with Hasura.

It’s better to use this API endpoint via Server to Server communication as we need to pass the secret token to It’s unsafe to query directly from a client App. So, For ex, if we’re creating a React Todo App. Then the React app will call the API server with the query and then API server will call and give back the response.

We’ll use Hasura to forward this request to and get back response using actions. With Hasura, We can do this without writing a single line of code.

Okay, Let’s Begin

You can follow the getting started with hasura documentation to create a Hasura project in their SAAS platform and connect to a new database for free. Hasura will use Heroku to spin up a Postgres database in one click.

After this, we can open the actions menu to create a new action. First step is to define the graphql object models to represent the request and response models of the new action.

tip: you can use JSON to graphql schema site to generate graphql schema from any JSON.

We can use the following schema definition for action input and action response in hasura console.

  1. Create the required input and output type
input Input {
  query: String!

type Output {
  text: String
  intents: [Intents]
  entities: jsonb
  traits: jsonb

type Intents {
  id: String
  name: String
  confidence: Float

Follow the screen capture:

create a action

Although we’re using the default mutation option to create action, a query type action would’ve made sense instead of mutation as we’re only querying the external service not changing any state. Will update later.

  1. Add the API endpoint to webhook handler input box:

  2. Add Authorization header and bearer token taken from settings.

  3. Configure the Rest Connector to pass query as URL param to endpoint using the Request Options Transform option.

As require two query params v (the version of the NLP model) and q (actual full text query from user)

select Get as http method and configure Url param q and v with the following values:

  • q: {{$body.input.arg1.query}}
  • v: 20220304

Hasura uses its JSON template language kriti to define the action transformation. So in the above template definition we’re telling Hasura to take query from the graphql POST request body (path: body.input.arg1.query).

  1. Goto API panel and try hitting the created action

I’m assuming that you have some basic knowledge on how to use Hasura graphql engine console to hit query/mutation. Please go through the docs if that’s not the case.

hit query

The graphql engine will parse the response from Api and send to client. That’s it for this post, Next we’ll see how to use this graphql action in a React Todo App.

Happy Coding!!