wobsoriano / trpc-nuxt

End-to-end typesafe APIs in Nuxt applications.
trpc-nuxt.vercel.app
MIT License
675 stars 38 forks source link

Fix `useLazyQuery` #173

Closed ClementNerma closed 3 months ago

ClementNerma commented 3 months ago

Hi there!

I'm currently writing an application using this project (which is great btw!) but I'm encountering a few problems.

One of them is that useLazyQuery seems to have some pieces missing. Notably, how do I re-trigger the same query, but with different parameters? The returned execute callback doesn't allow to do that...

wobsoriano commented 3 months ago

Hi! Have you tried passing in a ref/reactive as a param? Like:

const { $client } = useNuxtApp()
const text = ref('robert')

const { data } = await $client.hello.useLazyQuery(text)
// or
// const { data } = await $client.hello.useLazyQuery(() => text.value)

setTimeout(() => {
  text.value = 'clement' // updating the ref should rerun the query 
}, 500)

You only use the execute function when you want to run the query with same params :)