Automatic image tagging in Hasura Cloud with Cloudinary and Autocode
Tags: imageprocessing, actions, integrations
Published: 2021-04-30 10:41:45

---
title: Automatic image tagging in Hasura Cloud with Cloudinary and Autocode
published: true
date: 2021-04-28 18:47:43 UTC
tags: ImageProcessing,Actions,Integrations
canonical_url: https://hasura.io/blog/integrating-cloudinary-with-hasura-using-autocode/
---

 ![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://hasura.io/blog/content/images/2021/04/hasura_design_patterns_edit-1.png)

In this short tutorial, we will build an image storage integration with Hasura that auto-tags our images for us. We need to account for the following touch-points in our stack. We have a client that will act as the user interface. We have persistent storage for the images and for the user interaction data. Additionally, we need an external service that will provide the object recognition analysis of our image and suggest the additional, relevant tags in the image.

Our user journey involves uploading a photo, waiting for the photo to get analyzed, then be presented with the suggested categories, and save them to the right location.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh3.googleusercontent.com/S_iKQtvwKDUXMqhqDCKXkAuomBXHNEB4ymtu5yhPnswWwV61ApbaASAIoBIwRvTLhmLLr6Ds32prtSXIOZwKI6s9fIzFiFlkRT46lw-Hak_4FF79gp3b_2tyV29NCMjd7J3W0ETx)

Our data journey involves uploading a binary to our CDN, persist a reference to that along with our user and user data in our user database, update the image with some suggested categories, present that data to the user, then return with their confirmations back into the database, either keeping the machine suggested categories that were not confirmed or discarding – depending on the pattern we’ve chosen for.

Let’s reference the following chart to identify the parts in overview.

First, let’s look at a naive data diagram. How will our data travel through the application in a traditional architecture?

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh5.googleusercontent.com/pc-vw36l2d-KRBIWlZqxcXRV7yl51K5FGPwW3GArO7SNAXXg3nkwv2e7sz-qUmsuneXYxxwbAEDaHCXHI6_4_2wqr70UTo2-Qr8kvEBmUMgjzU8UamYmTMMBBvFY3g5_Bh7YQdbk)

Well, that diagram is a substantial departure from the simplicity of our user-flow. Let’s look at a domain-driven data diagram, an example of what a purist flow could be.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh4.googleusercontent.com/xcTGCQnmALCsIKyAdnu-KVjaL6yyk5IY3wk48u5_eJPF9SDwyB9nUmsbBScCZR8Jz_4fzA_zUWNkkDeIPfeWHSsxFVGwSfoh6_tHtf6aE8mHDOqxzjt0ljcYjN0YUCEZRlMzzCLe)

Something doesn’t align. Let’s see how we can improve on this design.

## Building the feature in Hasura

From the above diagram, we can see that our architecture is dramatically simplified. Let’s look at those distinct parts in some more detail with a small working example.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh6.googleusercontent.com/MI_0BvEqUO16nIGASKp9iwU8vDeCMeDnRJWg2PTzkromWu9lqMBBaBF_0LBZITKb9UQy-NL4jKJaZDREnoALEGk9wX2l8QYPWWUuSJCnI6VsV9yVHijon5wGv6FmwCSxWefYD_46)

The heavy lifter of our architecture is a feature in Hasura called “actions” - these allow you to define custom resolvers that effectively expose a webhook as part of your API.

We’ve chosen three services for our stack:

