Dynamically query resources
Learn how to dynamically access various resources with one query.
You're viewing documentation for classic apps, which use Retool's original app editor. The AI app builder is the recommended way to build apps and where Retool focuses new development.
Using dynamic queries, you can use a single query to access multiple different resources of the same type. You can use components to enable a user to select which resource to query. Dynamic queries decrease the maintenance burden for apps that need to query various similar resources.
Some example use cases for dynamic queries include:
- An app for your support team to interact with customer data, where every customer’s data is hosted on a standalone database with unique credentials.
- An admin panel to measure the health of customers’ deploys, where every customer has their own tenant.
- A generic budget dashboard for client projects, where every client has their own database.
Dynamic queries are available in authenticated, classic apps only. This feature is not available in public links to apps or in workflows.
This guide includes instructions for creating a simple dynamic query that retrieves a set of users from one of three different REST APIs.
Find resource ID
To create a dynamic query, find the ID of the resources that you want to query.
There are three methods to retrieve the resource ID:
- Manually: You can find ID of a resource from the URL of the resource’s configuration page. The ID is the character string following
resources/. For example, if the URL ishttps://demo.retool.com/resources/b49d8ef2-a774-433f-9b5c-cadd2a60c678, the resource ID isb49d8ef2-a774-433f-9b5c-cadd2a60c678. - Via database: If you self-host Retool, you can query the underlying Retool database to fetch a list of resources.
- Via the Retool API: You can query the
/resourcesendpoint of the Retool API to fetch a list of resources, filtered by the desired resource type.
Assign ID to a selection component
Use a component to select between resource IDs. For example, use a Select, Listbox, Radio Group, or other selection input component. Create an option for each resource, with the resource ID in the value field.
Create dynamic query
To create a dynamic query, add a new query from the Code menu. Click the 𝑓 button, and add a JavaScript snippet that represents a resource ID. For example, if you are using a Select to choose which resource to query, set the Resource ID.
In the following example, the select1 component includes three different resources, with their corresponding resource IDs in the value field.
You can only query one type of resource in a given dynamic query. This example uses three REST API resources.
To change the resource type, toggle the 𝑓 button and change the resource type.
Dynamic queries and public links
Dynamic queries don't run for users who open an app through a public link. A query that selects its resource dynamically returns the error Dynamic resource queries are not allowed in public apps. when someone runs it from a public link.
When you share an app that has the restriction and contains dynamic queries, the Share dialog warns you how many queries will fail for people who use the public link.
Enhanced security checks
Admins can turn on stricter checks for dynamic queries in your organization. Navigate to Settings > Beta, and toggle on Enhanced security checks for dynamic queries.
When the setting is on:
- A dynamic query runs only if its Resource ID field is set in the saved app. Requests to run a query against a different resource are rejected when the query has no Resource ID. Queries imported from the query playground are exempt.
- Previewing a dynamic query in the app IDE requires edit permission on the resource you select.
- REST API and GraphQL queries reject URL parameters and rendered URLs that contain a
..path segment, including encoded forms such as%2E%2Eand..%2F.
These stricter checks can break existing apps. Test your apps before you turn it on.