falcondev-oss / trpc-vue-query

Fully type-safe composables and helpers to make working with tRPC + Tanstack Query as intuitive as possible.
https://trpc-vue-query.falcondev.io
MIT License
13 stars 1 forks source link

how to use transform in useQuery? #4

Closed dimasxp closed 4 months ago

dimasxp commented 4 months ago

how to transform response before cache ?

LouisHaftmann commented 4 months ago

There currently is no built-in way to transform the response before caching. Instead, you could just create a composable that transforms the data like this:

export function useUserNames(){
  const query = useTRPC().users.list.useQuery()
  return {
    ...query,
    data: computed(() => query.data.value?.map(user => user.name))
  }
}

There also is a select query option:

useTRPC().users.list.useQuery(undefined, {
  select: (users) => users.map(user => user.name)
})
dimasxp commented 4 months ago

Got it, thanks for the good examples and this wrapper. very handy and useful 👏