-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
fix(sqllab): excessive API calls for schemas #29279
fix(sqllab): excessive API calls for schemas #29279
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @justinpark!
@@ -88,6 +87,10 @@ export function useCatalogs(options: Params) { | |||
onError?.(); | |||
}); | |||
|
|||
const resolver = useEffectEvent(() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of duplicated code. Maybe something like this?
export function useCatalogs(options: Params) {
const { dbId, onSuccess, onError } = options || {};
const [trigger] = useLazyCatalogsQuery();
const result = useCatalogsQuery(
{ dbId, forceRefresh: false },
{
skip: !dbId,
},
);
const fetchData = useCallback(
(forceRefresh: boolean) => {
trigger({ dbId, forceRefresh }).then(({ isSuccess, isError, data }) => {
if (isSuccess) {
onSuccess?.(data || EMPTY_CATALOGS, forceRefresh);
}
if (isError) {
onError?.();
}
});
},
[dbId, onError, onSuccess, trigger],
);
const refetch = useCallback(() => {
if (dbId) {
fetchData(true);
}
}, [dbId, fetchData]);
useEffect(() => {
if (dbId && !result.currentData) {
fetchData(false);
}
}, [dbId, fetchData, result.currentData]);
return {
...result,
refetch,
};
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good suggestion. One thing that including result.currentData in the dependencies array of useEffect can cause unintended fetchData calls. I'll change it to handle this within the fetchData function.
@@ -92,20 +91,24 @@ export function useSchemas(options: Params) { | |||
onError?.(); | |||
}); | |||
|
|||
const resolver = useEffectEvent(() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before.
SUMMARY
Currently, the refresh logic for schemas is being triggered excessively due to unnecessary dependencies, causing it to be called every time a tab is switched.
This commit addresses the issue by modifying the logic to execute only when a trigger is needed.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Before:
before--too-many-schemas.mov
After:
after--too-many-schemas.mov
TESTING INSTRUCTIONS
Switch the tabs between the same db selected tabs
Check the network tab and then only schemas called once
ADDITIONAL INFORMATION