kamilkisiela / apollo-angular

A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
https://apollo-angular.com
MIT License
1.5k stars 309 forks source link

For subscriptions, the errorPolicy setting does nothing. #2062

Closed marfrede closed 1 year ago

marfrede commented 1 year ago

Describe the bug

In the case of running an Apollo subscription with a query that results in server-side errors while still returning data, regardless of the errorPolicy setting chosen, the stream always errors out with ApolloError and the expected data cannot be read. This behaviour is expected for errorPolicy: none but neither for errorPolicy: all nor for errorPolicy: ignore.

This makes it impossible to read any data when the response message contains errors. Also this behaviour unexpectedly differs from apollo queries as it only appears for apollo subscriptions and not for apollo queries, which behave as expected.

To Reproduce

Steps to reproduce the behavior:

Run the same Apollo subscription with a query for which the server responds with errors three times, each with a different errorPolicy setting:

apollo.subscribe<SomeSubscriptionPayload>({
  query: AnyQuery,
  variables: { someId },
  errorPolicy: 'all'
}).pipe(tap(console.log), catchError((err) => {console.log(err); return of(undefined)}));

apollo.subscribe<SomeSubscriptionPayload>({
  query: AnyQuery,
  variables: { someId },
  errorPolicy: 'ignore'
}).pipe(tap(console.log), catchError((err) => {console.log(err); return of(undefined)}));

apollo.subscribe<SomeSubscriptionPayload>({
  query: AnyQuery,
  variables: { someId },
  errorPolicy: 'none'
}).pipe(tap(console.log), catchError((err) => {console.log(err); return of(undefined)}));

Now observe that in neither case the response is logged (tap is never reached) but in every case the error can be caught, meaning the stream errors out.

Expected behavior

As expected and as stated in the doc, the response of an Apollo Query differs depending on the errorPolicy setting:

  1. For errorPolicy: none the stream errors out, providing an error message, but no data.
  2. For errorPolicy: ignore the stream nexts, while providing data object but no errors array, and completes
  3. For errorPolicy: all the stream nexts, while providing data and errors array, and completes

Exactly this behaviour is expected for Apollo Subscriptions. Instead, however, the following behaviour is seen here:

  1. For errorPolicy: none the stream errors out, providing an error message, but no data.
  2. For errorPolicy: ignore the stream errors out, providing an error message, but no data.
  3. For errorPolicy: all the stream errors out, providing an error message, but no data.

Any change of the errorPolicy does nothing to the response.

Environment

- @angular/cli@16.1.7
- @angular/core@16.1.8
- @apollo/client@3.7.17
- apollo-angular@5.0.0
- graphql@16.6.0
- typescript@5.1.6
PowerKiKi commented 1 year ago

Can you create a reproduction case that proves that @apollo/client works as expected, but apollo-angular fails ?

marfrede commented 1 year ago

@PowerKiKi I will look into this.

marfrede commented 1 year ago

@PowerKiKi I was actually able to reproduce the opposite. The same error also exists when using @apollo/client with react.

PowerKiKi commented 1 year ago

Thanks for investigating that. It's as I expected. I'll close this issue and we can follow the development on the pre-existing one.

marfrede commented 1 year ago

For anyone with the same problem the new issue can be tracked here: https://github.com/apollographql/apollo-client/issues/10559 It should be merged any moment now 😊🎉

jerelmiller commented 1 year ago

Hey all 👋

Apollo Client maintainer here. Just wanted to let you know that this issue is fixed and has been released in @apollo/client v3.8.2. Thanks!