Prevent sending requests that are known to fail

In case it's still unknown if auth is set up on the server, only the initial request to detect this should be sent. Since all other requests will also just fail, they should be blocked until the auth initialization is completed.

While the access token is getting refreshed, no request will succeed since the access token is expired; thus, they should not be sent.
This commit is contained in:
schroda
2025-10-15 02:50:43 +02:00
parent 17a811cd39
commit 6636cc66b1
7 changed files with 186 additions and 64 deletions

View File

@@ -12,11 +12,12 @@ import {
ApolloClient,
ApolloClientOptions,
ApolloLink,
from,
fromPromise,
InMemoryCache,
NormalizedCacheObject,
split,
from,
fromPromise,
toPromise,
} from '@apollo/client';
import createUploadLink from 'apollo-upload-client/createUploadLink.mjs';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
@@ -210,6 +211,27 @@ export class GraphQLClient extends BaseClient<
this.wsClient.terminate();
}
protected override shouldQueueRequest(operationName: string | undefined): boolean {
const authOperations = ['GET_ABOUT', 'USER_LOGIN', 'USER_REFRESH'];
if (authOperations.includes(operationName!)) {
return false;
}
return super.shouldQueueRequest();
}
private createAuthGuardLink() {
return new ApolloLink((operation, forward) => {
const { operationName } = operation;
if (this.shouldQueueRequest(operationName)) {
return fromPromise(this.enqueueRequest(() => toPromise(forward(operation)), operationName));
}
return forward(operation);
});
}
private createErrorLink() {
return onError(({ graphQLErrors, operation, forward }) => {
if (!graphQLErrors) {
@@ -262,6 +284,7 @@ export class GraphQLClient extends BaseClient<
},
this.createWSLink(),
from([
this.createAuthGuardLink(),
this.createErrorLink(),
this.createAuthLink(),
removeTypenameLink,