Skip to content

Improve the return type of client.query#13130

Merged
phryneas merged 10 commits intorelease-4.2from
jerel/robust-query-types
Feb 6, 2026
Merged

Improve the return type of client.query#13130
phryneas merged 10 commits intorelease-4.2from
jerel/robust-query-types

Conversation

@jerelmiller
Copy link
Copy Markdown
Member

Fixes #13123

Improves the return type of client.query to be smarter about the applied errorPolicy. The data and error properties now better reflect the expected runtime value for a given errorPolicy.

This allow the removal of undefined checks or optional chaining for most uses of client.query.

Note

The ApolloClient.QueryResult type is used in several places throughout the code base, most of which do not know the underlying errorPolicy applied (e.g. refetchQueries, which might have a mix of ObservableQuery instances 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.

@jerelmiller jerelmiller requested a review from phryneas February 4, 2026 23:25
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 4, 2026

🦋 Changeset detected

Latest 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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 4, 2026

npm i https://pkg.pr.new/apollographql/apollo-client/@apollo/client@13130

commit: f055152

@apollo-librarian
Copy link
Copy Markdown

apollo-librarian bot commented Feb 4, 2026

✅ Docs preview ready

The preview is ready to be viewed. View the preview

File Changes

0 new, 1 changed, 0 removed
* (developer-tools)/react/(latest)/local-state/reactive-variables.mdx

Build ID: f243893f436e551bbb196e1c
Build Logs: View logs

URL: https://www.apollographql.com/docs/deploy-preview/f243893f436e551bbb196e1c

: // Fallback case via `undefined` for backwards compatibility. Helps with
// other APIs such as `ObservableQuery.refetch()` which we don't know the
// errorPolicy
{
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

@phryneas phryneas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment, apart from that approved

@github-actions github-actions bot added the auto-cleanup 🤖 label Feb 6, 2026
@phryneas phryneas merged commit dd12231 into release-4.2 Feb 6, 2026
43 checks passed
jerelmiller pushed a commit that referenced this pull request Feb 13, 2026
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>
phryneas added a commit that referenced this pull request Mar 4, 2026
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

client.query() return type should narrow data to non-null when errorPolicy: "none"

2 participants