Reusable queries

You can use the same query across multiple apps with Reusable Queries. Often, you want the same functionality across apps, but don’t want to copy paste the same query over and over again. Reusable queries help you avoid code duplication, version control queries, and share queries with your team.

Write your query

Reusable queries are written in the Query Library. To set up a reusable query, you would:

  1. Click New to create a new query. Select a resource, write a query of your choosing, and Save it.
  2. Share your query using the Share button. This makes your query visible and editable to every Editor in your team. You can also now import this query in your apps.

You can define Variables to this query by using the curly braces {{ }}. Variables are dynamic inputs to your query. You can define defaults in the Variable section of the right hand panel. When you import a query in a Retool app you can pass inputs to the variables you define here.

​​Figure 1. An example shared query

​​Figure 1. An example shared query

Tip: You can write Javascript inside the `{{ }}` curly braces

This works similarly to a regular Retool app. In this case, we parse words inside the braces as variables and display them in the Variables section on the right. You can try values here and test running the query. For example — {{ { my_start_date: moment() } }}

Import your query

Now that your query has been shared, you can import it inside any Retool app. To do this, create a new query in your app, click on the Resources dropdown, and select "Import from Query Library".

Figure 2. Choosing ‘Import from Query Library’ option in the dropdown

Figure 2. Choosing ‘Import from Query Library’ option in the dropdown

You can choose between the queries you have shared in the Query Library, and choose a version. By default, we’ll choose the latest available version for you.

Note: Each library query has a history

Every time you save a query in the library we create a new version. You’ll be able to access all previous versions of that query when you import it. It’s important to know that updates to a query in the library are not automatically propagated to your apps. However, it is as simple as opening the app and choosing the version of the query you want to use.

Figure 3. An imported query passing the value from textinput2 to the store_id parameter

Figure 3. An imported query passing the value from textinput2 to the store_id parameter

Using variables

Any words inside {{ }} curly braces in a reusable query will be parsed as variables. For example, if you write select * from users where age > {{ min_age }} and age < {{ max_age }}, Retool will detect two variables, min_age and max_age.

You can also write more complicated Javascript expressions. Consider this query: select * from users where status = {{ age > 65 ? 'retired': 'working' }}. In this example, you can pass in age as an numerical input, and we’ll evaluate it to ‘retired’ or ‘working’ based on the value.


Are changes in the library automatically propagated to my apps?

They are not, but you just have to choose the latest version in your app. We’ll be adding functionality letting you know which apps have imported which queries soon! Note that if you add or rename a variable in your new version, Retool won’t know what values to pass in, so you’ll have to make sure that your inputs are still correct.

What permissions do I need to see / edit / use library queries?

Right you you need to be an editor to be able to do this.

Updated 4 months ago

Reusable queries

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.