RFC: pass variables on refetch #1119
-
Hi there, I recently run into a case where I need to pass the queries variables directly to What I was building was an autocomplete input that has to trigger a request when the user changes the input value so it can fetch the options to be displayed. The input expects an async callback or a function returning a promise which returns/fulfills the results. Until here, RQ When the autocomplete input calls that given callback for loading the options, it passes the current input value entered so you can use it in your actual request to filter the results. How could I achieve this with RQ? I thought it would be possible with This is a snippet of what I have: const fetchUsersQueryFn = async (key, queryArgs) => {
const { results } = await readUsers(queryArgs);
return results;
};
const SearchUsers = () => {
const handleLoadOptions = async (inputValue) => {
// I can't reuse here anything from RQ
const results = await fetchUsersQueryFn(null, { q: inputValue});
return results;
}
const { data } = useQuery(['users'], fetchUsersQueryFn);
return (
<Async
loadOptions={handleLoadOptions}
defaultOptions={data}
// ...
/>
)
} If we look at this on Apollo, it provides what they call lazy queries. I know lazy queries can be done in RQ just using a regular Based on this, I'd like to be able to do something similar in RQ like: const fetchUsersQueryFn = async (key, queryArgs) => {
const { results } = await readUsers(queryArgs);
return results;
};
const SearchUsers = () => {
// this is the default query with empty `q` request payload for filtering
const { data, refetch } = useQuery(["users", { q: ''} ], fetchUsersQueryFn);
const handleLoadOptions = async (inputValue) => {
// here I call `refetch` passing the same shape of vars when defining `useQuery`
// but passing through `q` request payload the value from the input
const results = await refetch({ q: inputValue });
return results;
};
return (
<Async
loadOptions={handleLoadOptions}
defaultOptions={data}
// ...
/>
);
}; I know a workaround would be lifting up the input value from that What do you think to cover this use case then? Could Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Does this not solve your issue? const fetchUsersQueryFn = async (key, queryArgs) => {
const { results } = await readUsers(queryArgs)
return results
}
const SearchUsers = () => {
const [inputValue, setInputValue] = React.useState('')
const { data } = useQuery(['users', { q: inputValue }], fetchUsersQueryFn, {
enabled: !!inputValue,
})
return (
<Async
loadOptions={setInputValue}
defaultOptions={data}
// ...
/>
)
} |
Beta Was this translation helpful? Give feedback.
Does this not solve your issue?