Skip to content

Commit

Permalink
better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Sep 13, 2023
1 parent b8c6eb6 commit c3a761d
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/react/hooks/useSuspenseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { canonicalStringify } from "../../cache/index.js";
import { skipToken } from "./constants.js";
import type { SkipToken } from "./constants.js";
import type { CacheKey } from "../cache/types.js";
import { Operation } from "subscriptions-transport-ws";

export interface UseSuspenseQueryResult<
TData = unknown,
Expand All @@ -41,7 +42,10 @@ export interface UseSuspenseQueryResult<
}

export type FetchMoreFunction<TData, TVariables extends OperationVariables> = (
fetchMoreOptions: FetchMoreQueryOptions<TVariables, TData> & {
fetchMoreOptions: Omit<
FetchMoreQueryOptions<TVariables, TData>,
"updateQuery"
> & {
updateQuery?: (
previousQueryResult: TData,
options: {
Expand Down Expand Up @@ -178,7 +182,11 @@ export function useSuspenseQuery<
): UseSuspenseQueryResult<TData | undefined, TVariables> {
const client = useApolloClient(options.client);
const suspenseCache = getSuspenseCache(client);
const watchQueryOptions = useWatchQueryOptions({ client, query, options });
const watchQueryOptions = useWatchQueryOptions<any, any>({
client,
query,
options,
});
const { fetchPolicy, variables } = watchQueryOptions;
const { queryKey = [] } = options;

Expand All @@ -189,7 +197,7 @@ export function useSuspenseQuery<
];

const queryRef = suspenseCache.getQueryRef(cacheKey, () =>
client.watchQuery(watchQueryOptions as WatchQueryOptions<any, any>)
client.watchQuery(watchQueryOptions)
);

const [promiseCache, setPromiseCache] = React.useState(
Expand Down Expand Up @@ -236,21 +244,21 @@ export function useSuspenseQuery<

const result = fetchPolicy === "standby" ? skipResult : __use(promise);

const fetchMore: FetchMoreFunction<TData | undefined, TVariables> =
React.useCallback(
(options) => {
const promise = queryRef.fetchMore(
options as WatchQueryOptions<TVariables, TData | undefined>
);
const fetchMore = React.useCallback(
((options) => {
const promise = queryRef.fetchMore(options);

setPromiseCache((previousPromiseCache) =>
new Map(previousPromiseCache).set(queryRef.key, queryRef.promise)
);
setPromiseCache((previousPromiseCache) =>
new Map(previousPromiseCache).set(queryRef.key, queryRef.promise)
);

return promise;
},
[queryRef]
);
return promise;
}) satisfies FetchMoreFunction<
unknown,
OperationVariables
> as FetchMoreFunction<TData | undefined, TVariables>,
[queryRef]
);

const refetch: RefetchFunction<TData, TVariables> = React.useCallback(
(variables) => {
Expand Down

0 comments on commit c3a761d

Please sign in to comment.