Change8

@apollo/client@4.1.0

📦 apollo-clientView on GitHub →
9 features🐛 4 fixes🔧 15 symbols

Summary

This release introduces significant enhancements to cache interaction, including support for array watching via fragments, new options for context in `useMutation`, and expanded support for GraphQL incremental delivery specifications like `@defer` (v17 alpha 9) and `@stream`.

Migration Steps

  1. If using incremental delivery and targeting `graphql@17.0.0-alpha.9` format, import and use `GraphQL17Alpha9Handler` in your `ApolloClient` configuration.
  2. If using Apollo Router for incremental delivery, continue using `Defer20220824Handler` as Apollo Router does not yet support the newer format.
  3. If upgrading from an older format of `@stream` to a newer one, expect the timing of some incremental results to change due to differing implementations.
  4. If you relied on `@client` fields with defined `read` functions on array fields to always return non-null data when partially missing, be aware that `undefined` is now explicitly returned by `read` and processed.

✨ New Features

  • Support `headers` transport for enhanced client awareness.
  • The `context` option on the `mutate` function returned by `useMutation` can now be a callback function, allowing merging of the hook context with the mutation-specific context.
  • Support for `from: null` in `client.watchFragment` and `cache.watchFragment`, resulting in `{ data: null, dataState: "complete", complete: true }`.
  • Support for passing an array to `from` in `useFragment`, `useSuspenseFragment`, and `client.watchFragment`, returning an array of results.
  • Added a `getCurrentResult` function to the observable returned by `client.watchFragment` and `cache.watchFragment` to synchronously retrieve the current watched fragment value.
  • Added the `from` option to `readFragment`, `watchFragment`, and `updateFragment`.
  • Support for the newer incremental delivery format for the `@defer` directive implemented in `graphql@17.0.0-alpha.9` via `GraphQL17Alpha9Handler`.
  • Add support for the `@stream` directive on both the `Defer20220824Handler` and the `GraphQL17Alpha2Handler`.
  • Add an `extensions` option to `cache.write`, `cache.writeQuery`, and `client.writeQuery`, making `extensions` available in cache `merge` functions.

🐛 Bug Fixes

  • Fixed an issue where deferred payloads that returned arrays with fewer items than the original cached array would retain items from the cached array (applies to `@stream` arrays replacing cached arrays).
  • When a `read` function is defined for a `@client` field, its fallback value is no longer set to `null`; the `read` function is called with `existing: undefined` to allow default arguments to set the returned value.
  • If no `read` function is defined nor a resolver for a `@client` field, a warning is emitted and the value is set to `null` only in that instance.
  • `InMemoryCache` no longer filters out explicitly returned `undefined` items from `read` functions for array fields, enabling partial data reads that trigger fetches for the full list.

Affected Symbols