Change8

v4.0.0-alpha.14

Breaking Changes
📦 apollo-client
9 breaking9 features🔧 13 symbols

Summary

Apollo Client introduces a new `LocalState` system, removes several legacy APIs, and changes resolver context handling, requiring migration steps for existing applications.

⚠️ Breaking Changes

  • The `getCacheKey` function is removed from `operation.getContext()`. Use `operation.client.cache.identify(obj)` instead.
  • Removed `getLastResult`, `getLastError`, and `resetLastResults` from `ObservableQuery`.
  • The `resolvers` option was removed from `ApolloClient`; move resolver definitions into a `LocalState` instance and pass it via the `localState` option.
  • Removed local resolver APIs from `ApolloClient`: `addResolvers`, `getResolvers`, `setResolvers`, and `setLocalStateFragmentMatcher`.
  • Third‑party caches must now implement the `fragmentMatches` method handling both `InlineFragmentNode` and `FragmentDefinitionNode`.
  • Resolver functions now receive a `context` argument with shape `{ requestContext, client, phase }` instead of spreading request context and cache.
  • Support for `@client` fields is no longer built‑in; you must provide a `LocalState` instance via the `localState` option or an error is thrown.
  • The `fragmentMatcher` option was removed from `ApolloClient`; fragment matching is now performed by the cache’s `fragmentMatches` API.
  • The `saveAsLastResult` argument was dropped from `ObservableQuery.getCurrentResult`.

Migration Steps

  1. Replace any use of `operation.getContext().getCacheKey` with `operation.client.cache.identify(obj)`.
  2. If you used `ObservableQuery.getLastResult`, `getLastError`, or `resetLastResults`, remove those calls and rely on the current result APIs.
  3. Move any `resolvers` defined in the `ApolloClient` constructor into a new `LocalState` instance and pass it via the `localState` option.
  4. Remove calls to `addResolvers`, `getResolvers`, `setResolvers`, and `setLocalStateFragmentMatcher`; use the `LocalState` API instead.
  5. Implement `fragmentMatches(fragment, typename): boolean` on custom cache classes, handling both `InlineFragmentNode` and `FragmentDefinitionNode`.
  6. Update resolver signatures to accept `{ requestContext, client, phase }` and adjust code to access request data via `requestContext` and cache via `client.cache`.
  7. If your queries use `@client` fields, instantiate `LocalState` and provide it to `ApolloClient` via the `localState` option; otherwise remove `@client` fields.
  8. Remove the `fragmentMatcher` option from your `ApolloClient` configuration; ensure your cache’s `fragmentMatches` method provides the needed logic.
  9. Remove the `saveAsLastResult` argument from calls to `ObservableQuery.getCurrentResult`.

✨ New Features

  • `ObservableQuery` now retains previous `data` when emitting a loading state unless the query or variables changed.
  • Introduced `LocalState` class for managing local resolvers, including a `context` function and generic type support.
  • Resolver `context` now includes `requestContext`, `client`, and `phase` fields, providing clearer separation of concerns.
  • `@client` fields can be used when a `LocalState` instance is supplied to `ApolloClient`.
  • Cache implementations must provide a `fragmentMatches` method, enabling more accurate fragment matching.
  • Loading state emission logic was reworked: synchronous link responses omit loading state, asynchronous responses emit it; `@exports` variables affect timing.
  • Errors thrown inside resolvers now set the field value to `null` and add the error to the response’s `errors` array.
  • Remote results are dealiased before being passed as the parent object to resolvers.
  • A `context` function can be provided to customize the `requestContext` given to resolvers.

🔧 Affected Symbols

operation.getContextObservableQuery.getLastResultObservableQuery.getLastErrorObservableQuery.resetLastResultsApolloClient.resolversApolloClient.addResolversApolloClient.getResolversApolloClient.setResolversApolloClient.setLocalStateFragmentMatcherApolloCache.fragmentMatchesResolver context argumentApolloClient.fragmentMatcherObservableQuery.getCurrentResult (saveAsLastResult argument)