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.
npm install apollo-cache-inmemoryVerified import paths — ran on the pinned version, not inferred.
Initialize InMemoryCache and ApolloClient with a GraphQL endpoint, then execute a query.
Replace import { InMemoryCache } from 'apollo-cache-inmemory' with import { InMemoryCache } from '@apollo/client'. In Apollo Client 3.0+, the cache is bundled.If using fragments on unions or interfaces, provide an IntrospectionFragmentMatcher configured with your schema's possibleTypes.
Upgrade to @apollo/client and use its InMemoryCache with type policies instead of custom resolvers.
Provide a custom dataIdFromObject function that returns a stable unique identifier for each object (e.g., based on __typename and primary key).
Use type policies in @apollo/client's cache instead.
Pass an IntrospectionFragmentMatcher created from your schema's introspection data to InMemoryCache: new InMemoryCache({ fragmentMatcher: new IntrospectionFragmentMatcher({ introspectionQueryResultData }) }).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.
Verify the cache key format (default: typename:id) and ensure the data is present. Avoid evicting needed data.
Use the gql tag from 'graphql-tag' to parse the query before passing it: import gql from 'graphql-tag'; cache.readQuery({ query: gql`...` }).