我喜欢 apollo-angular 文档中建议的全局设置。我不确定是否将 errorLink 放入选项中,或者是否应将其与 httpLink 分组。
最大的问题是:如何在我的代码中使用它?我在任何地方都找不到示例,也不知道如何开始。我的脑海中还没有 apollo-link-error 的概念。
app.module.ts
...
import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { onError } from 'apollo-link-error';
// This is just a copy and past from the docs at this time.
const errorLink = 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}`);
});
@NgModule({
imports: [
...
],
declarations: [
...
],
providers: [
...
{ provide: APOLLO_OPTIONS,
useFactory: (httpLink: HttpLink) => {
return {
cache: new InMemoryCache(),
link: httpLink.create({
uri: 'http://localhost:3000/graphql',
}),
options: {
errorLink
},
defaultOptions: {
}
};
},
deps: [HttpLink]
},
],
})
export class AppModule { }
带有查询的组件:
this.apollo
.watchQuery({
query: getAllMembers,
})
.valueChanges
.subscribe(result => {
if (result !== null) {
this.dataSource.data = result.data['getMembers'];
} else {
// What should be here??? This doesn't seem to work.
console.log('errors ', result.errors);
}
});