Templates for Tanstack Query queries & mutations
Usage
- Duplicate the files in the __templates directory you need depending on your use case.
- Rename the files to match your resource name.
- Update the
resourcevariables to match your resource name. Usually find and replace works great here if you use case sensitive search (resourcethenResource) - Implement your
queryFnfunction.
Referencing the other files in the data directory is a good way to see how to use the library.
SQL queries and mutations
The dashboard often needs to query the users database directly. There are already some reusable hooks for this in data/sql. Reference the data/fdw directory for an example of how to use them.
Note that the query key of the useExecuteSqlQuery hook will be automatically filled with an md5 hash of the SQL query, unless you provide a name for the query.
Files
resources-query.ts
For queries that return a list of results.
⚠️ Should only be used for simple lists, as it does not include any pagination.
useInfiniteQueryshould be used instead.
resource-query.ts
For getting a single item based on parameter(s). id is used in this example but can be updated to whatever is needed.
resource-update-mutation.ts
For updating a resource. This can easily be adapted to work for create, delete, or any verb that is needed. You may need to modify the invalidateQueries section depending on your use case. For example removing:
queryClient.invalidateQueries(resourceKeys.resource(projectRef, id))
if you are creating a new resource.
keys.ts
Standardized keys for use with useQuery and invalidateQueries. This is not required but is a good way to keep things consistent.
Learning Resources
The blog posts in particular are very helpful for learning best practices and how to use the library effectively.