Hi everyone I am a bit stuck on a problem with apollo-angular and apollo-link-error. I've tried a few different ways and I can't seem to catch any errors client-side in my angular web app. I posted my attempts below. Any suggestions or an extra set of eyes would be much appreciated.
Basically all I am trying to do is when an error occurs to prompt my user about the problem. If anyone has some alternate npm package other than apollo-link-error I am all ears.
Attempt 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);
}
});
}
}
Attempt 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);
}
});
}
}
Attempt 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}`);
});
}
}