Use gql for "fetchMore" workaround

The "loading" and "networkStatus" state do not get correctly updated when e.g. calling "fetchMore".
This seems to happen because an abort signal is passed on every hook call.
Due to passing this signal, the passed args to the hook change, and thus, it looks like apollo considers this a completely new hook call.
By preventing the signal from being passed, the hook correctly updates these states, but, on the contrary, it is not possible to abort these requests anymore
This commit is contained in:
schroda
2023-10-02 16:58:32 +02:00
parent aad87463f3
commit e2f34f1f47
2 changed files with 42 additions and 10 deletions

View File

@@ -14,11 +14,11 @@ import {
ApolloQueryResult, ApolloQueryResult,
DocumentNode, DocumentNode,
FetchResult, FetchResult,
MutationHookOptions, MutationHookOptions as ApolloMutationHookOptions,
MutationOptions, MutationOptions as ApolloMutationOptions,
MutationTuple, MutationTuple,
QueryHookOptions, QueryHookOptions as ApolloQueryHookOptions,
QueryOptions, QueryOptions as ApolloQueryOptions,
QueryResult, QueryResult,
TypedDocumentNode, TypedDocumentNode,
useMutation, useMutation,
@@ -217,10 +217,41 @@ type SWRInfiniteResponseLoadInfo = {
isLoadMore: boolean; isLoadMore: boolean;
}; };
type ApolloPaginatedMutationOptions<Data = any, Variables = OperationVariables> = MutationHookOptions< type CustomApolloOptions = {
/**
* This is a workaround for an apollo bug (?).
*
* A new abort signal gets passed on every hook call.
* This causes the passed arguments to change (due to updating the "context" option, which is only relevant for the actual request),
* which - I assume - results in apollo to handle this as a completely new hook call.
* Due to this, when e.g. calling "fetchMore", "loading" and "networkStatus" do not get updated when enabling "notifyOnNetworkStatusChange".
*
* By not passing an abort signal, the states get correctly updated, BUT it won't be possible to abort the request.
*/
omitAbortSignal?: boolean;
};
type QueryOptions<Variables extends OperationVariables = OperationVariables, Data = any> = ApolloQueryOptions<
Variables,
Data
> &
CustomApolloOptions;
type QueryHookOptions<Data = any, Variables extends OperationVariables = OperationVariables> = ApolloQueryHookOptions<
Data, Data,
Variables Variables
> & { skipRequest?: boolean }; > &
CustomApolloOptions;
type MutationHookOptions<Data = any, Variables extends OperationVariables = OperationVariables> = Partial<
ApolloMutationHookOptions<Data, Variables>
> &
CustomApolloOptions;
type MutationOptions<Data = any, Variables extends OperationVariables = OperationVariables> = Partial<
ApolloMutationOptions<Data, Variables>
> &
CustomApolloOptions;
type ApolloPaginatedMutationOptions<
Data = any,
Variables extends OperationVariables = OperationVariables,
> = MutationHookOptions<Data, Variables> & { skipRequest?: boolean };
type AbortableRequest = { abortRequest: AbortController['abort'] }; type AbortableRequest = { abortRequest: AbortController['abort'] };
export type AbortableAxiosResponse<Data = any> = { response: Promise<Data> } & AbortableRequest; export type AbortableAxiosResponse<Data = any> = { response: Promise<Data> } & AbortableRequest;
@@ -730,7 +761,7 @@ export class RequestManager {
context: { context: {
...options?.context, ...options?.context,
fetchOptions: { fetchOptions: {
signal, signal: options?.omitAbortSignal ? undefined : signal,
...options?.context?.fetchOptions, ...options?.context?.fetchOptions,
}, },
}, },
@@ -746,7 +777,7 @@ export class RequestManager {
context: { context: {
...options?.context, ...options?.context,
fetchOptions: { fetchOptions: {
signal, signal: options?.omitAbortSignal ? undefined : signal,
...options?.context?.fetchOptions, ...options?.context?.fetchOptions,
}, },
}, },
@@ -762,7 +793,7 @@ export class RequestManager {
context: { context: {
...options?.context, ...options?.context,
fetchOptions: { fetchOptions: {
signal, signal: options?.omitAbortSignal ? undefined : signal,
...options?.context?.fetchOptions, ...options?.context?.fetchOptions,
}, },
}, },
@@ -778,7 +809,7 @@ export class RequestManager {
context: { context: {
...options?.context, ...options?.context,
fetchOptions: { fetchOptions: {
signal, signal: options?.omitAbortSignal ? undefined : signal,
...options?.context?.fetchOptions, ...options?.context?.fetchOptions,
}, },
}, },

View File

@@ -113,6 +113,7 @@ const Updates: React.FC = () => {
} = requestManager.useGetRecentlyUpdatedChapters(undefined, { } = requestManager.useGetRecentlyUpdatedChapters(undefined, {
fetchPolicy: 'cache-and-network', fetchPolicy: 'cache-and-network',
notifyOnNetworkStatusChange: true, notifyOnNetworkStatusChange: true,
omitAbortSignal: true,
}); });
const hasNextPage = !!chapterUpdateData?.chapters.pageInfo.hasNextPage; const hasNextPage = !!chapterUpdateData?.chapters.pageInfo.hasNextPage;
const endCursor = chapterUpdateData?.chapters.pageInfo.endCursor; const endCursor = chapterUpdateData?.chapters.pageInfo.endCursor;