I like this suggested global setup from the apollo-angular docs. I'm not sure about putting errorLink in options or if it should be grouped with httpLink.
The big question is: How do I use this in my code? I can't find examples anywhere and I don't know how to start. I don't have the concept of apollo-link-error in my head yet.
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 { }
A component with a query:
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);
}
});