admin 管理员组文章数量: 1086019
I want to use the standard approach from Apollo client to automatically get a refreshed token when I face an error related to authentication. I initially wanted to use the following approach:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.map(({ message, locations, path }) => {
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
//wanted to call the Refreshtoken from here but it doesn't work due to circular dependancy
authService.refreshToken().......
});
}
if (networkError) {
notificationService.show(0, 'Network Error', networkError.message);
}
});
However, I noticed that I can't call a service that I initially developed due to circular dependancy as ApolloClient must be first instanciated....
Do you have any other idea or suggestion to proceed?
I also thought by using the interceptor like below but Graphql is not RestAPI....
import { HttpErrorResponse, HttpEvent, HttpHandlerFn, HttpRequest } from '@angular/common/http';
import { catchError, type Observable, throwError } from 'rxjs';
import { inject } from '@angular/core';
import { NotificationService } from '../../../shared/components/notification.service';
export function authInterceptor(req: HttpRequest<any>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
const notificationService = inject(NotificationService);
return next(req).pipe(
catchError((err: HttpErrorResponse) => {
console.log(err);
notificationService.show(err.status, err.error, err.message);
return throwError(() => err);
})
);
}
```
My setup:
Angular 19
Apollo client 3
本文标签: Angular Graphql JWTToken Http OnlyStack Overflow
版权声明:本文标题:Angular Graphql JWTToken Http Only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744010725a2518100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论