-
Hi everyone. This's my first day working with TanStack Query. Just wondering how much it's type system can provide. I'm thinking about this very simple example: export async function me(): Promise<{ user: User }> {
return await rest.fetch(AuthContracts.Me, {})
}
export async function login(email: string, password: string): Promise<{ user: User }> {
return await rest.fetch(AuthContracts.Login, { body: { email, password } })
}
export function useMe() {
return useQuery({
queryKey: ['auth', 'me'],
queryFn: me,
})
}
export function useLogin() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ email, password }: { email: string; password: string }) => login(email, password),
onSuccess: async data => {
queryClient.setQueryData(['auth', 'me'], data)
},
})
} I can see that I think for situations like this people would usually do type augmentation (like what fastify encourages us), but I'm not sure whether TanStack Query also works this way. I've seen some type augmentation examples here but it seems only providing limited features? Also, correct me if I'm wrong, it's a common practice to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Just came across this thread, so it seems that currently the best we can do is const meOptions = queryOptions({
queryKey: ['auth', 'me'],
queryFn: me,
})
export function useMe() {
return useQuery(meOptions)
}
export function useLogin() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ email, password }: { email: string; password: string }) => login(email, password),
onSuccess: async data => {
queryClient.setQueryData(meOptions.queryKey, data)
},
})
} right? It looks good to me, but requires a bit of research to know... |
Beta Was this translation helpful? Give feedback.
Just came across this thread, so it seems that currently the best we can do is
right? It looks good to me, but requires a bit of research to know...