Change8

@apollo/client@4.2.0

📦 apollo-clientView on GitHub →
10 features🔧 9 symbols

Summary

Apollo Client 4.2 introduces optional "classic" and "modern" method/hook signatures to improve type accuracy based on default options. This release also adds comprehensive support for automatic event-based query refetching via the new RefetchEventManager.

Migration Steps

  1. If you rely on manually specified TypeScript generics (e.g., useSuspenseQuery<MyData>(...)), be aware that this is discouraged; use TypedDocumentNode instead.
  2. If you declare a non-optional property in ApolloClient.DeclareDefaultOptions, methods/hooks will switch to modern signatures globally, which removes support for manual generic arguments.
  3. To manually switch to modern signatures without declaring defaultOptions, augment ApolloClient.TypeOverrides with { signatureStyle: "modern" }.
  4. To manually switch back to classic signatures (not recommended long-term), augment ApolloClient.TypeOverrides with { signatureStyle: "classic" }.
  5. To activate event-based refetching, create and pass a RefetchEventManager instance to the ApolloClient constructor.
  6. To opt out of event refetching for a query, use refetchOn: false or specify false for specific events in refetchOn.
  7. To opt in to event refetching per-query when default is opt-out, use refetchOn: { eventName: true }.
  8. To add custom events, register the event name and payload type via TypeScript module augmentation on RefetchEvents, and provide a source function returning an Observable to RefetchEventManager.

✨ New Features

  • Introduce "classic" and "modern" method and hook signatures. Modern signatures automatically incorporate declared defaultOptions into return types and infer types from the document node.
  • Modern signatures do not support manually passing generic type arguments.
  • Methods and hooks automatically switch to modern signatures when any non-optional property is declared in DeclareDefaultOptions.
  • Support for manually switching to modern signatures via TypeOverrides interface.
  • Improve the accuracy of client.query return type to better detect the current errorPolicy; data property is no longer nullable when errorPolicy is none.
  • Add support for automatic event-based refetching, such as window focus, managed by RefetchEventManager.
  • Provide built-in sources for event refetching: windowFocusSource and onlineSource.
  • Refetching can be configured per-query using refetchOn option, supporting boolean, false, true, or a function.
  • Support for custom events triggering refetches via RefetchEventManager and module augmentation for event types.
  • Imperative triggering of event refetches via refetchEventManager.emit.

Affected Symbols