question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dymanic Path Parameters

See original GitHub issue

Describe the bug I have a route that has path parameters. I’ve run the generation tool to get specific use methods for this route. In my function setup I initially don’t have the value for these parameters. Ive set the use function to lazy:true so it doesn’t trigger right away. I have a function that is triggered by the user after they input the value for these parameters. I find that I cannot pass in these values when I call refetch because the absolute path has already been interpreted to be /some/path/undefined. This is likely because the code expects the path parameter when first construct it (due to the `/some/path/${pathParam}’ that is generated as the output from the generation step. My question is this:

How can I call refetch and replace the path parameter of generated functions dynamically. It’s not clear from the documentation. I’d love to use this library cuz it’s so close to what I need

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ApacheExcommented, Aug 15, 2020

I have a scenario in where I need to make many submissions in a component, each for a different id, which is a path parameter. It’d be so nice to be able to do this imperatively in the mutate call.

if I understood your question properly it should be possible with current approach:

const { mutate } = useMutate({
  verb: 'POST',
  path: ({ id }) => `/api/v1/${id}/projects`,
});

// and then you just call.
mutate(body, { 
  pathParams: { id: 'whatever1' },
});

mutate(body, { 
  pathParams: { id: 'whatever2' },
});

mutate(body, { 
  pathParams: { id: 'whatever3' },
});

and also it should work for DELETE request since v14.4.0:

const { mutate } = useMutate({
  verb: 'DELETE',
  path: ({ id }) => `/api/v1/projects/${id}`,
});

// and then you just call.
mutate({}, { 
  pathParams: { id: 'whatever1' },
});

mutate({}, { 
  pathParams: { id: 'whatever2' },
});

mutate({}, { 
  pathParams: { id: 'whatever3' },
});
1reaction
zeorincommented, May 4, 2020

Sorry to hear about your accident!

In my case this is not an ergonomic solution. The issue I have is how do I know when to change the path parameter.

It’s not just a case of not having one at the component mount and then getting it during the lifetime of the component. Using the lazy trick works well enough for that.

But when I have a list of things that I need to update, I need to loop through them. If I need to change the path parameter, currently, I don’t know when I can do this. Even if I manage to work around it somehow, that’s really not readable code, what I’m actually intending to achieve is not apparent.

A path parameter is conceptually not any different from a query parameter. If we allow for query parameters to be set when making the mutate call, why not a path parameter? Seems like a fairly arbitrary distinction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Routes - Next.js
Dynamic Routes are pages that allow you to add custom params to your URLs. Start creating Dynamic Routes and learn more here.
Read more >
Setting URL Parameters to Dynamic Values
The URL that you specify in the View Mapping system points to a JSP, template, or HTML page. The URL may contain a...
Read more >
Dynamic Route Matching with Params | Vue Router
Vue Router uses its own path matching syntax, inspired by the one used by express , so it supports many advanced matching patterns...
Read more >
How To Pass Multiple Route Parameters in a React URL Path
Route params are parameters whose values are set dynamically in a page's URL. This allows a route to render the same component while...
Read more >
FastAPI dynamic multiple path parameters - Stack Overflow
FastAPI dynamic multiple path parameters ... looking for is a way to make a path parameter validation dependent on another path parameter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found