npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

apollo-simple-cache

v0.4.3

Published

Simple cache features for Apollo Client

Readme

apollo-simple-cache

Simplified cache classes for Apollo Client.

For more performance test, please see https://github.com/jet2jet/apollo-simple-cache-test .

Install

npm install apollo-simple-cache

Usage

Note: If you use Apollo Client version 4 (v4.x), please replace package name 'apollo-simple-cache' with 'apollo-simple-cache/v4'.

There are two cache classes in this package.

SimpleDocumentCache

This is a very simple cache, writing data per each query (i.e. document cache). This is fastest but may increase query requests.

import { SimpleDocumentCache } from 'apollo-simple-cache';

const cache = new SimpleDocumentCache();
const client = new ApolloClient({ cache });

To use with default options, the GraphQL document must have an operation name (stored in DocumentNode.<OperationDefinitionNode>.name.value).

Note that SimpleDocumentCache does not support 'optimistic' processings.

constructor options

SimpleDocumentCache constructor accepts one optional parameter typed SimpleDocumentCacheOptions.

getCacheKey?: <TVariables>(document: DocumentNode, variables: TVariables | undefined) => CacheKey

A callback function to generate key from document and variables (CacheKey is equal to string). If the GraphQL document cannot have an operation name, you must implement this function.

OptimizedNormalizedCache

This aims to make performance faster than Apollo's InMemoryCache. In some cases OptimizedNormalizedCache is slower than InMemoryCache, but in many cases OptimizedNormalizedCache is faster than InMemoryCache.

import { OptimizedNormalizedCache } from 'apollo-simple-cache';

const cache = new OptimizedNormalizedCache();
const client = new ApolloClient({ cache });

constructor options

OptimizedNormalizedCache constructor accepts one optional parameter typed OptimizedNormalizedCacheOptions.

keyFields?: KeyFields | undefined

Specifies the key (field) names to treat as 'ID'. An array of names or map object including typename referring an array of names can be specified. This is the same for InMemoryCache's option.

possibleTypes?: PossibleTypesMap | undefined

Specifies the type map which indicates the type is extended by some types. This is the same for InMemoryCache's option.

optimizedRead?: OptimizedReadMap | undefined

Specifies for optimized read operation. The value must be the map of type names and functions typed OptimizedReadFunction, which receives fieldName, existingValue, and context: OptimizedReadContext, and should return the value for actual field value.

Example:

const cache = new OptimizedNormalizedCache({
  optimizedRead: {
    Query: (fieldName, existingValue, context) => {
      if (fieldName !== 'person') {
        return existingValue;
      }
      if (existingValue != null) {
        return existingValue;
      }
      const id = context.effectiveArguments?.id;
      if (id == null) {
        return undefined;
      }
      const dataId = context.dataIdFromObject({
        __typename: 'Person',
        id,
      });
      if (dataId == null) {
        return undefined;
      }
      return context.readFromId(dataId);
    },
  },
});
writeToCacheMap?: WriteToCacheMap | undefined

Specifies for touching values during write operation. The value must be the map of type names and functions typed WriteToCacheFunction, which receives fieldName, existingValue, incomingValue, and context: WriteToCacheContext, and should return the value to write to the cache.

dataIdFromObject?: DataIdFromObjectFunction | undefined

Specifies the function to make unique ID for the object. By default <typename>:<id> is used.

rootTypes?: { Query?: string, Mutation?: string, Subscription?: string }

Overrides root type names for Query, Mutation, and Subscription.

Notes
  • You can make keyFields, possibleTypes, optimizedRead, and writeToCacheMap more strictly typed for type names by extending CustomDefinition interface as follows:
// For Apollo Client v4, use 'apollo-simple-cache/v4' instead of 'apollo-simple-cache'.
declare module 'apollo-simple-cache' {
  export interface CustomDefinition {
    // Include your typenames (defined in the schema) into Array type
    typenames: Array<'Person' | 'Prefecture' | 'City'>;
  }
}
  • OptimizedNormalizedCache assumes the results (returned by the cache) to be immutable and writing to the results have no effect. If modifying the result is necessary, deep-cloning is required.
    • You can pass assumeImmutableResults: true safely to ApolloClient's constructor.

License

MIT License