T
TanStack•3y ago
harsh-harlequin

What is the best way to handle dependent queries. Will the `useQuery`?

user.value must be a string before the query can run. target accepts a string to build the query string however typescript is complaining because it thinks it could be null there
No description
3 Replies
harsh-harlequin
harsh-harlequinOP•3y ago
export const useParticipantsTableQuery = (user: Ref<string | null>, config: ParticipantTableConfig) => {
const enabled = computed(() => !!user.value);
return useQuery(
['participants', config, user],
async () => {
const target = config.all
? participantClient.getParticipantsForUser
: participantClient.getPrimaryParticipantsForUser;

return target(user.value as string, {
page: {
number: config.page,
size: config.itemsPerPage,
},
sort: getSortString(config),
filter: buildFilters(config),
});
},
{ placeholderData: { data: [], meta: { total: 0 } }, keepPreviousData: true, enabled }
);
};
export const useParticipantsTableQuery = (user: Ref<string | null>, config: ParticipantTableConfig) => {
const enabled = computed(() => !!user.value);
return useQuery(
['participants', config, user],
async () => {
const target = config.all
? participantClient.getParticipantsForUser
: participantClient.getPrimaryParticipantsForUser;

return target(user.value as string, {
page: {
number: config.page,
size: config.itemsPerPage,
},
sort: getSortString(config),
filter: buildFilters(config),
});
},
{ placeholderData: { data: [], meta: { total: 0 } }, keepPreviousData: true, enabled }
);
};
Am I correct in my understanding that I can safely cast this to a string as it will never run unless it is a string?
conscious-sapphire
conscious-sapphire•3y ago
Yes
harsh-harlequin
harsh-harlequinOP•3y ago
Thanks 🙂

Did you find this page helpful?