apollo-state-sync - API Reference (Users)
    Preparing search index...

    Class InMemoryCacheSynced

    An Apollo In-Memory Cache that synchronizes its state automatically across browsing contexts (such as browser tabs, windows, or iframes).

    This class is a drop-in replacement for Apollo Client's standard InMemoryCache.

    import { InMemoryCacheSynced, stateSyncLink } from 'apollo-state-sync'
    import { terminatingLink } from './util/terminatingLink'

    const apolloClient = new ApolloClient({

    // stateSyncLink must be used, as a non-terminating link.
    ApolloLink.from([stateSyncLink, terminatingLink]),

    // use InMemoryCacheSynced as Apollo Cache.
    cache: new InMemoryCacheSynced(),

    })

    Hierarchy

    Implements

    Index
    assumeImmutableResults: true
    getMemoryInternals?: () => {
        addTypenameDocumentTransform: { cache: number }[];
        cache: { fragmentQueryDocuments: number | undefined };
        fragmentRegistry: {
            findFragmentSpreads: number | undefined;
            lookup: number | undefined;
            transform: number | undefined;
        };
        inMemoryCache: {
            executeSelectionSet: number
            | undefined;
            executeSubSelectedArray: number | undefined;
            maybeBroadcastWatch: number | undefined;
        };
    }

    This is not a stable API - it is used in development builds to expose information to the DevTools. Use at your own risk!

    This is an internal API and should not be used directly. This can be removed or changed at any time.

    makeVar: <T>(value: T) => ReactiveVar<T>
    onAfterBroadcast: (cb: () => void) => void

    Can be overridden by subclasses to delay calling the provided callback until after all broadcasts have been completed - e.g. in a cache scenario where many watchers are notified in parallel.

    policies: Policies
    stateSyncerConfig?: CacheSyncerConfigType
    • Executes multiple cache operations as a single batch, ensuring that watchers are only notified once after all operations complete. This is useful for improving performance when making multiple cache updates, as it prevents unnecessary re-renders or query refetches between individual operations.

      The batch method supports both optimistic and non-optimistic updates, and provides fine-grained control over which cache layer receives the updates and when watchers are notified.

      For usage instructions, see Interacting with cached data: cache.batch.

      Type Parameters

      • TUpdateResult

      Returns TUpdateResult

      The return value of the update function.

      cache.batch({
      update(cache) {
      cache.writeQuery({
      query: GET_TODOS,
      data: { todos: updatedTodos },
      });
      cache.evict({ id: "Todo:123" });
      },
      });
      // Optimistic update with a custom layer ID
      cache.batch({
      optimistic: "add-todo-optimistic",
      update(cache) {
      cache.modify({
      fields: {
      todos(existing = []) {
      return [...existing, newTodoRef];
      },
      },
      });
      },
      });
    • Parameters

      Returns void

    • Returns data read from the cache for a given query along with information about the cache result such as whether the result is complete and details about missing fields.

      Will return complete as true if it can fulfill the full cache result or false if not. When no data can be fulfilled from the cache, null is returned. When returnPartialData is true, non-null partial results are returned if it contains at least one field that can be fulfilled from the cache.

      Type Parameters

      Parameters

      Returns DiffResult<TData>

    • Exposes the cache's complete state, in a serializable format for later restoration.

      Parameters

      • Optionaloptimistic: boolean

      Returns NormalizedCacheObject

    • Parameters

      • update: (cache: InMemoryCache) => any
      • OptionaloptimisticId: string | null

      Returns any

    • Parameters

      Returns void

    • Parameters

      • idToRemove: string

      Returns void

    • Determines whether a @client field can be resolved by the cache. Used when LocalState does not have a local resolver that can resolve the field.

      Parameters

      • typename: string
      • fieldName: string

      Returns boolean

      Cache implementations should return true if a mechanism in the cache is expected to provide a value for the field. LocalState will set the value of the field to undefined in order for the cache to handle it.

      Cache implementations should return false to indicate that it cannot handle resolving the field (either because it doesn't have a mechanism to do so, or because the user hasn't provided enough information to resolve the field). Returning false will emit a warning and set the value of the field to null.

      A cache that doesn't implement resolvesClientField will be treated the same as returning false.

    • Replaces existing state in the cache (if any) with the values expressed by serializedState.

      Called when hydrating a cache (server side rendering, or offline storage), and also (potentially) during hot reloads.

      Parameters

      Returns this