A Tutorial using Hasura with Draftbit, a low-code tool for building mobile apps
Tags: mobile
Published: 2021-08-05 18:02:32


In this tutorial we will integrate Hasura with the low-code mobile app development tool Draftbit. We will scaffold a basic application with Draftbit, map the required data to a couple of GraphQL queries, and save those queries behind idiomatic REST endpoints in the Hasura console. As mobile-first development continues to lead many of today’s go-to-market strategies for new initiatives, tools like Draftbit allow teams to iterate on layouts visually, with native code running in the background.

## The Hasura features we’ll use

We will be using Hasura’s ability to generate instant REST endpoints to integrate with the UI components that Draftbit makes available to us. The primary glue to integrate Hasura with Draftbit is creating idiomatic REST endpoints for GraphQL queries in the Hasura console. This will allow us to define a query for Draftbit (which doesn’t support GraphQL), on the Hasura side and save that behind a RESTful endpoint. More on that later.

## Our data model

We’ll be re-using an existing API for our project from an auto-tagging image service. [If you’d like to follow along you can find the original tutorial here](https://hasura.io/blog/hasura-design-patterns-asynchronous-content-enhancement-and-analysis/). The concepts involved are universal, so you can feel free to simply adapt the use-case for your individual project.

As a point of reference, our model reflects the following pattern.  

![Diagram of content model.](https://lh6.googleusercontent.com/cESwXjkMXDvlimORQJymOJWRifOLOhyKpjKVM1eYdfAwY_16ZqzaLNFA3xYxMadY76LCY0XuBXThl3RYHJTSgY_Aht9RI71_DbpJ05Sr1fbRNs7NZiGllGVKFD2TcEyLmBGOj0Uf)

The exact models vary, and include an upload join table with both photo and tags associated with a user.

## The interface

For the purposes of learning we won’t be creating anything detailed with the interface. We’ll be using stock components from Draftbit and create an interface that let us view all the photos in a list, then view the details (tags) for an individual photo.

## The Tutorial

**Prerequisites:**

*   A working, accessible [Hasura project](https://hasura.info/dev_jesse) (not running on localhost) with existing data.
*   [A Draftbit account.](https://draftbit.com?ref=hasura)

**From Draftbit**

Create a new project from the Dashboard view.

![Draftbit dashboard.](https://lh3.googleusercontent.com/7xZl4QIoCQ7HEHsCBcs4-L67vnQT1rjOCWpBFZpBZXQ57NXeC5rhemwZSvGn7RgHjlLotaLtew4rDbBv9SrovSrOA2xnq18dMtsrSZhwTn9igCDiPqnCskZpERGAQy0hCi9UDqgL)

Provide some details about your application.

![Name and description for Draftbit project.](https://lh4.googleusercontent.com/a8aEa6wuOgYllUYlSl7rixaSElG604QG-mjU-Z_Zmchs7fjgcoE4q9o_VJMOS5YsXzMjVT_mfKonidNrMATu0kougvjPiR7YV8fQnq7X_cMpl2gnUflyBsudEujhVelcRdZ4jFM9)

And for our demo we’ll be choosing “Posts / Content” and “Feed” as our template starters. Allow the project to finish initializing, then click “Start Building”.

![Choosing starter templates.](https://lh6.googleusercontent.com/nDoNbpIDBd639_X22Acibf5-AvlLnELukOTfch-G4F0EIhRODmNk_9FrkZtqwaA5b0BZR0u1qg8f88LvlUTrNqtyZwwUK6_JtxBImDFEgcN5VyyxknJB-BEcKKGNbPOyeElF9_mz)

The interface is quite intuitive with standard looking design and preview functionalities. You should be seeing an initial screen with a pre-filled feed of some data.

![Editor in Draftbit.](https://lh6.googleusercontent.com/OaiWceLxQizE8B36gUOHVrLoJqperp8AU_HyDsQh1L2N28kU0eoCiet8tz1ZGpOkAOGvVfRSjtBLOrSxTsEz_KUOA_gfV56VKORj11tKKB3WZiKTlkpVJj0Aec5c_L0sDVl9YG9U)

Let’s see how far we can get with these default settings by simply replacing the source content with our API.To do that, we need to select the data tab from the right-hand menu, and choose a custom API for “REST”.

Many tools including low-code tooling are optimized for REST consumption. This is exactly why Hasura has support for creating REST endpoints – for handling the widest range of use-cases possible.

And the best part is you still get to leverage your GraphQL experience!

![Add Cloud service screen.](https://lh3.googleusercontent.com/-A-N0nykdzQ73V_QzzfJ-0lgNHsu321V5hQJHiliHbXkdZ96qFzQ3Gnv5CY9_fjhTlurMIRthZzYJPf_yIkSoLXFkeVAlDCNibJjeNRXFXD1k6sW4x6hwN8uThTBCjwXZl90SLkl)

We will be creating a base resource here that we can use throughout the application. In this case, I will simply name my resource “Hasura” – and for simplicity, I will provide my Hasura Admin Secret to authenticate the requests. You could use a user context for requests later on. If you would like to see user authentication with low-code, let us know!

_Side note: You can create Global variables under the settings tab for the project to avoid pasting string values into the configuration here. These are loaded into the environment during runtime._

![Global variable definition screen.](https://lh4.googleusercontent.com/tSQ7SpAEouYMOfll6tD-O1FmV35CdOcWEG0Q9hPqhZz5wmutRvVxArjzJjJ8TwgEnr-GGd1Mp4dPlaglVcV-mC4HaoMRO4IJZZIY8Qlp4A57Q8PfgN6XmZwvPh4yeuwufv1MeNpv)

Returning to our REST API dashboard, we need to configure three critical parts of the resource definition.

![REST API definitions.](https://lh3.googleusercontent.com/x_L8jKJCzV97sfEmVPilGdD2EgTRK4xrzpSTrRbaJ91AuqOHecrGmEZ_iejai6RAvEeXlr0bb6b3hfSIjA67LfiNb7Fh2U4_655VFYCvrnNEg3kLpfub_ajTuzYgOfbXCDDJ0J4z)

We need to provide the base API path for REST requests to Hasura. This is the part of your API before the “v1/graphql” path segment. Then add “api/rest” at the end.

Secondly, we need to define the Hasura admin secret header (x-hasura-admin-secret) and the value - which I have defined in a global variable called “HAS”.

![REST API headers.](https://lh6.googleusercontent.com/xmV-pGW-uYKGtB7trpksgFVOVTolzTWlP3zO1P2N_saJgEDUTClCEs-6lwJ7cA_fq-GOkg5LxPPrCC6sbIk3El92egwxxQ6lpjRHR2m6k_erCOTEFA2Gcjuk9iNh_pjbGEXxtIK-)

Once you have saved the resource, you need to define some endpoints which will be available to the application. But before we do that, we need to now turn to the Hasura console to define what those will be. Open your Hasura project in a new Tab so we can hop back-and-forth as needed.

The default UI from Draftbit’s feed component included a date, image URL, and title. Let’s fetch the equivalent data from our API. We won’t worry about pagination in this tutorial. I’ll create a named query operation that will request this data. It’s important for the next step that this be a named operation. It’s also a good best practice to name operations in general for tracking down performance leaks in our application.

![Hasura API playground.](https://lh3.googleusercontent.com/PSuyw2daobX_dUk5lGCOAyw_rCbfKjwunsqy6503R5OT9VI3Xe3A9U7_keqr_4z1SP93lRUp8H3eAizUTnkOphJvM0O-LI_eOyR-4mALnvXZ95BswmkN9l1ZLcnmIXLe1S1fVnpU)

With our query working, we’ll now navigate to the power feature available to us in Hasura which enables this entire integration.

![Save to REST button.](https://lh4.googleusercontent.com/h6O1XlgIX8tAox-TfSyX_XzPcOrPWL6moeiJPH5e1m2OYBo5FaL7oC8Rh6OIiXXqgS1nvv5QjohUtxWfohnNCmxRj96XMx4EyVcrXhlGI5y9eT4Z1iczsQ1ckA4HEkhBwC0C7OP-)

From the REST interface, we can name our operation, choose our protocol, and create our endpoint. See the image below as a reference.

![Saving a REST API.](https://lh5.googleusercontent.com/sTPqLOvTXvmLglB7NyqmnwdlnhNq42n7_95pbgsxsIs5LWECWSuXrQxhdFH2K2IuqVSUWw4eF2aBgOOHDgidgEPEINjULsvWZSlay5hOk_9QMJ_dQUwoNceW80bJEBcaZKm0hvzY)

We’ll return to Draftbit and add an endpoint.

![Creating endpoints in Draftbit: Name.](https://lh4.googleusercontent.com/fh7RVcRkPPRu9aJn7CMXLo_ee4qss5cpza3YHr43I7LxRKJc2qN1quBl7_VztIMFB2G9gYCv9hBWXW8jLsmF4Blzgb2Z9EEz17CeFEoHDXMUzmu8sqV2ghb97N3RxStUCDmIsta-)

![Creating endpoints in Draftbit: Path.](https://lh4.googleusercontent.com/NnEWAVJvQFNq2X0EEf60JpfIV5bD9PjqWYbS5GBrRD7Q1tb6OgqVbQulxwYYhttdGuvEkowJi282McdtbzdFTZWx1uZmn2kjLmm_4F8O80KyZd-JoghUUKM5YQ1dra8HOGfsZL1q)

Skip step three.

![Creating endpoints in Draftbit: Test.](https://lh6.googleusercontent.com/toim5ka3GVEm4UsNLtFeXUPxCAmVuDVyrAef83FDl0A0nM0lEKqtUC_7bzjc4gtz41DbFDWJHhxq_xlLOl_YdLydsBmNo_Za7wS9PsfZMDGji5Vc5D7X_ZKv9J5MDFMCnztN700Z)

Save that endpoint and click done on the API resource window. We’ve got our first resource from Hasura fetching data!

Now to iterate over our content, we need to tell our UI to use this new data source. Click on the “Fetch” component in our Layout tree on the left side of the screen.

![Select List component.](https://lh5.googleusercontent.com/MP1_Kp-TfFhl0D7g72zO6vjO2G3Ave2URkB0iXJcVifRm214cWWouMxLkbn_fDNPFeyCm0WhunURQ1-0QdxmjEjF3sVLq6jRPf5crIP6UCkiGguMFjFg4BirLeJy1ph0NrZ8Gslp)

From the data properties pane on the right side of the window, we’ll need to configure our new data source as the feed provider.

![Define data source.](https://lh5.googleusercontent.com/ua36DI1NV-kibOQh6Dg8YbzA-tV234HHoCQqBklki6r4INDTI3IKE2ZO21kkUFukhqcCfwzTmJVwTXVmNUKyaTFu4_nYJNnkvbbd1MpGvpJSte-iadckKCc83_oeud14C7onU1uy)

The blank screen means that it is working! Almost…

Now we need to choose the List component in the Layout tree on the left and ensure we’re still viewing the Data properties pane on the right. For “Data” we need to choose the response from our API that is of an Array type - in our case, it will be the first key in the response called “upload”.

![Selecting data to pass in to List.](https://lh3.googleusercontent.com/Oe9iySO199vyrwYe0yvrDqPjzn0pib6LbNqKlLBdH0RZCvWXum2ilPZLdYQ0pMX3pMzWRFiLaRS3J-Z5gUlWSYP5qe28h6S5eGj3qLX-ZxX2FynKoIhFRv9nkwMaT_97TrxHetwN)

You’ll see the list component update now to look as though it’s iterating over “items” - though they are all invisible at the moment. Now we’ll update which pieces of data will be fed into the UI and at which point.

Select the “image background” component in the Layout tree. From Data, we’ll choose the Source dropdown of the Setup section. We’ll look for “image.url” from the dropdown.

![Mapping image background.](https://lh6.googleusercontent.com/q3Jjla7q268BsTMfJlaJ-CqxvaNK7LaBVgmLMXIml4HH6WOkE8_bMUfe-gTT66T8uQwNtX5bYd278jCu2j-ja4VWjXzYv6-iqrJJWXvb2GKcQh0mm47erTIJstd8SLVy2V1Nu2jl)

We have live art on the page!

![Viewing the current state.](https://lh3.googleusercontent.com/Hszg7I8kheXMnxcd4I9LYF75ENNAHCHqgBX7lB1IDq02l9q3sz6eVlsfERlSSzMFlF4bLE7SGo5Hck6II--3KSM-qPEa3oy4ee0Wuc8VvIr7hhRvjBbCVnx4aiqN9QT1ZYPPfwfY)

Let’s move on to the date and title.

![Mapping date and title.](https://lh4.googleusercontent.com/skDZb27jUzYyi0YHcmf2m-tNvIFCbr4C3kPpd24m1AsKCdOQ2IcsLaKx_DfpKEETCAbvKDkz8pPmDbwXTRSZpsKTTTanhUt3F4LsndGuo0_s8c2bHeDedbZHd7bWPB6rV1Nl2ps1)

It seems that our date is coming back in an undesired format, let’s create a custom transformer to handle that.

Click on “custom” in the toolbar across the top. We’ll write a “low-code” transformer that runs on this bit of text.

Under the “Functions” menu item, click “+ Add” to create a new function.

![Create a transformer.](https://lh5.googleusercontent.com/B847N7GTaueIh6gGLtelMcAd2SGgLNR7ppBg0ua5Ykwytl4RkLRHj3iw4UzLr2DVGCfdMbUmL7CgOd0N7I_UDDgq6WcXIjEel4qCHhTBnWqSar-LTagrf0LYRFeoViojpLbNv91Y)

Under the information for the function, provide a name and input parameters. For my date, I’ll do a simple Locale String parse. You can run any native Javascript and access some libraries if needed.

![Code for the transformer.](https://lh3.googleusercontent.com/PCQ-cvUkQhzYuFebu2eqYSGo4ABNb8L0XNoiZRgg8tZwObZEUkxloqAVc_aEMZr434bREj1G0pSifHuWDAMvadHV-1W_eBhWjSxA7fpyp14l5b6ptZ4xe4P2NTwFFDbfgj71YpJW)

Select the new transformer and now we have nicely formatted date and time.

![Previewing the result.](https://lh3.googleusercontent.com/bd3GzycERUsaMkksHA6zdDuM-oEKRueFPZmraEnQxsH6mixJh7i3lisjGE3-76Vhw4Q32YfF8GP_9Ur4J-tB34fj13I60Cv6N6qD6uLTOZeXKsl0ayK-rw8y1txgiC6amT-dNBuD)

Let’s repeat these steps for our filename.

![Adding transformer for title.](https://lh3.googleusercontent.com/NW_aY-tSzImh66jzBBTndYW2fUBxuVF50zUTQpLCEYLFactcsFl5OClUxOEQtlV3yZKTQ2A87lXsE9Pf-jckeKfLkIYhXR9Xxk5MIJLTG3gCupyCch7Gd3tIRgcoNFDLk-ySzRoh)

The code for the second transformer simply replaces some repetitive text in the titles.

```js
    function niceFormat(text) {
        return text
            .replace("image-from-rawpixel-","")
            .replace("-jpeg.jpg","");
    }
```

Play around with some of the formatting options for the different components. In my case, I wanted the posters to be full-size. The end effect looks like this.

![Preview updated design.](https://lh4.googleusercontent.com/s2rZzlC2tckWSRbSXG0geIc1TOaOf-vybyqLPvtq12rxl2LV7UbB7-G3rVI-6l3IqY4Gd9PLFKIrP69-ZecPZo7DJRjPTVKXRQaLcBAyRNTBf0ho-P4PfQXivhzJGPcBzDM443Qt)

Now it’s time to handle individual details. We’ll tell our “Touchable” component to pass the id of this box to the detail view screen.

![Update date definition for touchable component.](https://lh3.googleusercontent.com/-FJnp2v7GicZNj9cRRBRaNIxxvv24F-mToBKTmMBEX2-TT6brT4uxbF_AH-KHtwSi7jK6MJLhhHH4IOvzHISu7dCi2I8GXOJHz-dBZRHkKDY8VRLSYx92QLDBu8mGh7oP-VE4WUB)

![Update fetch component for top-level.](https://lh6.googleusercontent.com/WZmWzL5GiVeWNb6j3hYTnIg6yzoTitFc8oSLNL785qq7BKDPJJUYros60dDKBoX8QxDoEGgmxkXRRmKYoeDB_tmKXjlXgkcdvYtdG0hIHZP8eqKxtyai_CRcB6ORqSTSzPLlKhHn)

With that configured, we can now navigate to the Article View screen.

![Update the article view details.](https://lh4.googleusercontent.com/aH0mhH4cG4gGWe2STs14SnyIIH1J59z1i5m8zkfDzum2cldGfXGWVrWQ7HWKJZDDTrj5EHydgvAoE6RrNDK9aSHnBfWbCTLcTbsvE9kX9GtkpumeWKPn5l6Ymlo88zN1rn2Ifi_B)

Looking at the top-level fetch for this view, under the Data panel, we can see that it passes “articleId” as a parameter to the URL structure of the request.

What this means is we will pass a variable with the value of the ID of an individual upload to a specified REST endpoint. Let’s create that endpoint in Draftbit now.

_Note, we can provide a test variable that will be run in the case of a non-existing URL parameter, as is the case in development mode._

![Defining default case.](https://lh5.googleusercontent.com/Y6MKPAv0kLBNSa0SXy31ZmW_pANe-PcReBjIOExIEog_fF_ccAHnWVEq_k0ERZjDD-iwfm1YCibnFL0JAVPiCb1zmShlUj2g8b5kJgGp4T2xLcaduR1eHdi1WiYxNu-PAm4o9pZ5)

If we test that now, however, it will fail, since we have not defined the endpoint in Hasura. Let’s do that now. First, we test the query we’d like to persist to an endpoint.

![Creating single endpoint in Hasura.](https://lh6.googleusercontent.com/E7BZtWzFkZar-_eUM2UjTxRN7pog0zN2eCI3IRofytF_1DRAs_8snu-D-ORETNdwbhcDs4F7oMzkAazGzKVdaK-7O94NsKu6civ1_W43qM_IahEPUVZVsT36lJR97P_Rdxqmge5U)

Second, we need to save that as we did with our listing API call. Using the syntax _:variable_ lets us define where we’ll accept inputs in the REST API parameters.

![Save the API endpoint.](https://lh3.googleusercontent.com/LJAPaotttnMv84PNtOncKyb2Rzqm8eDO4dgQandbSvThX-MdSTvxvdyzuQFaa8VFXRicHiZFTbWSOsvGEbCnV_-xf92ku8tPTWQLI5lBv_AXHJ__ZWDoij2VRV8PP0Zlp18gE9bx)

Back to Drafitbit, we can save that endpoint now that we have a successful Test case run.

Returning to our detailed view, we can select our top-level Fetch in the Layout browser, choose our saved Hasura service under the data tab, select the Get Single endpoint, and choose the articleId we passed in from our list-view (also defined in our test case).

![Testing our result.](https://lh3.googleusercontent.com/vTRFKomGkqUwAmlzcVPn1cZIRxmjp-zDX_DLz4objBRDwyL-tHSKJSraTb6EudJx7TEmC-FZhYu4PTXqwTY72Y5jvGA2xPNMRm19Rfk60Dd3-pK_fxVeN4gZUJXbRBbhlULW-66A)

We can see by the partial UI loading, that we are at least receiving some form of data. Let’s connect our pieces of data as we did in the previous screen.

With a little bit of cleanup on the components we need to display our data, re-applying some text transforms, and adjusting the styles, we end up with a rough shape of the following:

![Mapping the content to the layout.](https://lh5.googleusercontent.com/z2V3pfnCx403Qua9h0YD41mQx50puCWwHDWyqz-vIo8kx3UCyw9K5_lHvjqMceCem1ZvbUR71X_vAm6fP8WK-E2O3zrzWhUSgC6KmfXOIxNN6oqESCpQ6ZZzKRJLAwchcQAuC7ft)

However, we’ve done nothing with our tags! Let’s add a way to loop over tag elements. Based on what we’ve seen so far, we can assume we’ll need some combination of another “View” box - typically as a container primitive, some form of a “List” component to iterate over an array of data, and the final form of our tag element.

Implementation details will vary, but with these primitives we were able to loop over our tags, and output them on the screen.

![Adding tags to screen.](https://lh5.googleusercontent.com/MtumDuQeDq9hhYCor4OP5oKE1v0ILmsLwfm0VoHlLVBWnSGR9Vp2scLI1w5FlHndaiHvgAOL8MsWCetJyxpAat3u0OkB4LpWaci35k2tHTVtQpM6pdibYV2EQP_NdZrGrs34YIv4)

But we’ll notice that’s a lot of tags! And tags like “meat” don’t seem relevant to our pictures of fruit! Fortunately, our auto-tagging API includes the confidence level of these tags. Let’s adjust the response data from Hasura.

Here, we are limiting our tags to only those with a confidence greater than 50.

![Clean up tags display.](https://lh4.googleusercontent.com/d3HWmO0GxhofIOeJErQDB3Yo3q1mIq7xmpB9DQWv3nw8spckLFifG8f7EnSTAqo1PE9g7mMfBm_UOx8I1rsMnZxBYEaImm01II3MJll4msf3z-xTKxSe7xDVpLBsf8tgrHS-dJCh)

Once we save that data, we can fetch again from Draftbit to see what we get.

![Preview result.](https://lh4.googleusercontent.com/kvm8imYw9z7SIkowKggotkiKBm-8k1-ZehNE3EvB9dE_cvBcf98IkbMtBTnfpi3pU1KbhA1NWDLjEWsclrTvd_OWib-YKmaJTDfZUHsQGUp3qjZ9mSZvn6UQ-sZ1EopvyMKrGYjU)

That’s a considerable improvement! It’s time to preview this app in context, and play with the different pages.

Pressing the play button gives us a scannable QR code to preview the app directly on our phones, you’ll need to download the Expo app, first.

![Load preview button for app.](https://lh3.googleusercontent.com/DQ2-KUpRkBkmt1C8aj2J4kqwXbfA3sOQBnpTotlZSsZJHh4jl8vjbJujvndphBknYvRSHLY8gSO62X-TIH6HcsBGAMXur8R4qB8y8iNfcVbTBX9kP5BvwZbScDeayUpZnSY6LbZu)

{% youtube m12phMatKZk %}

With just a few functions, some powerful features from both Draftbit and Hasura, we’ve been able to create a native app from our API with very low coding, indeed.

Publishing this app is out of scope for this tutorial, but you can follow the guides at Draftbit and use the built-in UI to deliver your app to users around the world.

![UI to publish app.](https://lh3.googleusercontent.com/dy83W95KviA28O7k5-IsT_AtCVpmmyudvGdYccPDuDb2CBtxLRp7Pn353Utcr9BGGo6BcEWRaqW7GUryleXJfUhUClK8cu5EDH8rUrGEdDg9pD4jlTQ4XFCaBhkBtEUrj8F_vN72)

Build something with [your API](https://hasura.info/dev_jesse) today and let us know what you made!