Dynamic URL parameters in wasp routes
How can I read dynamic URL route parameters in wasp operations (queries/mutations)
6 Replies
Hi @Julius_Java#00. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Wohooo @Julius_Java#00, you just became a Waspeteer level 1!
----------------------
Reading Dynamic URL Parameters in Wasp Operations
In Wasp, you can define dynamic URL parameters in your routes and then access them in your operations (queries/actions). Here's how to do it:
1. Define a Route with Parameters
First, define a route with dynamic parameters in your
main.wasp
file:
The :id
part in the path indicates a dynamic parameter.
2. Accessing Parameters in Operations
When you want to access these parameters in your operations, you need to pass them as arguments when calling the query or action.
In Client Components
When calling a query from a client component, you can pass the URL parameters:
In Server Operations
On the server side, your query implementation will receive these parameters in the args
object:
Type-Safe Links
Wasp also provides a type-safe Link
component that helps with dynamic parameters:
This ensures that all required parameters are correctly passed Type-Safe Links.
You can also use the routes
object for programmatic navigation:
This approach gives you type safety when working with route parameters Type-Safe Links - routes Object.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @Julius_Java#00, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@Julius_Java#00 is there any specific constraint that forces you to have a dynamic url param instead of just doing it through the regular wasp mechanism?
for example, instead of
/api/tasks/:id
, you'd do:
this will have the same effective result, and you take advantage of our Wasp magic@Carlos Noted, thanks so much for your help!
Happy to help! Btw I forgot to link to the docs, here's the info on Queries: https://wasp.sh/docs/data-model/operations/queries
Queries | Wasp
We'll explain what Queries are and how to use them. If you're looking for a detailed API specification, skip ahead to the API Reference.