Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@tanstack/vue-query plugin: allow passing Refs in generated option functions #1720

Open
PotatoesMaster opened this issue Feb 14, 2025 · 0 comments
Labels
feature 🚀 New feature or request

Comments

@PotatoesMaster
Copy link

PotatoesMaster commented Feb 14, 2025

Description

queryOptions() allows to pass refs (Vue reactive variables) in the queryKey property (it is typed using MaybeRefDeep, a helper type in @tanstack/vue-query). Tanstack Query automatically refetches when a ref is updated.

Considering a endpoint named "getFoo" having a query param "bar", calling the generated getFooOptions with an argument containing refs (bypassing the type check) seems to work:

const bar = ref('foo');

const { data } = useQuery(getFooOptions({
    query: {
        bar: bar as any
    }
}));

const someTrigger = () => {
   bar.value = 'bar'; // will trigger a refetch
}

Ideally the plugin generated function signature should allows this usage.

// query key creation functions are impacted, because query key may be reactive
export const getFooQueryKey = (options?: Options<GetFooData>) => createQueryKey('getFoo', options);
//                             options?: MaybeRefDeep<Options<GetFooData>>

export const getFooOptions = (options?: Options<GetFooData>) => {
//                            options?: MaybeRefDeep<Options<GetFooData>>
    return queryOptions({
        queryFn: async ({ queryKey, signal }) => {
            const { data } = await getFoo({
                ...options, // refs are passed here, but this is unwanted
                ...queryKey[0], // here are the finite values
                signal,
                throwOnError: true
            });
            return data;
        },
        queryKey: getFooQueryKey(options)
    });
};
@PotatoesMaster PotatoesMaster added the feature 🚀 New feature or request label Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🚀 New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant