T
TanStack3y ago
like-gold

Query Key Factory type not inferred for old data in queryClient.setQueryData

So I am playing around with the query key factory and looking for patterns to establish that I will use, and the first "roadblock" I hit is with queryClient.setQueryData.
const useFollowUser = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: ({ walletAddress }: PutFollowUser["req"]) => {
const userApi = new UserAPI();

return userApi.followUser({ walletAddress });
},
onSuccess: (_, { walletAddress }) => {
// oldFollowers does not have its type inferred as string[]
queryClient.setQueryData(queries.user.followers().queryKey, (oldFollowers) => [...oldFollowers, walletAddress]);
}
});
};

const user = createQueryKeys("user", {
followers: () => ({
queryKey: ["user", "followers"],
queryFn: () => {
const userApi = new UserAPI();

return userApi.getFollowers(); // this has the return type of string[]
}
}),
});
const useFollowUser = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: ({ walletAddress }: PutFollowUser["req"]) => {
const userApi = new UserAPI();

return userApi.followUser({ walletAddress });
},
onSuccess: (_, { walletAddress }) => {
// oldFollowers does not have its type inferred as string[]
queryClient.setQueryData(queries.user.followers().queryKey, (oldFollowers) => [...oldFollowers, walletAddress]);
}
});
};

const user = createQueryKeys("user", {
followers: () => ({
queryKey: ["user", "followers"],
queryFn: () => {
const userApi = new UserAPI();

return userApi.getFollowers(); // this has the return type of string[]
}
}),
});
If I decide to give it string[] myself, I get a typescript error. If I dont give it a type, I also get a typescript error on the spread
No description
No description
3 Replies
rich-copper
rich-copper3y ago
setQueryData<string[]>
like-gold
like-goldOP3y ago
cant it somehow be inferred or connected to the query factory? i thought that was one of the problems this package was supposed to solve
No description
like-gold
like-goldOP3y ago
because now, to be able to satisfy the setQueryData, I also need to import all of my types for my requests on all of my queries here its a simple string[] but i have more complex types on other queries

Did you find this page helpful?