|
| 1 | +# Error handling - design proposal |
| 2 | + |
| 3 | +## Motivation |
| 4 | +ApolloClient currently does not deal with errors very gracefully. When the server returns any error, Apollo Client will discard the result and call `observer.error`. With the recent addition of error paths in the standard `formatError` function of [graphql-js](https://github.com/graphql/graphql-js/pull/561) and the [proposal to add it to the spec](https://github.com/facebook/graphql/pull/230), we have an opportunity to improve error handling and write partial results to the store when the server provides one. |
| 5 | + |
| 6 | +## Proposed feature / solution |
| 7 | +As far as Apollo Client is concerned, errors can be roughly divided into two categories: |
| 8 | + |
| 9 | +1. Errors that make the entire result unusable |
| 10 | +2. Errors that make only part of the result unusable |
| 11 | + |
| 12 | +An example for the first kind of error is when the server cannot be reached, or the server does not return valid JSON. An example for the second kind of error is when the server returned a partial result and a GraphQL error because it could not reach a backend service. |
| 13 | + |
| 14 | +For errors of the first category, the server does not return any data that can be displayed to the user, only errors, so Apollo Client should call `observer.error` and not write data to the store. |
| 15 | + |
| 16 | +For errors of the second category, the server returns data *and* errors, so there is data that can be displayed to the user and Apollo Client should call `observer.error` with `{ data, errors }`, and write as much of the result to the store as possible. Any `null` fields in `result.data` should not be written to the store if there is an error with a path corresponding to that field. |
| 17 | + |
| 18 | +Note: We call `observer.error` with the partial result instead of `observer.next` in order to make error handling stay as close as possible to the current behavior. |
| 19 | + |
| 20 | +Because initially not all servers will have support for error paths, the current behavior (discarding all data) will be used when errors without path are encountered in the result. |
| 21 | + |
| 22 | +## Implementation steps / changes needed |
| 23 | +1. Call `observer.error` and discard the result if not all errors have an error path |
| 24 | +2. Write result to store if at least partial data is available. Ignore fields if there's an error with a path to that field. |
| 25 | +3. Call `observer.next` with data and errors if there is at least a partial result. |
| 26 | + |
| 27 | +## Changes to the external API |
| 28 | +* ApolloClient will have a new configuration option to keep using the current error behavior. Initially this could be on by default to provide a non-breaking change & smooth transition. |
| 29 | +* `observer.error` will now receive data as well if is a partial result |
| 30 | +* Partial results are now written to the store in the presence of GraphQL errors. |
| 31 | + |
0 commit comments