Dynamically query resources
Learn how to dynamically access various resources with one query.
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 Retool Apps only. This feature is not available in Retool 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 are on a self-hosted instance of Retool, you can query the underlying Retool database to fetch a list of resources.
- Via the Retool API: You can query the
/resources
endpoint 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.