apollo-cache-inmemory

raw JSON →
1.6.6 verified Sat Apr 25 auth: no javascript deprecated

apollo-cache-inmemory is the recommended cache implementation for Apollo Client 2.x, providing a normalized data store without Redux dependency. Version 1.6.6 is the final release; this package is deprecated in favor of @apollo/client which bundles InMemoryCache starting from Apollo Client 3.0. The cache uses a normalized data store with configurable dataIdFromObject, supports readQuery, readFragment, writeQuery, writeFragment for direct cache access, and includes a heuristic fragment matcher with optional introspection support for unions/interfaces. Releases adhere to Apollo Client's schedule (monthly patches, minor releases less frequent). Key differentiator: seamless integration with Apollo Client without external state management.

error Error: You are using the simple (heuristic) fragment matcher, but your queries contain union or interface types. Apollo Client cannot match fragments on unions/interfaces without an introspection fragment matcher.
cause Default heuristic fragment matcher cannot handle unions or interfaces.
fix
Pass an IntrospectionFragmentMatcher created from your schema's introspection data to InMemoryCache: new InMemoryCache({ fragmentMatcher: new IntrospectionFragmentMatcher({ introspectionQueryResultData }) }).
error TypeError: Cannot read property 'id' of undefined
cause Cache lookup using readFragment or readQuery with an object reference that doesn't exist in the cache.
fix
Ensure the data has been written to the cache first (e.g., after a query or writeQuery). Check that the fragment or query matches the cache shape exactly.
error Error: Could not find result for store key: ...
cause Accessing a cache entry that does not exist or has been evicted.
fix
Verify the cache key format (default: typename:id) and ensure the data is present. Avoid evicting needed data.
error Invariant Violation: You must pass a graphql document to the query option of ...
cause Passing a string instead of a parsed GraphQL document (gql) to cache methods.
fix
Use the gql tag from 'graphql-tag' to parse the query before passing it: import gql from 'graphql-tag'; cache.readQuery({ query: gql... }).
deprecated Package apollo-cache-inmemory is deprecated. Use @apollo/client instead.
fix Replace import { InMemoryCache } from 'apollo-cache-inmemory' with import { InMemoryCache } from '@apollo/client'. In Apollo Client 3.0+, the cache is bundled.
gotcha The InMemoryCache constructor uses a heuristic fragment matcher by default which silently drops union/interface fragments.
fix If using fragments on unions or interfaces, provide an IntrospectionFragmentMatcher configured with your schema's possibleTypes.
breaking Apollo Client 3.0 (and @apollo/client) changes the cache API significantly; apollo-cache-inmemory is no longer compatible.
fix Upgrade to @apollo/client and use its InMemoryCache with type policies instead of custom resolvers.
gotcha dataIdFromObject defaults to using path-based keys if id or _id are missing, causing duplicate objects to be stored separately.
fix Provide a custom dataIdFromObject function that returns a stable unique identifier for each object (e.g., based on __typename and primary key).
deprecated The CacheResolver API is deprecated and removed in Apollo Client 3.0.
fix Use type policies in @apollo/client's cache instead.
npm install apollo-cache-inmemory
yarn add apollo-cache-inmemory
pnpm add apollo-cache-inmemory

Initialize InMemoryCache and ApolloClient with a GraphQL endpoint, then execute a query.

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

const cache = new InMemoryCache({
  addTypename: true,
  dataIdFromObject: (object) => object.id || object._id,
});

const client = new ApolloClient({
  link: new HttpLink({ uri: 'https://api.example.com/graphql' }),
  cache,
});

client.query({ query: gql`{ hello }` }).then(result => console.log(result));