Skip to content
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

refactor useQuery to not use an internal class #11869

Merged
merged 33 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c2bd48b
extract hooks from InternalState
phryneas May 27, 2024
dc58beb
inline hooks and functions into each other, move `createWatchQueryOpt…
phryneas May 27, 2024
10466e5
move `executeQuery` to `useLazyQuery`
phryneas May 27, 2024
ea77f60
move more functions out
phryneas May 27, 2024
fde4c4d
move `unsafeHandlePartialRefetch` into `setResult`
phryneas May 28, 2024
e1b7ed7
remove `toQueryResultCache`, save transformed result in `internalStat…
phryneas May 29, 2024
9c865ca
fixup test
phryneas May 29, 2024
b5e1643
move `getDefaultFetchPolicy` out
phryneas May 29, 2024
b4c8bf9
move more functions out
phryneas May 29, 2024
12e19f9
moved all class methods out
phryneas May 29, 2024
24e4491
replace class with single mutable object
phryneas May 29, 2024
e31d953
move callbacks into their own ref
phryneas May 29, 2024
891e211
move `obsQueryFields` out of `internalState`
phryneas Jun 3, 2024
ec1c7a9
inline `useInternalState`
phryneas Jun 4, 2024
79566a8
redactor away `internalState.queryHookOptions`
phryneas Jun 4, 2024
873f24d
make function arguments more explicit
phryneas Jun 4, 2024
8bb445c
replace `internalState.watchQueryOptions` with `observable[lastWatchO…
phryneas Jun 5, 2024
635a32b
move observable fully into a readonly prop on internalState
phryneas Jun 5, 2024
d6de17f
remove all direct access to `internalState` after initializing
phryneas Jun 5, 2024
06d98ac
extract new `useInternalState` hook
phryneas Jun 5, 2024
7717b4f
extract `useResubscribeIfNecessary` hook
phryneas Jun 5, 2024
3889fff
add comment
phryneas Jun 5, 2024
da4b840
extract `bindObservableMethods`
phryneas Jun 7, 2024
f18b753
extract `useHandleSkip` and `useRegisterSSRObservable` hooks
phryneas Jun 7, 2024
8480ce6
extract useObservableSubscriptionResult hook
phryneas Jun 7, 2024
30b1769
change some method arguments. remove obsolete comment
phryneas Jun 7, 2024
c896885
curry `createMakeWatchQueryOptions`
phryneas Jun 7, 2024
1485651
bring function and hook argyuments into a common order
phryneas Jun 7, 2024
70f5aaf
Move `onQueryExecuted` into `useInternalState`
phryneas Jun 7, 2024
fed117b
some style adjustments to be more compiler-friendly
phryneas Jun 7, 2024
a69327c
remove R19 exception from test, chores
phryneas Jul 3, 2024
987aaad
Apply suggestions from code review
phryneas Jul 4, 2024
33c0fef
Merge branch 'release-3.11' into pr/rewrite-useQuery
phryneas Jul 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move getDefaultFetchPolicy out
  • Loading branch information
phryneas committed Jul 3, 2024
commit b5e16431494ff97109039cc17b359cee3fb117f9
6 changes: 5 additions & 1 deletion src/react/hooks/useLazyQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
import type { InternalState } from "./useQuery.js";
import {
createWatchQueryOptions,
getDefaultFetchPolicy,
toQueryResult,
useInternalState,
useQueryWithInternalState,
Expand Down Expand Up @@ -103,7 +104,10 @@ export function useLazyQuery<

const initialFetchPolicy =
useQueryResult.observable.options.initialFetchPolicy ||
internalState.getDefaultFetchPolicy();
getDefaultFetchPolicy(
internalState.queryHookOptions.defaultOptions,
internalState.client.defaultOptions
);

const { obsQueryFields } = internalState;
const forceUpdateState = React.useReducer((tick) => tick + 1, 0)[1];
Expand Down
33 changes: 23 additions & 10 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useSyncExternalStore } from "./useSyncExternalStore.js";
import { equal } from "@wry/equality";

import type {
DefaultOptions,
OperationVariables,
WatchQueryFetchPolicy,
} from "../../core/index.js";
Expand Down Expand Up @@ -385,7 +386,10 @@ export function createWatchQueryOptions<

if (skip) {
const {
fetchPolicy = internalState.getDefaultFetchPolicy(),
fetchPolicy = getDefaultFetchPolicy(
internalState.queryHookOptions.defaultOptions,
internalState.client.defaultOptions
),
initialFetchPolicy = fetchPolicy,
} = watchQueryOptions;

Expand All @@ -399,7 +403,10 @@ export function createWatchQueryOptions<
} else if (!watchQueryOptions.fetchPolicy) {
watchQueryOptions.fetchPolicy =
internalState.observable?.options.initialFetchPolicy ||
internalState.getDefaultFetchPolicy();
getDefaultFetchPolicy(
internalState.queryHookOptions.defaultOptions,
internalState.client.defaultOptions
);
}

return watchQueryOptions;
Expand Down Expand Up @@ -456,14 +463,6 @@ class InternalState<TData, TVariables extends OperationVariables> {
return toMerge.reduce(mergeOptions) as WatchQueryOptions<TVariables, TData>;
}

getDefaultFetchPolicy(): WatchQueryFetchPolicy {
return (
this.queryHookOptions.defaultOptions?.fetchPolicy ||
this.client.defaultOptions.watchQuery?.fetchPolicy ||
"cache-first"
);
}

// Defining these methods as no-ops on the prototype allows us to call
// state.onCompleted and/or state.onError without worrying about whether a
// callback was provided.
Expand Down Expand Up @@ -538,6 +537,20 @@ class InternalState<TData, TVariables extends OperationVariables> {
}
}

export function getDefaultFetchPolicy<
TData,
TVariables extends OperationVariables,
>(
queryHookDefaultOptions?: Partial<WatchQueryOptions<TVariables, TData>>,
clientDefaultOptions?: DefaultOptions
): WatchQueryFetchPolicy {
return (
queryHookDefaultOptions?.fetchPolicy ||
clientDefaultOptions?.watchQuery?.fetchPolicy ||
"cache-first"
);
}

function toApolloError<TData>(
result: ApolloQueryResult<TData>
): ApolloError | undefined {
Expand Down