Registry / storage / apollo-cache-updater

apollo-cache-updater

JSON →
library0.1.3jsnpmunverified

Zero-dependency helper for updating Apollo Client's cache after GraphQL mutations. Version 0.1.3, under heavy development. Automatically updates multiple cached queries (including those with filtered variables) after add/remove/list operations, reducing boilerplate compared to manual cache writes. Works with Apollo Client's data proxy to handle complex scenarios like adding/removing items from lists, updating filtered lists, and maintaining cross-query consistency. Supports React, Vue, and React Native. Note: early stage, breaking changes likely.

npm install apollo-cache-updater
INSTALL
IMPORT
SIG · APOLLO-CACHE-UPDAT
A
apollo-cache-updater
storagejavascriptv0.1.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default
import ApolloCacheUpdater from 'apollo-cache-updater'
import { ApolloCacheUpdater } from 'apollo-cache-updater'
Package exports a single default function. Named import will fail.
ApolloCacheUpdater (require)
const ApolloCacheUpdater = require('apollo-cache-updater')
const { ApolloCacheUpdater } = require('apollo-cache-updater')
CommonJS default import via require, not destructured.
TypeScript type
import ApolloCacheUpdater from 'apollo-cache-updater'; type Options = Parameters<typeof ApolloCacheUpdater>[0]
import { Options } from 'apollo-cache-updater'
No TypeScript types are shipped. Use Parameters to infer options type.

Shows how to use ApolloCacheUpdater to automatically update multiple queries in Apollo cache after a mutation, with variable filtering.

import ApolloCacheUpdater from 'apollo-cache-updater'; import { getArticles, articlesCount } from './queries'; function createArticle(proxy, mutationResult) { const updates = ApolloCacheUpdater({ proxy, queriesToUpdate: [getArticles, articlesCount], searchVariables: { published: true }, mutationResult, }); if (updates) console.log('Article added'); }
Debug
Known issues
breakingPackage is in early development (v0.1.x). API is unstable and may change without notice.
fix
Pin to exact version and test upgrades carefully.
affects: <1.0.0
gotchaThe 'proxy' argument must be the first argument of Apollo's mutation 'update' function, not the cache directly.
fix
Use (proxy, { data }) => ApolloCacheUpdater({ proxy, ... }) from mutation's update handler.
affects: *
gotchaqueriesToUpdate expects query document objects (gql parsed), not string literals or operation names.
fix
Pass the imported gql template literal result, e.g., [GET_ARTICLES].
affects: *
gotchasearchVariables must exactly match the variables used in the target query. Partial matching not supported.
fix
Include all variables from the query's cache key that need to match.
affects: *
deprecatedNo TypeScript definitions provided. May cause 'any' type issues when used with TypeScript.
fix
Consider using apollo-cache-updater with plain JavaScript or create custom type declarations.
affects: *
Errors
Common errors & fixes
TypeError: ApolloCacheUpdater is not a function
Named import used instead of default import.
fix
Change import to: import ApolloCacheUpdater from 'apollo-cache-updater'
Cannot read property 'readQuery' of undefined
proxy is undefined or not passed correctly from mutation update handler.
fix
Ensure proxy is the first argument of the update function: update: (proxy, { data }) => ...
Error: queriesToUpdate must be an array of QueryDefinition objects
Passed string or non-gql object instead of parsed GraphQL document.
fix
Pass array of queries imported from .graphql or gql template: [GET_ARTICLES]
No cache updates applied unexpectedly
searchVariables do not match any cached query's variables exactly.
fix
Check that searchVariables includes all variables from the target query, not just a subset.
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
41 hits · last 30 days
node
34
Resources
apollo-cache-updater — npm install apollo-cache-updater · libregistry