Skip to content

Commit

Permalink
fix(package/gqty): rerender after first fetch to refresh isLoading
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Mar 27, 2024
1 parent 7198c17 commit 41b44e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
21 changes: 14 additions & 7 deletions packages/react/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ import {
import type { ReactClientOptionsWithDefaults } from './utils';

export interface ReactClientDefaults {
/**
* The value of $state.isLoading before the first fetch happens, useful
* for skipping SSR and hydrations.
*/
initialLoadingState?: boolean;
/**
* Enable/Disable by default 'React Suspense' behavior
*
Expand Down Expand Up @@ -185,6 +190,7 @@ export function createReactClient<TSchema extends BaseGeneratedSchema>(
client: GQtyClient<TSchema>,
{
defaults: {
initialLoadingState = false,
suspense = false,
transactionFetchPolicy = 'cache-first',
lazyFetchPolicy = 'network-only',
Expand All @@ -204,18 +210,19 @@ export function createReactClient<TSchema extends BaseGeneratedSchema>(
const opts: ReactClientOptionsWithDefaults = {
...options,
defaults: {
transactionFetchPolicy,
initialLoadingState,
lazyFetchPolicy,
staleWhileRevalidate,
suspense,
retry,
lazyQuerySuspense,
transactionQuerySuspense,
mutationSuspense,
preparedSuspense,
refetchAfterHydrate,
paginatedQueryFetchPolicy,
paginatedQuerySuspense,
preparedSuspense,
refetchAfterHydrate,
retry,
staleWhileRevalidate,
suspense,
transactionFetchPolicy,
transactionQuerySuspense,
},
};

Expand Down
10 changes: 8 additions & 2 deletions packages/react/src/query/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const createUseQuery = <TSchema extends BaseGeneratedSchema>(
client: GQtyClient<TSchema>,
{
defaults: {
initialLoadingState: defaultInitialLoadingState,
suspense: defaultSuspense,
staleWhileRevalidate: defaultStaleWhileRevalidate,
retry: defaultRetry,
Expand All @@ -111,7 +112,7 @@ export const createUseQuery = <TSchema extends BaseGeneratedSchema>(
fetchInBackground = false,
fetchPolicy,
cachePolicy = translateFetchPolicy(fetchPolicy ?? 'cache-first'),
initialLoadingState = false,
initialLoadingState = defaultInitialLoadingState,
suspense = defaultSuspense,
notifyOnNetworkStatusChange = !suspense,
onError,
Expand Down Expand Up @@ -332,6 +333,8 @@ export const createUseQuery = <TSchema extends BaseGeneratedSchema>(
context.hasCacheHit = false;
context.hasCacheMiss = false;
context.notifyCacheUpdate = cachePolicy !== 'default';

// Synchronous mutex release, just to be safe.
state.promise = undefined;

// Release co-fetching context, dropping reference to the last
Expand All @@ -343,7 +346,10 @@ export const createUseQuery = <TSchema extends BaseGeneratedSchema>(
renderSession.set('postFetch', true);

// Trigger a post-fetch render, keeps the error if caught.
setState(({ error }) => ({ error }));
setState(({ error }) => ({
error,
promise: undefined,
}));
}
},
[
Expand Down

0 comments on commit 41b44e5

Please sign in to comment.