T
TanStack3mo ago
useful-bronze

Using vue query outside of setup functions

Does anybody have a working example of how to use vue query composable outside of the vue component? For example inside an pinia action. I can't find any example of this in the docs or online. Thanks!
2 Replies
quickest-silver
quickest-silver3mo ago
queryClient.fetchQuery(queryOptions)
deep-jade
deep-jade3mo ago
You can use something like the following composable:
export function useEntity() {

function fetchEntity(entityId: MaybeRefOrGetter<string>) {
return useQuery({
queryKey: ['entity', { entityId }],
queryFn: () => yourQueryOrApi({ id: toValue(entityId) }),
enabled: computed(() => !!toValue(entityId)),
})
}

return {
fetchEntity
}
}
export function useEntity() {

function fetchEntity(entityId: MaybeRefOrGetter<string>) {
return useQuery({
queryKey: ['entity', { entityId }],
queryFn: () => yourQueryOrApi({ id: toValue(entityId) }),
enabled: computed(() => !!toValue(entityId)),
})
}

return {
fetchEntity
}
}

Did you find this page helpful?