Registry / web-framework / apollo-cache-inmemory

apollo-cache-inmemory

JSON →
library1.6.6jsnpmunverified

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-inmemory
INSTALL
IMPORT
SIG · APOLLO-CACHE-INMEM
A
apollo-cache-inmemory
web-frameworkjavascriptv1.6.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

InMemoryCache
import { InMemoryCache } from 'apollo-cache-inmemory'
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache
Default export does not exist; use named import. With Apollo Client 3.0+, import from '@apollo/client' instead.
IntrospectionFragmentMatcher
import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'
import IntrospectionFragmentMatcher from 'apollo-cache-inmemory'
Only needed for GraphQL schema with unions or interfaces. Import from same package.
defaultDataIdFromObject
import { defaultDataIdFromObject } from 'apollo-cache-inmemory'
Utility for generating default cache keys based on __typename and id/_id.
CacheResolver
import { CacheResolver } from 'apollo-cache-inmemory'
Deprecated in Apollo Client 3.0; use type policies instead.

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));
Debug
Known issues
deprecatedPackage 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.
affects: >=1.0.0
gotchaThe 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.
affects: >=1.0.0
breakingApollo 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.
affects: >=3.0.0
gotchadataIdFromObject 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).
affects: >=1.0.0
deprecatedThe CacheResolver API is deprecated and removed in Apollo Client 3.0.
fix
Use type policies in @apollo/client's cache instead.
affects: >=1.0.0
Errors
Common errors & fixes
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.
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 }) }).
TypeError: Cannot read property 'id' of undefined
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: Could not find result for store key: ...
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.
Invariant Violation: You must pass a graphql document to the query option of ...
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`...` }).
Upgrade
Version history
1.6.6latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for GraphQL type definitions and query parsing.
Agent activity
32 hits · last 30 days
node
26
OpenAI (training)
1
Resources
apollo-cache-inmemory — npm install apollo-cache-inmemory · libregistry