Refactoring Queries with a Factory Function & Async Returns – Is This Approach Acceptable? #194
-
|
I’ve been refactoring some query definitions to follow DRY principles, and I’d like some feedback on the following approach: I created a factory function that dynamically generates our queries based on a table name and a key. The idea is to reduce the repetition in our query definitions since most of them only differ by these two parameters. Additionally, I’m returning an async function for the query so that we can immediately process the API response and extract the res array safely. Are there any concerns with returning an async function within defineQuery? For example, does this affect error handling or caching in any way? Here’s a code snippet : const q = (t, k) => defineQuery({
key: [k],
query: async () => {
const r = await apiClient.get(`/requests/get_labels_ids?table_name=${t}`);
return r.data?.res ?? [];
}
});
export const q1 = q('dim_1', 'get1');
export const q2 = q('dim_2', 'get2');
export const q3 = q('dim_3', 'get3');
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
The approach seems good to me 👍 |
Beta Was this translation helpful? Give feedback.
The approach seems good to me 👍