Normalized in-memory cache implementation for Apollo Client providing GraphQL data storage and retrieval
npx @tessl/cli install tessl/npm-apollo-cache-inmemory@1.6.0Apollo 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.
npm install apollo-cache-inmemoryimport { 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";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
});Apollo Cache InMemory is built around several key components:
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;
}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;
}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;
}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;
}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;
}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;