Improve the return type of client.query#13130
Conversation
🦋 Changeset detectedLatest commit: f055152 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
✅ Docs preview readyThe preview is ready to be viewed. View the preview File Changes 0 new, 1 changed, 0 removedBuild ID: f243893f436e551bbb196e1c |
| : // Fallback case via `undefined` for backwards compatibility. Helps with | ||
| // other APIs such as `ObservableQuery.refetch()` which we don't know the | ||
| // errorPolicy | ||
| { |
There was a problem hiding this comment.
I went with this fallback approach (which is used by default) since QueryResult is used by quite a few other APIs (ObservableQuery.refetch(), ApolloClient.refetchObservableQueries(), etc) which are not aware of errorPolicy`. For backwards compatibility, I added this case. In order to use the "smarter" cases above, you need to provide the error policy to this type explicitly.
phryneas
left a comment
There was a problem hiding this comment.
Minor comment, apart from that approved
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to release-4.2, this PR will be updated.⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ `release-4.2` is currently in **pre mode** so this branch has prereleases rather than normal releases. If you want to exit prereleases, run `changeset pre exit` on `release-4.2`.⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ # Releases ## @apollo/[email protected] ### Minor Changes - [#13130](#13130) [`dd12231`](dd12231) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Improve the accuracy of `client.query` return type to better detect the current `errorPolicy`. The `data` property is no longer nullable when the `errorPolicy` is `none`. This makes it possible to remove the `undefined` checks or optional chaining in most cases. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fixes #13123
Improves the return type of
client.queryto be smarter about the appliederrorPolicy. Thedataanderrorproperties now better reflect the expected runtime value for a givenerrorPolicy.This allow the removal of
undefinedchecks or optional chaining for most uses ofclient.query.Note
The
ApolloClient.QueryResulttype is used in several places throughout the code base, most of which do not know the underlyingerrorPolicyapplied (e.g.refetchQueries, which might have a mix ofObservableQueryinstances with different error policies). As such, I left a fallback case that leaves the type as-is. You need to be explicit about an error policy in order to get the smarter types.