or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cache-management.mdcache-storage.mdfragment-matching.mdindex.mdoptimistic-updates.mdstore-operations.md
tile.json

tessl/npm-apollo-cache-inmemory

Normalized in-memory cache implementation for Apollo Client providing GraphQL data storage and retrieval

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/apollo-cache-inmemory@1.6.x

To install, run

npx @tessl/cli install tessl/npm-apollo-cache-inmemory@1.6.0

index.mddocs/

Apollo Cache InMemory

Apollo Cache InMemory is the recommended normalized in-memory caching solution for Apollo Client 2.0. It provides a complete GraphQL data normalization system that automatically splits query results into individual objects with unique identifiers, enabling efficient data storage, retrieval, and cache manipulation without Redux dependencies.

Package Information

  • Package Name: apollo-cache-inmemory
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install apollo-cache-inmemory

Core Imports

import { InMemoryCache } from "apollo-cache-inmemory";

For CommonJS:

const { InMemoryCache } = require("apollo-cache-inmemory");

Additional imports for advanced usage:

import { 
  InMemoryCache, 
  InMemoryCacheConfig,
  defaultDataIdFromObject,
  HeuristicFragmentMatcher,
  IntrospectionFragmentMatcher
} from "apollo-cache-inmemory";

Basic Usage

import { InMemoryCache } from "apollo-cache-inmemory";
import ApolloClient from "apollo-client";
import { HttpLink } from "apollo-link-http";

// Basic cache setup
const cache = new InMemoryCache();

const client = new ApolloClient({
  link: new HttpLink({ uri: "/graphql" }),
  cache
});

// Configure cache with custom options
const configuredCache = new InMemoryCache({
  dataIdFromObject: (object) => `${object.__typename}:${object.id}`,
  fragmentMatcher: new HeuristicFragmentMatcher(),
  addTypename: true,
  resultCaching: true,
  freezeResults: false
});

Architecture

Apollo Cache InMemory is built around several key components:

  • InMemoryCache: Main cache class providing normalized data storage and retrieval
  • Normalization System: Automatic data flattening using configurable ID generation
  • Fragment Matching: Support for GraphQL fragments on unions and interfaces
  • Optimistic Updates: Transaction-based optimistic update system with rollback capability
  • Cache Layers: Multi-layer caching with dependency tracking for performance optimization
  • Direct Manipulation: Read/write API for programmatic cache access

Capabilities

Core Cache Management

Primary cache functionality for storing, retrieving, and manipulating normalized GraphQL data.

class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
  constructor(config?: InMemoryCacheConfig);
  read<T>(options: Cache.ReadOptions): T | null;
  write(write: Cache.WriteOptions): void;
  diff<T>(query: Cache.DiffOptions): Cache.DiffResult<T>;
  reset(): Promise<void>;
}

interface InMemoryCacheConfig extends ApolloReducerConfig {
  resultCaching?: boolean;
  freezeResults?: boolean;
}

Core Cache Management

Fragment Matching

GraphQL fragment matching system for unions, interfaces, and complex query structures.

class HeuristicFragmentMatcher implements FragmentMatcherInterface {
  match(idValue: IdValue, typeCondition: string, context: ReadStoreContext): boolean | 'heuristic';
}

class IntrospectionFragmentMatcher implements FragmentMatcherInterface {
  constructor(options?: { introspectionQueryResultData?: IntrospectionResultData });
  match(idValue: IdValue, typeCondition: string, context: ReadStoreContext): boolean;
}

Fragment Matching

Optimistic Updates

Transaction-based system for handling optimistic mutations with automatic rollback capabilities.

class OptimisticCacheLayer extends ObjectCache {
  constructor(optimisticId: string, parent: NormalizedCache, transaction: Transaction<NormalizedCacheObject>);
  toObject(): NormalizedCacheObject;
  get(dataId: string): StoreObject;
}

Optimistic Updates

Cache Storage Implementations

Multiple cache storage backends optimized for different use cases and performance requirements.

class ObjectCache implements NormalizedCache {
  constructor(data?: NormalizedCacheObject);
  get(dataId: string): StoreObject;
  set(dataId: string, value: StoreObject): void;
}

class DepTrackingCache implements NormalizedCache {
  constructor(data?: NormalizedCacheObject);
  get(dataId: string): StoreObject;
  set(dataId: string, value?: StoreObject): void;
}

class MapCache implements NormalizedCache {
  constructor(data?: NormalizedCacheObject);
  get(dataId: string): StoreObject;
  set(dataId: string, value: StoreObject): void;
}

Cache Storage

Store Operations

Low-level read and write operations for direct cache manipulation and query processing.

class StoreReader {
  constructor(config: StoreReaderConfig);
  readQueryFromStore<QueryType>(options: ReadQueryOptions): QueryType;
  diffQueryAgainstStore<T>(options: DiffQueryAgainstStoreOptions): Cache.DiffResult<T>;
}

class StoreWriter {
  writeQueryToStore(options: WriteQueryOptions): NormalizedCache;
  writeResultToStore(options: WriteResultOptions): void;
}

Store Operations

Core Types

interface NormalizedCache {
  get(dataId: string): StoreObject;
  set(dataId: string, value: StoreObject): void;
  delete(dataId: string): void;
  clear(): void;
  toObject(): NormalizedCacheObject;
  replace(newData: NormalizedCacheObject): void;
}

interface NormalizedCacheObject {
  [dataId: string]: StoreObject | undefined;
}

interface StoreObject {
  __typename?: string;
  [storeFieldKey: string]: StoreValue;
}

interface ApolloReducerConfig {
  dataIdFromObject?: IdGetter;
  fragmentMatcher?: FragmentMatcherInterface;
  addTypename?: boolean;
  cacheRedirects?: CacheResolverMap;
}

type IdGetter = (value: IdGetterObj) => string | null | undefined;

interface IdGetterObj extends Object {
  __typename?: string;
  id?: string;
}

// Backward compatibility aliases
type CustomResolver = CacheResolver;
type CustomResolverMap = CacheResolverMap;