下面是我的阿波罗角设置代码:
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: (httpLink: HttpLink) => {
return {
cache: new InMemoryCache(),
link: httpLink.create({
uri: AppSettings.API_ENDPOINT
})
}
},
deps: [HttpLink]
}
],
我想在下面使用:
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}`);
});
如何链接此链接,以便在控制台中出现错误?
答案 0 :(得分:0)
我昨天刚开始处理这个问题,所以没有很好的专家,但是我认为它确实可以满足您的要求。我在网上也找不到很好的例子。
这将触发一个对话框,在对话框中显示您想要的任何消息。我没有包含对话框代码,但是它存在于我的messagesService中。
请注意,我把InMemoryCache留在了那里,以作为诸如此类不需要的响应的注释。它包含在Boost中并自动设置。我可以使用readQuery读取它。
我使用Boost并将其放在导出类中的app.module中,因为无法在模块提供程序中使其正常工作。最初,我的设置方式与您一样,但是无法到达messagesService。它也可以是一项服务,我可以将其移到那里。这是全局的,您无需在组件中做任何事情。很好!
app.module.ts
export class AppModule {
// Create and setup the Apollo server with error catching globally.
constructor(
private messagesService: MessagesService,
private apollo: ApolloBoost,
) {
apollo.create({
uri: 'http://localhost:3000/graphql',
// cache: new InMemoryCache(), // This is included by default. Can be modified.
onError: ({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`),
console.log('This is a graphQL error!'),
);
const msg1 = 'GraphQL error';
const msg2 = 'Please contact support.';
this.handleError(msg1, msg2)
}
if (networkError) {
console.log('This is a Network Error!', networkError);
console.log('Can be called from a query error in the browser code!');
const msg1 = 'Network error';
const msg2 = 'Please check your Internet connection. If OK then contact support .';
this.handleError(msg1, msg2)
}
}
});
}
public handleError(msg1, msg2) {
this.messagesService.openDialog(msg1, msg2);
}
}
答案 1 :(得分:0)
这是我使用'apollo-link-error'处理graphQL错误和网络错误的方法。我创建了一个名为 apollo-clients-module.ts 的单独模块,并将其导入到app.module.ts和我拥有的其他功能模块中。
apollo-clients-module.ts代码:
import { NgModule } from "@angular/core";
import { HttpLink } from "apollo-angular-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { Apollo, ApolloModule } from "apollo-angular";
import { ApolloLink } from 'apollo-link';
import { onError } from "apollo-link-error";
@NgModule({
declarations: [],
imports: [ApolloModule]
})
export class ApolloClientsModule {
constructor(private apollo: Apollo, private httpLink: HttpLink) {
const link = onError(({ graphQLErrors, networkError }) => {
if(graphQLErrors){
graphQLErrors.map(({ message, locations, path }) => {
// Here you may display a message to indicate graphql error
// You may use 'sweetalert', 'ngx-toastr' or any of your preference
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
})
}
if(networkError){
// Here you may display a message to indicate network error
console.log(`[Network error]: ${networkError}`);
}
});
apollo.create(
{
link : ApolloLink.from([link, httpLink.create({ uri: "/end-point-uri-goes-here/graphql" })]) ,
cache: new InMemoryCache(),
defaultOptions : {
watchQuery : {
fetchPolicy : 'network-only',
errorPolicy : 'all'
}
}
},
"default"
);
}
}
记住'npm install apollo-link-error'
访问https://www.apollographql.com/docs/angular/features/error-handling/了解更多信息。