Skip to content

Commit 70f5aaf

Browse files
committed
Move onQueryExecuted into useInternalState
1 parent 1485651 commit 70f5aaf

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

src/react/hooks/useLazyQuery.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ApolloClient,
77
ApolloQueryResult,
88
OperationVariables,
9+
WatchQueryOptions,
910
} from "../../core/index.js";
1011
import { mergeOptions } from "../../utilities/index.js";
1112
import type {
@@ -16,16 +17,11 @@ import type {
1617
QueryHookOptions,
1718
QueryResult,
1819
} from "../types/types.js";
19-
import type {
20-
InternalResult,
21-
ObsQueryWithMeta,
22-
UpdateInternalState,
23-
} from "./useQuery.js";
20+
import type { InternalResult, ObsQueryWithMeta } from "./useQuery.js";
2421
import {
2522
createMakeWatchQueryOptions,
2623
getDefaultFetchPolicy,
2724
getObsQueryOptions,
28-
lastWatchOptions,
2925
toQueryResult,
3026
useQueryInternals,
3127
} from "./useQuery.js";
@@ -108,7 +104,7 @@ export function useLazyQuery<
108104
client,
109105
resultData,
110106
observable,
111-
updateInternalState,
107+
onQueryExecuted,
112108
} = useQueryInternals(document, queryHookOptions);
113109

114110
const initialFetchPolicy =
@@ -171,7 +167,7 @@ export function useLazyQuery<
171167
client,
172168
document,
173169
{ ...options, skip: false },
174-
updateInternalState
170+
onQueryExecuted
175171
).then((queryResult) => Object.assign(queryResult, eagerMethods));
176172

177173
// Because the return value of `useLazyQuery` is usually floated, we need
@@ -187,7 +183,7 @@ export function useLazyQuery<
187183
initialFetchPolicy,
188184
observable,
189185
resultData,
190-
updateInternalState,
186+
onQueryExecuted,
191187
]
192188
);
193189

@@ -211,7 +207,7 @@ function executeQuery<TData, TVariables extends OperationVariables>(
211207
options: QueryHookOptions<TData, TVariables> & {
212208
query?: DocumentNode;
213209
},
214-
updateInternalState: UpdateInternalState<TData, TVariables>
210+
onQueryExecuted: (options: WatchQueryOptions<TVariables, TData>) => void
215211
) {
216212
const query = options.query || currentQuery;
217213
const watchQueryOptions = createMakeWatchQueryOptions(
@@ -224,21 +220,7 @@ function executeQuery<TData, TVariables extends OperationVariables>(
224220
const concast = observable.reobserveAsConcast(
225221
getObsQueryOptions(observable, client, options, watchQueryOptions)
226222
);
227-
// this needs to be set to prevent an immediate `resubscribe` in the
228-
// next rerender of the `useQuery` internals
229-
observable[lastWatchOptions] = watchQueryOptions;
230-
updateInternalState({
231-
client,
232-
observable,
233-
// might be a different query
234-
query,
235-
resultData: Object.assign(resultData, {
236-
// We need to modify the previous `resultData` object as we rely on the
237-
// object reference in other places
238-
previousData: resultData.current?.data || resultData.previousData,
239-
current: undefined,
240-
}),
241-
});
223+
onQueryExecuted(watchQueryOptions);
242224

243225
return new Promise<
244226
Omit<QueryResult<TData, TVariables>, (typeof EAGER_METHODS)[number]>

src/react/hooks/useQuery.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,37 @@ function useInternalState<
222222
// triggered with the new state.
223223
const newInternalState = createInternalState(internalState);
224224
updateInternalState(newInternalState);
225-
return [newInternalState, updateInternalState] as const;
225+
return [newInternalState, onQueryExecuted] as const;
226226
}
227227

228-
return [internalState, updateInternalState] as const;
228+
return [internalState, onQueryExecuted] as const;
229+
230+
/**
231+
* Used by `useLazyQuery` when a new query is executed.
232+
* We keep this logic here since it needs to update things in unsafe
233+
* ways and here we at least can keep track of that in a single place.
234+
*/
235+
function onQueryExecuted(
236+
watchQueryOptions: WatchQueryOptions<TVariables, TData>
237+
) {
238+
// this needs to be set to prevent an immediate `resubscribe` in the
239+
// next rerender of the `useQuery` internals
240+
Object.assign(internalState.observable, {
241+
[lastWatchOptions]: watchQueryOptions,
242+
});
243+
const resultData = internalState.resultData;
244+
updateInternalState({
245+
...internalState,
246+
// might be a different query
247+
query: watchQueryOptions.query,
248+
resultData: Object.assign(resultData, {
249+
// We need to modify the previous `resultData` object as we rely on the
250+
// object reference in other places
251+
previousData: resultData.current?.data || resultData.previousData,
252+
current: undefined,
253+
}),
254+
});
255+
}
229256
}
230257

231258
export function useQueryInternals<
@@ -250,7 +277,7 @@ export function useQueryInternals<
250277
isSyncSSR
251278
);
252279

253-
const [{ observable, resultData }, updateInternalState] = useInternalState(
280+
const [{ observable, resultData }, onQueryExecuted] = useInternalState(
254281
client,
255282
query,
256283
options,
@@ -308,7 +335,7 @@ export function useQueryInternals<
308335
observable,
309336
resultData,
310337
client,
311-
updateInternalState,
338+
onQueryExecuted,
312339
};
313340
}
314341

0 commit comments

Comments
 (0)