Skip to content
Source
Sign inSign up
TanStack/query

Allow option to transform the response body #17

ClosedLam9090(GitHub profile, opens in a new tab) opened this issue 3 comments

In some case ,the API server will give a internal code in the response body like:

{
   code:"200",
   data:{},
   msg:"xxx"
}

It's helpful if we can transform the response body into somthing we want.

useQuery('key', queryFn , {
   transform:res=>{
       if(res.code==="200")return res.data       
   }
})
  1. You can do this in your query function, no?

    Tanner Linsley On Nov 6, 2019, 9:50 PM -0700, ltm <notifications@github.com>, wrote:

    In some case ,the API server will give a internal code in the response body like: { code:"200", data:{}, msg:"xxx" } It's helpful if we can transform the response body into somthing we want. useQuery('key', queryFn , { transform:res=>{ if(res.code==="200")return res.data } }) — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

  2. I think unless a very strong use-case for needing this as opposed to doing something in your query function is made, we'll hold off on this.

    It's also important to remember that because the query ID is derived from the string and variables, the query function does not need to be stable or memoized. This means you can do inline functions as much as your heart desires:

    useQuery('key', () => queryFn().then(res => res.code === '200' ? res.data : null))
  3. tannerlinsley closed this

  4. I think unless a very strong use-case for needing this as opposed to doing something in your query function is made, we'll hold off on this.

    It's also important to remember that because the query ID is derived from the string and variables, the query function does not need to be stable or memoized. This means you can do inline functions as much as your heart desires:

    useQuery('key', () => queryFn().then(res => res.code === '200' ? res.data : null))

    Thx for the answer.

Sign in to join this conversation.