Refresh in queries happens outside of application context? #395
Answered
by
posva
amandesai01
asked this question in
Questions
-
|
The code to fetch data relies on How can I tackle this? Is there a way to bind values? Code: export const useRawAPI = () => {
const apiToken = inject<string | undefined>(API_OVERRIDE_API_TOKEN, undefined);
const $api = makeOFetch(apiToken);
const useRawFetch: typeof useFetch = (arg1, arg2, ...args) => {
return useFetch(
arg1,
{
...arg2,
$fetch: $api,
},
...args
);
};
return { $api, useRawFetch };
};export const projectsQuery = defineQueryOptions(() => ({
key: ['projects'],
query: () => {
const { $api } = useRawAPI();
return $api('/api/projects');
},
staleTime: DEFAULT_STALE_TIME,
})); |
Beta Was this translation helpful? Give feedback.
Answered by
posva
Oct 7, 2025
Replies: 1 comment 4 replies
-
|
You can pass arguments to the function of defineQueryOptions: export const projectsQuery = defineQueryOptions(($api: ReturnType<typeof useRawAPI>['$api']) => ({
key: ['projects'],
query: () => {
return $api('/api/projects');
},
staleTime: DEFAULT_STALE_TIME,
}));
const { $api } = useRawAPI()
useQuery(projectsQuery, () => $api)
// or in your case, since `$api` doesn't change, you can also do
useQuery(projectsQuery($api))It would be great to document this and push a bit more defineQueryOptions and what it is in docs |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also put those two lines of code into its own composable if it’s used everywhere. If your $api is implemented using inject provide, you will have to inject it unfortunately
Query options can be called anywhere so they can’t use injections:
another option not always possible is using a global (not safe for SSR)