Sun*_*y G 5 graphql angular apollo-angular
下面是我的apollo angular设置代码:
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: (httpLink: HttpLink) => {
return {
cache: new InMemoryCache(),
link: httpLink.create({
uri: AppSettings.API_ENDPOINT
})
}
},
deps: [HttpLink]
}
],
Run Code Online (Sandbox Code Playgroud)
我想在下面使用:
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}`);
});
Run Code Online (Sandbox Code Playgroud)
如何链接它以便我能够在控制台中收到错误?
小智 5
以下是我如何使用“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"
);
}
}
Run Code Online (Sandbox Code Playgroud)
请记住“npm install apollo-link-error”
访问https://www.apollographql.com/docs/angular/features/error-handling/了解更多信息。
我昨天才让它工作,所以没有这方面的专家,但我认为它满足了你的要求。我在网上也找不到好的例子。
这将触发一个关于您的视图的对话,其中包含您想要的任何消息。我没有包含对话框代码,但它存在于我的 messagesService 中。
请注意,我将 InMemoryCache 留在那里作为响应的注释,例如不需要它。它自动包含在 Boost 中并进行设置。我可以用 readQuery 读取它。
我选择了 Boost,并将其放在导出类的 app.module 中,因为我无法让它在模块提供程序中正常工作。最初我是像你一样设置的,但是到达 messagesService 没有成功。它也可能是一项服务,我可能会将其移至那里。这是全局的,您无需在组件中执行任何操作。很不错!
应用程序模块.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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3762 次 |
| 最近记录: |