Table of Contents
Method 2: Create a relationship
Summarize
Home Web Front-end CSS Tutorial Creating Arrays in Hasura

Creating Arrays in Hasura

Mar 31, 2025 pm 02:12 PM

Creating Arrays in Hasura

Hasura is one of my favorite tools for building application hosting GraphQL APIs, it is simple to use and suitable for a variety of use cases. However, since using Hasura I have encountered the same problem repeatedly: How to create an array? Since arrays and mappings are not provided in the drop-down menu for row types, what is the best way to do it?

In fact, Hasura can implement the concept of arrays in several different ways, and the following is a breakdown of several methods. My personal favorite is the last method, JSONB, but we'll go through the other methods step by step in case you want to try the others as each has slightly different advantages.

Method 1: Create a literal array manually

Hasura does not provide "array" as type, but we can create a string array by selecting "text" and adding square brackets at the end, as follows:

We will get text[] and will prompt us to create the array in one of two ways:

 <code>["one", "two"] // 或{"one", "two"}</code>
Copy after login

Method 2: Create a relationship

We can also create relationships with another table containing a series of text elements. To do this, create a line with text type.

We will also create a new table via the Add Table button in the sidebar and create a very simple row with unique keys for the type we need (text, integer, or any necessary data).

Now, click on the Relationships tab. In the table, select the Array Relationship option, name it, and refer to the original table you just created.

The ID of the first table should be related to the ID of the second table we just created.

After saving, you should see the reflected array relationship in the table on the same relation tab, with the arrow indicating the direction of the relationship.

 <code>users.gameId → favoriteGames.id</code>
Copy after login

Now we can find the array in the " GraphiQL " tab:

Method 3: JSONB

Here is one of my favorite ways to create arrays in Hasura. I like it because it performs very well. We can use the "JSONB" type and select JSONB from the drop-down menu to create an array. Then we will receive a prompt like the text[] option:

After filling in, it will look like this:

From there, not only can we add arrays to your query like above, we can also search by tag through multiple indexes, and their GraphiQL tabs make exploration easy. See how I filter tags by content containing "animation":

 <code>query MyQuery { codesamples(where: {tags: {_contains: "animation"}}) { userId name id } }</code>
Copy after login

However, such searches are not necessarily efficient when used out of the box, especially when the data volume is large. Fortunately, we can define the fields to index. From the Data tab at the top, select the SQL group in the side panel, where we can define the fields to index to maintain efficient searches. In this example, we will create an index for the "tags" field:

 <code>create index codesample_gin on codesamples using gin(tags)</code>
Copy after login

This will run the query and index this lookup to make it more efficient in the future.

Summarize

Hasura provides an excellent developer experience to set up a fast and easy-to-use GraphQL API. For those interested in setting up arrays using Hasura, I hope this article helps you.

Thanks to Adron Hall of Hasura for proofreading this article.

The above is the detailed content of Creating Arrays in Hasura. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there&#039;s a bit of a learning curve, but Grid is

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

See all articles