Skip to content

Commit

Permalink
Safer refetch (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Jun 3, 2024
1 parent 715be70 commit 56813ee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-clocks-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@soundxyz/graphql-react-query': minor
---

Underlying refetch is not called if query is not enabled or variables is false
27 changes: 25 additions & 2 deletions packages/graphql-react-query/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,14 @@ export function GraphQLReactQueryClient<
) {
const queryKey =
filterQueryKey !== undefined ? [query, variables, filterQueryKey] : [query, variables];

const isEnabled = enabled && variables !== false;

const result = useQueryReactQuery<ExecutionResultWithData<ResultOf<Doc>>, Error, QueryData>({
queryFn: fetchOptions ? queryFnWithFetchOptions(fetchOptions) : defaultQueryFn,
queryKey,
...options,
enabled: enabled && variables !== false,
enabled: isEnabled,
staleTime: getTimeProp(staleTime),
cacheTime: cacheTime != null ? getTimeProp(cacheTime) : undefined,
});
Expand Down Expand Up @@ -604,11 +607,18 @@ export function GraphQLReactQueryClient<
},
);

const refetch = useStableCallback((...params: Parameters<typeof result.refetch>) => {
if (!isEnabled) return;

return result.refetch(...params);
});

return {
...result,
isLoading: result.isInitialLoading,
setQueryData: setQueryDataCallback,
queryKey,
refetch,
};
}

Expand Down Expand Up @@ -874,6 +884,8 @@ export function GraphQLReactQueryClient<
null,
);

const isEnabled = enabled && !!variables;

const result = useInfiniteReactQuery({
staleTime: getTimeProp(staleTime),
cacheTime: cacheTime != null ? getTimeProp(cacheTime) : undefined,
Expand Down Expand Up @@ -902,7 +914,7 @@ export function GraphQLReactQueryClient<
throw Error(`Missing variables required to execute query!`);
},
...options,
enabled: enabled && !!variables,
enabled: isEnabled,
});

const {
Expand All @@ -916,12 +928,16 @@ export function GraphQLReactQueryClient<
} = result;

const loadMoreNextPage = useStableCallback(() => {
if (!isEnabled) return;

if (hasNextPage && !isFetchingNextPage) return fetchNextPage();

return null;
});

const loadMorePreviousPage = useStableCallback(() => {
if (!isEnabled) return;

if (hasPreviousPage && !isFetchingPreviousPage) return fetchPreviousPage();

return null;
Expand Down Expand Up @@ -981,6 +997,12 @@ export function GraphQLReactQueryClient<
return Object.values(values);
}, [stableOrderType, data, entityStoreNodesSnapshot, customPages, filter]);

const refetch = useStableCallback((...params: Parameters<typeof result.refetch>) => {
if (!isEnabled) return;

return result.refetch(...params);
});

return {
...result,
isLoadingNewPage:
Expand All @@ -995,6 +1017,7 @@ export function GraphQLReactQueryClient<
setInfiniteQueryData: setInfiniteQueryDataCallback,
latestData,
queryKey,
refetch,
};
}

Expand Down

0 comments on commit 56813ee

Please sign in to comment.