【发布时间】:2018-08-31 10:03:30
【问题描述】:
大家好,我有点卡在 apollo-angular 和 apollo-link-error 的问题上。我尝试了几种不同的方法,但我似乎无法在我的 Angular Web 应用程序中发现任何客户端错误。我在下面发布了我的尝试。任何建议或额外的眼睛将不胜感激。
基本上,我要做的就是在发生错误时提示我的用户有关问题。如果有人有除 apollo-link-error 之外的其他 npm 包,我会全力以赴。
尝试 1:
export class AppModule {
constructor (apollo: Apollo, httpLink: HttpLink) {
apollo.create({
link: httpLink.create({
uri: 'http://localhost:8080/graphql'
}),
cache: new InMemoryCache()
});
const error = onError(({ networkError }) => {
const networkErrorRef:HttpErrorResponse = networkError as HttpErrorResponse;
if (networkErrorRef && networkErrorRef.status === 401) {
console.log('Prompt User', error);
}
});
}
}
尝试 2:
export class AppModule {
constructor (apollo: Apollo, httpLink: HttpLink) {
apollo.create({
link: httpLink.create({
uri: 'http://localhost:8080/graphql'
}),
cache: new InMemoryCache()
});
const error = onError(({networkError}) => {
if (networkError.status === 401) {
console.log('Prompt User', error);
}
});
}
}
尝试 3:
export class AppModule {
constructor (apollo: Apollo, httpLink: HttpLink) {
apollo.create({
link: httpLink.create({
uri: 'http://localhost:8080/graphql'
}),
cache: new InMemoryCache()
});
const link = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
if (networkError) console.log(`[Network error]: ${networkError}`);
});
}
}
【问题讨论】:
标签: angular typescript apollo apollo-client