{"id":18120,"library":"apollo-cache-inmemory","title":"apollo-cache-inmemory","description":"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.","status":"deprecated","version":"1.6.6","language":"javascript","source_language":"en","source_url":"https://github.com/apollographql/apollo-client","tags":["javascript","typescript"],"install":[{"cmd":"npm install apollo-cache-inmemory","lang":"bash","label":"npm"},{"cmd":"yarn add apollo-cache-inmemory","lang":"bash","label":"yarn"},{"cmd":"pnpm add apollo-cache-inmemory","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for GraphQL type definitions and query parsing.","package":"graphql","optional":false}],"imports":[{"note":"Default export does not exist; use named import. With Apollo Client 3.0+, import from '@apollo/client' instead.","wrong":"const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache","symbol":"InMemoryCache","correct":"import { InMemoryCache } from 'apollo-cache-inmemory'"},{"note":"Only needed for GraphQL schema with unions or interfaces. Import from same package.","wrong":"import IntrospectionFragmentMatcher from 'apollo-cache-inmemory'","symbol":"IntrospectionFragmentMatcher","correct":"import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'"},{"note":"Utility for generating default cache keys based on __typename and id/_id.","wrong":"","symbol":"defaultDataIdFromObject","correct":"import { defaultDataIdFromObject } from 'apollo-cache-inmemory'"},{"note":"Deprecated in Apollo Client 3.0; use type policies instead.","wrong":"","symbol":"CacheResolver","correct":"import { CacheResolver } from 'apollo-cache-inmemory'"}],"quickstart":{"code":"import { InMemoryCache } from 'apollo-cache-inmemory';\nimport { HttpLink } from 'apollo-link-http';\nimport ApolloClient from 'apollo-client';\n\nconst cache = new InMemoryCache({\n  addTypename: true,\n  dataIdFromObject: (object) => object.id || object._id,\n});\n\nconst client = new ApolloClient({\n  link: new HttpLink({ uri: 'https://api.example.com/graphql' }),\n  cache,\n});\n\nclient.query({ query: gql`{ hello }` }).then(result => console.log(result));","lang":"typescript","description":"Initialize InMemoryCache and ApolloClient with a GraphQL endpoint, then execute a query."},"warnings":[{"fix":"Replace import { InMemoryCache } from 'apollo-cache-inmemory' with import { InMemoryCache } from '@apollo/client'. In Apollo Client 3.0+, the cache is bundled.","message":"Package apollo-cache-inmemory is deprecated. Use @apollo/client instead.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"If using fragments on unions or interfaces, provide an IntrospectionFragmentMatcher configured with your schema's possibleTypes.","message":"The InMemoryCache constructor uses a heuristic fragment matcher by default which silently drops union/interface fragments.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to @apollo/client and use its InMemoryCache with type policies instead of custom resolvers.","message":"Apollo Client 3.0 (and @apollo/client) changes the cache API significantly; apollo-cache-inmemory is no longer compatible.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Provide a custom dataIdFromObject function that returns a stable unique identifier for each object (e.g., based on __typename and primary key).","message":"dataIdFromObject defaults to using path-based keys if id or _id are missing, causing duplicate objects to be stored separately.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use type policies in @apollo/client's cache instead.","message":"The CacheResolver API is deprecated and removed in Apollo Client 3.0.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Pass an IntrospectionFragmentMatcher created from your schema's introspection data to InMemoryCache: new InMemoryCache({ fragmentMatcher: new IntrospectionFragmentMatcher({ introspectionQueryResultData }) }).","cause":"Default heuristic fragment matcher cannot handle unions or interfaces.","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."},{"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.","cause":"Cache lookup using readFragment or readQuery with an object reference that doesn't exist in the cache.","error":"TypeError: Cannot read property 'id' of undefined"},{"fix":"Verify the cache key format (default: typename:id) and ensure the data is present. Avoid evicting needed data.","cause":"Accessing a cache entry that does not exist or has been evicted.","error":"Error: Could not find result for store key: ..."},{"fix":"Use the gql tag from 'graphql-tag' to parse the query before passing it: import gql from 'graphql-tag'; cache.readQuery({ query: gql`...` }).","cause":"Passing a string instead of a parsed GraphQL document (gql) to cache methods.","error":"Invariant Violation: You must pass a graphql document to the query option of ..."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}