Skip to content

Commit dfda94e

Browse files
phryneasjerelmiller
andcommitted
Apply suggestions from code review
Co-authored-by: Jerel Miller <[email protected]>
1 parent a69327c commit dfda94e

File tree

2 files changed

+31
-45
lines changed

2 files changed

+31
-45
lines changed

src/react/hooks/useLazyQuery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
toQueryResult,
2626
useQueryInternals,
2727
} from "./useQuery.js";
28+
import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js";
2829

2930
// The following methods, when called will execute the query, regardless of
3031
// whether the useLazyQuery execute function was called before.
@@ -188,7 +189,7 @@ export function useLazyQuery<
188189
);
189190

190191
const executeRef = React.useRef(execute);
191-
React.useLayoutEffect(() => {
192+
useIsomorphicLayoutEffect(() => {
192193
executeRef.current = execute;
193194
});
194195

src/react/hooks/useQuery.ts

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ function useInternalState<
193193
(renderPromises &&
194194
renderPromises.getSSRObservable(makeWatchQueryOptions())) ||
195195
client.watchQuery(
196-
getObsQueryOptions(
197-
undefined,
198-
client,
199-
options,
200-
makeWatchQueryOptions()
201-
)
196+
getObsQueryOptions(void 0, client, options, makeWatchQueryOptions())
202197
),
203198
resultData: {
204199
// Reuse previousData from previous InternalState (if any) to provide
@@ -267,7 +262,7 @@ export function useQueryInternals<
267262
const renderPromises = React.useContext(getApolloContext()).renderPromises;
268263
const isSyncSSR = !!renderPromises;
269264
const disableNetworkFetches = client.disableNetworkFetches;
270-
const ssrAllowed = !(options.ssr === false || options.skip);
265+
const ssrAllowed = options.ssr !== false && !options.skip;
271266
const partialRefetch = options.partialRefetch;
272267

273268
const makeWatchQueryOptions = createMakeWatchQueryOptions(
@@ -310,11 +305,7 @@ export function useQueryInternals<
310305
isSyncSSR
311306
);
312307

313-
useRegisterSSRObservable<TData, TVariables>(
314-
observable,
315-
renderPromises,
316-
ssrAllowed
317-
);
308+
useRegisterSSRObservable(observable, renderPromises, ssrAllowed);
318309

319310
const result = useObservableSubscriptionResult<TData, TVariables>(
320311
resultData,
@@ -349,7 +340,6 @@ function useObservableSubscriptionResult<
349340
disableNetworkFetches: boolean,
350341
partialRefetch: boolean | undefined,
351342
skipSubscribing: boolean,
352-
353343
callbacks: {
354344
onCompleted: (data: TData) => void;
355345
onError: (error: ApolloError) => void;
@@ -483,11 +473,8 @@ function useObservableSubscriptionResult<
483473
);
484474
}
485475

486-
function useRegisterSSRObservable<
487-
TData = any,
488-
TVariables extends OperationVariables = OperationVariables,
489-
>(
490-
observable: ObsQueryWithMeta<TData, TVariables>,
476+
function useRegisterSSRObservable(
477+
observable: ObsQueryWithMeta<any, any>,
491478
renderPromises: RenderPromises | undefined,
492479
ssrAllowed: boolean
493480
) {
@@ -569,32 +556,30 @@ function useResubscribeIfNecessary<
569556
options: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>,
570557
watchQueryOptions: Readonly<WatchQueryOptions<TVariables, TData>>
571558
) {
572-
{
573-
if (
574-
observable[lastWatchOptions] &&
575-
!equal(observable[lastWatchOptions], watchQueryOptions)
576-
) {
577-
// Though it might be tempting to postpone this reobserve call to the
578-
// useEffect block, we need getCurrentResult to return an appropriate
579-
// loading:true result synchronously (later within the same call to
580-
// useQuery). Since we already have this.observable here (not true for
581-
// the very first call to useQuery), we are not initiating any new
582-
// subscriptions, though it does feel less than ideal that reobserve
583-
// (potentially) kicks off a network request (for example, when the
584-
// variables have changed), which is technically a side-effect.
585-
observable.reobserve(
586-
getObsQueryOptions(observable, client, options, watchQueryOptions)
587-
);
588-
589-
// Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,
590-
// but save the current data as this.previousData, just like setResult
591-
// usually does.
592-
resultData.previousData =
593-
resultData.current?.data || resultData.previousData;
594-
resultData.current = void 0;
595-
}
596-
observable[lastWatchOptions] = watchQueryOptions;
559+
if (
560+
observable[lastWatchOptions] &&
561+
!equal(observable[lastWatchOptions], watchQueryOptions)
562+
) {
563+
// Though it might be tempting to postpone this reobserve call to the
564+
// useEffect block, we need getCurrentResult to return an appropriate
565+
// loading:true result synchronously (later within the same call to
566+
// useQuery). Since we already have this.observable here (not true for
567+
// the very first call to useQuery), we are not initiating any new
568+
// subscriptions, though it does feel less than ideal that reobserve
569+
// (potentially) kicks off a network request (for example, when the
570+
// variables have changed), which is technically a side-effect.
571+
observable.reobserve(
572+
getObsQueryOptions(observable, client, options, watchQueryOptions)
573+
);
574+
575+
// Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,
576+
// but save the current data as this.previousData, just like setResult
577+
// usually does.
578+
resultData.previousData =
579+
resultData.current?.data || resultData.previousData;
580+
resultData.current = void 0;
597581
}
582+
observable[lastWatchOptions] = watchQueryOptions;
598583
}
599584

600585
/*
@@ -724,7 +709,7 @@ function setResult<TData, TVariables extends OperationVariables>(
724709
);
725710
}
726711

727-
function handleErrorOrCompleted<TData, TVariables extends OperationVariables>(
712+
function handleErrorOrCompleted<TData>(
728713
result: ApolloQueryResult<TData>,
729714
previousResult: ApolloQueryResult<TData> | undefined,
730715
callbacks: Callbacks<TData>

0 commit comments

Comments
 (0)