Queries

Learn more about how queries access data and how to use them queries with components.

Queries let your components read or write data from various datasources. Retool supports querying most databases, as well as any API.

Your components then connect to queries via their props - for example, you could set your Table's data to be {{query1.data}}, and the table would then show data from query1. Similarly, you could change the onClick of a Button to trigger a {{resetPassword}} function for example, which could fire an API request.

Overview

Queries that don't mutate data (eg. read-only SQL statements, or GET API requests) are automatically run on page load. Then they populate their .data field, which you can access via queryName.data. Afterwards, when their parameters change (eg. select * from users where id = {{textinput1.value}}), read-only queries are automatically re-run.

Queries that mutate data are only run when you explicitly tell them to. For example, if you have a table of users, and you have a POST request that resets the selected user's password, the POST query won't automatically run when you change rows. Instead, POST and other write requests must be run explicitly, generally via a Button. That way, we won't write data back accidentally.

When do queries run and refresh?

Queries that read data (SQL statements + GET API queries) will automatically refresh when their parameters change. For example, the following query will auto-update every time textinput.value changes:

select * from users where customer_id = {{textinput.value}}

🚧

Queries that write data don't automatically run when their parameters change

Queries that write data (SQL actions with our GUI editor + all other HTTP methods excluding GET) will not automatically refresh when their parameters change. If you want them to change, you can select the "Run query automatically when inputs change" in the dropdown at the top of the query.

But you probably don't want that - since if your query pulls in the value from a TextInput, every time you entered a keystroke in the TextInput, the query would get called once. So you would find multiple rows inserted in your database: m, ma, mar, and mary. We suggest adding a button that triggers the POST.

Trigger queries on success or failure

You'll often want to trigger more queries when a query finishes. For example, after you change something in a database table, you probably want to refresh the users query that pulls in all your users. So updateUsername should cause your users query to refresh.

To do that, add an event handler to run the users query after the updateUsername query runs successfully.

Refreshing users after updateEmail succeeds.

Response options

By default, queries show notifications in the center of the app screen on any failures. JavaScript queries also show notification messages on success. You can modify the messages, adjust the notification duration, and disable these success or failure notifications from the Response tab of the query editor.

Prevent query variable spoofing

To prevent a user from manipulating the network request to pass in arbitrary values to the prepared statement, you can enable the Prevent query variable spoofing feature. When enabled, Retool rejects network requests that refer to current_user properties (e.g., current_user.email) which don’t match the expected values for the user making the query. If you include additional logic in your expression (e.g., current_user.groups.map(group => group.name)), query spoofing prevention is not available.

Advanced options

The following options are available in the query editor's Advanced tab.

Require confirmation before running

If you don't want a query to run automatically (whether because parameters change or because you triggered it), you can require confirmation before running. Before the query is run, a modal will pop up (the message can be dynamic), asking whether you're sure you want to run the query.

Requiring a confirmation message before running the delete user query.

Run queries periodically

Retool also provides the mechanism to periodically run a query in a user specified period. You can find this under the Advanced tab. Here is an example of this.

Example of a query refreshing every 5 seconds (5000 milliseconds)

Disabling queries dynamically

On more complex pages with many queries, it is sometimes useful to stop queries from running if they are not immediately needed. For example, if you are using a tabbed container, you might only want a query to run when the user switches the active tab to the second tab. To do this, you could write the following.

Only run a query when the second tab is active.

Hide parameters from audit logs

Run queries and their parameters are logged in the audit logs. You should hide sensitive parameters, such as API keys, from your audit logs. Add sensitive parameters to the Disable logging for field on the Advanced tab in the query editor. In the audit logs, these parameter values display as --blacklisted-by-developer--.

Disable tagging for query parameter

Query throttling

If you find that Retool performance is slow, it might be because your queries are refreshing too frequently (eg. on every keystroke). To solve that, you can throttle query such that it runs less frequently. The default is 750ms.

Watch inputs

If the query uses a dynamic input--for example, using a text input component to supply an ID parameter--and is triggered manually, you can set the query to watch and update in reaction to these inputs. In the Advanced tab under Advanced options, add any inputs to watch to the Watched inputs dropdown.

Timing options

You can control when queries run using options in the Timing section of the Advanced tab in the query editor.

Query timeout

Retool automatically times out queries that run for longer than 10 seconds (10000ms). You can adjust the Timeout after duration for queries on an individual basis in the Advanced tab of the query editor. For Retool-hosted users, the maximum duration before a query times out is 120 seconds (120000ms). Self-hosted users can set the DBCONNECTOR_QUERY_TIMEOUT_MS environment variable to change the maximum timeout value.

Delay between query runs

To update the amount of time to wait between query runs, in the Advanced tab, update the Delay between runs field. This field specifies the number of milliseconds to wait in between runs of the same query and defaults to 750 milliseconds.

Delay page load

If the query runs on page load, you can set a delay by updating the Page load delay (ms) field in the Advanced tab.


What’s Next

Here's how you read and write from specific resources: