Skip to main content

Resource query tutorial

A hands-on introduction to querying resources.

This tutorial explains how to create resource queries that interact with connected resources. There are two types of resource query:

  • API: Queries that use API requests to interact with API-based resources.
  • SQL: Queries that use SQL statements to interact with SQL-based resources such as databases and similar data stores.

Introduction

Queries for API resources include relevant options for making API requests. Resources for popular service integrations, (Amazon S3, Google Sheets Twilio, etc.) include a tailored set of options. This abstracts away some of the complexities of working with APIs.

Other resources, such as GraphQL-based integrations or custom APIs, include more generic options and require further knowledge of working with the API.

Prerequisites

This tutorial uses a demo REST API with sample customer data. You can use the Sample API generator to create a fully functional REST API for testing purposes that's preloaded with sample data.

Save the base URL for later, then navigate to Apps and click Create new > App.

Sample API generator

Generate a sample API to use for this tutorial. A temporary API base URL will be copied to the clipboard.

1. Add a resource query to read data

Click to open the Code tab., then click + to add a new query. Retool can interact with a REST API that is not configured as a resource so you don't need to create one first. Search for REST API in the Resource field and select it.

2. Write the query code

Update the query with the following options.

OptionValueDescription
Action typeGETPerform a GET request that retrieves data.
URLYOUR_API_URLThe URL for the sample API.

Click Save & Run to save the resource query. The results appear in the Output section below.

3. Display results in a table

With the query results available, you can now display them using a component. Right-click on the canvas, click Add component, and select the Table component.

You can reference the query output elsewhere in the app using the query's data property. This contains an object with key names that correspond to the column names. Each key contains an array of values.

Tables automatically reference the most recently created query or you can change this in the Data setting of the Inspector.

4. Edit table data

Tables are read-only by default but can be configured to allow editing. To do this, you specify which columns should be editable.

Position the cursor over the First table column, click •••, then select Make editable. Repeat this process for the Last table column. Once done, you can click to edit cells in these columns.

Although the table data is editable, there is no query to save the edits back to the database. When you make a table editable, Retool adds a Save action to the table. You configure this with an event handler that triggers a query when changes are saved.

5. Add a resource query to write data

Navigate back to the Code tab and click + to add another REST API resource query. The action you use often depends on the API. POST, PUT, and PATCH actions modify data differently. Since this query modifies existing data only, select the PATCH action.

You provide the modified data in the body of the the API request. Retool supports different types of body data, such as JSON and Form Data. Select JSON and set the value to table1.changesetArray.

Each table row corresponds to a customer object in the API. When run, this query updates each object.

6. Configure the save action

The final step is to configure the table's Save action. Select the table to display its settings in the Inspector, then select the Save action.

Event handlers trigger queries and perform actions in response to events or user interactions, such as clicking the Save button. Add an event handler and select the query you just created.

7. Refresh data on save

Now that the save action is set up, you can edit table data and then save changes. However, the table does not immediately reflect the changes. This is because the query that retrieves data from the database needs to be run again.

Queries also support event handlers and can perform actions on success or failure. Add an event handler to the write query that triggers the read query on success. Now, whenever changes are successfully saved, the read query runs again and the table reflects the latest changes.