1. Our Image analytics, storage, and auto tagging is provided by [Cloudinary](https://cloudinary.com/) (using the [Imagga](https://imagga.com/) analytics addon).
2. Our resolver, or webhook, that acts as our proxy, will be managed in a platform called [Autocode](https://autocode.com/) for convenience and educational purposes.
3. And lastly, our user-data persistence layer, service orchestration happens in Hasura.

### Cloudinary

Configuring Cloudinary involves little more than registering for an account, choosing the analytics service add-on that we’d like to use, and locating our API settings. Cloudinary itself offers about as powerful of image transformations as anyone in the game, so if you’re looking for a good storage CDN, it’s worth a visit.

### Modeling our data using Hasura Console

You can configure everything through a CLI and migration scripts, but for our purposes, we’ll use the UI in Hasura Cloud. Our data model looks like this:

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh4.googleusercontent.com/Lx3UQjFg6h6lS8CPo28CwAS7C4hkhYnz1qjD2Bn1Pkmc2XNNpSGVahFZyZK7BBsLHjoD1c62T3s6tcolMhUN1sLFmPl-sNaDox6Oj8D6X_W421saL8rJ3ggN_UyVXBBeOgn7D0ID)

To support the idea of individual images having individual tags with their own confidence rating, we needed the join table. The join table also allows us to create a many-to-many cardinality in our data. This structure allows us to query our data with the following structure:

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh4.googleusercontent.com/QaClt0ZszWoKFmM7PZHSiwlbVhXlnl74SrIYhB3jPypExe1K60n_e4V6k0JJFC_M6NftZWCFvr3uvadd_jSZb-_ebfZK7G3JuswKL36_PqFbAdMK9uaPiPoZnPw1n6G2dex1IUxH)

### Custom business logic with Autocode

Our proxy layer is where we’ll do our business logic. For simplicity, I’ve abstracted my codebase into three main services. One is simply a promise wrapper around the Cloudinary SDK. The second is a simple transformation utility to help alter the shape of our data for sending back to Hasura. The third service is our glue layer where we stitch the two services together.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh6.googleusercontent.com/UOqsng8UNJZ4uuuNT6I3j4YDM6v4ZGdDfvteDTQUAGEtHPXI1T_HQaqR07-ndgeqvODJ_Ib6q7dCcgZz-K8TUhTeDKl-1GpCH8dAkxNg-yc9M8udt43NHfQisPVRPuhy3kMPG9fR)<figcaption>Cloudinary wrapper</figcaption>

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh5.googleusercontent.com/fPBzzatepoSgNll9aw8al4CTSgtJp_-9UsqxGrhT-YYkZwrHQmH8v637yymAxlP5Y4UTPfy0TjQqPjBMK1rMLw85yixWrKXzUk-CKIuXZrx17879UT3-d7P_ETaF_8p0u0wLwpNm)<figcaption>Tag Transformation</figcaption>

###   
Upload the image and transform the response

This first half of the code is where we declare dependencies, authenticate with the SDK, upload the asset, and transform the response.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh5.googleusercontent.com/KSMyd23vr49jD44b5VkdnxO34qgwMNDV1xsnnCrFoF95M5qjIJxxCEscvxpT2ZWpgSmrVQkCiVgz4WVfV36VGs6qvpmchEN7Aw-VGXHIf9IrcF3BURiBZnwLi1O9ojS1NvDkUVUk)

This last half of the code is where we write that response back into Hasura. Our mutation is worth looking at in detail.

Our data model is split across three different tables with varying degrees of dependency between them. Ordinarily, this would require a number of round trips. Utilizing the underlying GraphQL execution engine, we are able to write our mutation in a descriptive way, where we insert data intro three different tables, and benefit from their relationship to each other.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh6.googleusercontent.com/umV0zN0HjTyxs6NcrZLivP-Zz1CoqhefsD0I4jqSnBuj8vPsoAxycg2lAeNsPgqpjoZGaOsXP-0zwE2A6_23whQI5zfEsVbWuG2xgZgkaApvrAFXxpeiWWmP187xuSTg6xMTdQpe)

## Shipping the API

With our data connected to each other, and our custom resolver in place, we are able to expose a single, elegant API to build our app with. Because our response resolver returns a known type, we can enforce a relationship with our other tables, and be able to resolve the data that our proxy later wrote into our database for us.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh6.googleusercontent.com/XDjnsWt5SJFG758E820JW0Qybn9CAUJmCWXc4_q8X9DCw3MravlB-tAjDcmHMOEwr7ak_0xtXjL4Ey5ReW7c4OUVtsn8TYbGf-52lsDsBLg7BQIkB6z0k2huMLYNrSjuIhU0Pm6R)

