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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user