### From MVP to monitoring and performance tuning

Our API is now a lean, shippable product that can be consumed on web-aware clients. But resilient apps are more than shipping functioning code. Let’s have a look at our monitoring tab to see how our API is performing.

As we can see from the errors monitoring, very few errors are surfacing, and we can drill into the individual causes.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh4.googleusercontent.com/_o8CVdN98DF4OHOL-6LdSiVqgcTUqaypuApYJDQg52wpK5yxXD-xX5yF8EaRfDxpN-ex8cyeA5Q5wEGbZEYKO9o9Sx84xD6nuLjcPdoOOHVa9zm9rmEEc2vnOC80QUZQah5ycnh-)

Let us check to see what the response time is for our upload mutation, which is performing AI content analysis, storing data, and performing some general IO.

If we inspect a test-case, our operation time is slow.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh3.googleusercontent.com/YBZM1Za6WqngzlbZCxJGDDTSZHmgXJSB02xRLcysbWzVv743Dli8tRtlQzN7dm_ZEI0zMHH2gGNRGCvZwdTLkRzQuqKErS6nP62ucmvp8mKjErFQ9KLwOcag8S_pDXPKrUA3j_1n)

Looking into the tracing, we see that the underlying engine, because there’s no existing data to reference at the time we create our relationships, is generating individual inserts for each tag we have. As a developer, it’s up to us to determine which optimizations are more important. We can split our mutations into few overall inserts, or keep the elegance if our users could be expected to wait. Let’s see what a small refactor would accommodate. In this case, we’ll split our mutation into two pieces. First we’ll write our image and our tags to the database, and then we’ll write our relationships.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh3.googleusercontent.com/nfQy0AJX7KFcUCiXyW9DegM0RUNkBhFVipR1YqZKNsJ7cEK4jxqBw_gAQEW2FdKjmUpPxQCNFqiyxelQ0qTusbM2JpMzL_ELjjw8aivoFbzRNxkwc9mn6FO_a_mRFMQFaXIdpkJs)

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh4.googleusercontent.com/RDPfoIT0aFzQdSwgRcRa1pLpZXx_0Lg-ghJWWtgyd6v8eiwhxGfvMxtceeYLSMli21wWN4hY-mljVg2UZSvm5gIPLkpsOXQEahmw4sG2cBcKJle28lxAzGGDd_DrHN6pt0QAQ235)

If we inspect our mutation after that, we can see things improved nicely! Our two mutations combined are now just over a 1 second.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh4.googleusercontent.com/Yxv8oqnM5ZC1fg4oVKqIhxx6isZSMUjT-m5R3boJLAVkylpkGGeFs5Aaq1ClWMUIxlSjMHrrh8LDPkLgP8Fvc4JOPj1PGMJnV0anK68aNTHhOiRX_gQwvHALPCN7GyYWa1zak6ym)

Our initial mutation without the relationship inserts is less than half a second, that’s a substantial performance improvement! That’s a 20x improvement in our mutation time.

![Automatic image tagging in Hasura Cloud with Cloudinary and Autocode](https://lh5.googleusercontent.com/WOmRJ6e6cCv9z34rfadAXK3Z98Qw-zHi0vR0T3QcddwZ8Hukag3FKSGozS2o1-kSkTewX7ib_HgQRTem1A57tbKyasyUUWYAIP9RdGFWA4WIYI_oyP1SSY0SDx9SiuVusnvVjU0h)

Beyond that, with Hasura, we also offer the ability to convert actions to asynchronous events, which will return an id we can subscribe to for when the long-running process finishes. And since Hasura provides subscriptions out of the box, we can take immediate advantage of them.

Now our action is performing much better thanks to the monitoring tab analysis, and our API is ready for shipping to our developers.