apollo-caching-strategies
Guides implementation of Apollo Client caching mechanisms to optimize data fetching and state management.
Install
mkdir -p .claude/skills/apollo-caching-strategies && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/14383" && unzip -o skill.zip -d .claude/skills/apollo-caching-strategies && rm skill.zipInstalls to .claude/skills/apollo-caching-strategies
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Use when implementing Apollo caching strategies including cache policies, optimistic UI, cache updates, and normalization.Key capabilities
- →Configure InMemoryCache with type policies for pagination
- →Implement optimistic UI updates for perceived performance
- →Read and write data directly to the cache
- →Define custom merge functions for fields like comments
- →Use different fetch policies for data retrieval
- →Normalize GraphQL cache data using keyFields
How it works
Apollo Client's in-memory cache stores and manages GraphQL data, allowing for efficient data fetching and updates based on configured policies and normalization rules.
Inputs & outputs
When to use apollo-caching-strategies
- →Configure Apollo cache policies
- →Implement optimistic UI updates
- →Normalize GraphQL cache data
About this skill
Apollo Caching Strategies
Master Apollo Client's caching mechanisms for building performant applications with optimal data fetching and state management strategies.
Overview
Apollo Client's intelligent cache is a normalized, in-memory data store that allows for efficient data fetching and updates. Understanding cache policies and management strategies is crucial for building high-performance apps.
Installation and Setup
Cache Configuration
// apollo/cache.js
import { InMemoryCache, makeVar } from '@apollo/client';
export const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
posts: {
// Pagination with offset
keyArgs: ['filter'],
merge(existing = [], incoming, { args }) {
const merged = existing.slice(0);
const offset = args?.offset || 0;
for (let i = 0; i < incoming.length; i++) {
merged[offset + i] = incoming[i];
}
return merged;
}
}
}
},
Post: {
keyFields: ['id'],
fields: {
comments: {
merge(existing = [], incoming) {
return [...existing, ...incoming];
}
}
}
},
User: {
keyFields: ['email'],
fields: {
fullName: {
read(_, { readField }) {
return `${readField('firstName')} ${readField('lastName')}`;
}
}
}
}
}
});
Core Patterns
1. Fetch Policies
// Different fetch policies for different use cases
import { useQuery } from '@apollo/client';
import { GET_POSTS } from './queries';
// cache-first (default): Check cache first, network if not found
function CacheFirstPosts() {
const { data } = useQuery(GET_POSTS, {
fetchPolicy: 'cache-first'
});
return <PostsList posts={data?.posts} />;
}
// cache-only: Never make network request, cache or error
function CacheOnlyPosts() {
const { data } = useQuery(GET_POSTS, {
fetchPolicy: 'cache-only'
});
return <PostsList posts={data?.posts} />;
}
// cache-and-network: Return cache immediately, update with network
function CacheAndNetworkPosts() {
const { data, loading, networkStatus } = useQuery(GET_POSTS, {
fetchPolicy: 'cache-and-network',
notifyOnNetworkStatusChange: true
});
return (
<div>
{networkStatus === 1 && <Spinner />}
<PostsList posts={data?.posts} />
</div>
);
}
// network-only: Always make network request, update cache
function NetworkOnlyPosts() {
const { data } = useQuery(GET_POSTS, {
fetchPolicy: 'network-only'
});
return <PostsList posts={data?.posts} />;
}
// no-cache: Always make network request, don't update cache
function NoCachePosts() {
const { data } = useQuery(GET_POSTS, {
fetchPolicy: 'no-cache'
});
return <PostsList posts={data?.posts} />;
}
// standby: Like cache-first but doesn't auto-update
function StandbyPosts() {
const { data, refetch } = useQuery(GET_POSTS, {
fetchPolicy: 'standby'
});
return (
<div>
<button onClick={() => refetch()}>Refresh</button>
<PostsList posts={data?.posts} />
</div>
);
}
2. Cache Reads and Writes
// apollo/cacheOperations.js
import { gql } from '@apollo/client';
// Read from cache
export function readPostFromCache(client, postId) {
try {
const data = client.readQuery({
query: gql`
query GetPost($id: ID!) {
post(id: $id) {
id
title
body
}
}
`,
variables: { id: postId }
});
return data?.post;
} catch (error) {
console.error('Post not in cache:', error);
return null;
}
}
// Write to cache
export function writePostToCache(client, post) {
client.writeQuery({
query: gql`
query GetPost($id: ID!) {
post(id: $id) {
id
title
body
}
}
`,
variables: { id: post.id },
data: { post }
});
}
// Read fragment
export function readPostFragment(client, postId) {
return client.readFragment({
id: `Post:${postId}`,
fragment: gql`
fragment PostFields on Post {
id
title
body
likesCount
}
`
});
}
// Write fragment
export function updatePostLikes(client, postId, likesCount) {
client.writeFragment({
id: `Post:${postId}`,
fragment: gql`
fragment PostLikes on Post {
likesCount
}
`,
data: {
likesCount
}
});
}
// Modify cache fields
export function incrementPostLikes(client, postId) {
client.cache.modify({
id: client.cache.identify({ __typename: 'Post', id: postId }),
fields: {
likesCount(currentCount = 0) {
return currentCount + 1;
},
isLiked() {
return true;
}
}
});
}
3. Optimistic Updates
// components/OptimisticLike.js
import { useMutation } from '@apollo/client';
import { LIKE_POST } from '../mutations';
function OptimisticLike({ post }) {
const [likePost] = useMutation(LIKE_POST, {
variables: { postId: post.id },
// Optimistic response
optimisticResponse: {
__typename: 'Mutation',
likePost: {
__typename: 'Post',
id: post.id,
likesCount: post.likesCount + 1,
isLiked: true
}
},
// Update cache
update(cache, { data: { likePost } }) {
cache.modify({
id: cache.identify(post),
fields: {
likesCount() {
return likePost.likesCount;
},
isLiked() {
return likePost.isLiked;
}
}
});
},
// Handle errors
onError(error) {
console.error('Like failed, reverting:', error);
// Optimistic update automatically reverted
}
});
return (
<button onClick={() => likePost()}>
{post.isLiked ? 'Unlike' : 'Like'} ({post.likesCount})
</button>
);
}
// Complex optimistic update with multiple changes
function OptimisticCreateComment({ postId }) {
const [createComment] = useMutation(CREATE_COMMENT, {
optimisticResponse: ({ body }) => ({
__typename: 'Mutation',
createComment: {
__typename: 'Comment',
id: `temp-${Date.now()}`,
body,
createdAt: new Date().toISOString(),
author: {
__typename: 'User',
id: currentUser.id,
name: currentUser.name,
avatar: currentUser.avatar
}
}
}),
update(cache, { data: { createComment } }) {
// Add comment to post
cache.modify({
id: cache.identify({ __typename: 'Post', id: postId }),
fields: {
comments(existing = []) {
const newCommentRef = cache.writeFragment({
data: createComment,
fragment: gql`
fragment NewComment on Comment {
id
body
createdAt
author {
id
name
avatar
}
}
`
});
return [...existing, newCommentRef];
},
commentsCount(count = 0) {
return count + 1;
}
}
});
}
});
return <CommentForm onSubmit={createComment} />;
}
4. Cache Eviction
// apollo/eviction.js
export function evictPost(client, postId) {
// Evict specific post
client.cache.evict({
id: client.cache.identify({ __typename: 'Post', id: postId })
});
// Garbage collect
client.cache.gc();
}
export function evictField(client, postId, fieldName) {
// Evict specific field
client.cache.evict({
id: client.cache.identify({ __typename: 'Post', id: postId }),
fieldName
});
}
export function evictAllPosts(client) {
// Evict all posts from cache
client.cache.modify({
fields: {
posts(existing, { DELETE }) {
return DELETE;
}
}
});
client.cache.gc();
}
// Usage in delete mutation
function DeletePost({ postId }) {
const [deletePost] = useMutation(DELETE_POST, {
variables: { id: postId },
update(cache) {
// Remove from posts list
cache.modify({
fields: {
posts(existingPosts = [], { readField }) {
return existingPosts.filter(
ref => postId !== readField('id', ref)
);
}
}
});
// Evict post and related data
cache.evict({ id: cache.identify({ __typename: 'Post', id: postId }) });
cache.gc();
}
});
return <button onClick={() => deletePost()}>Delete</button>;
}
5. Reactive Variables
// apollo/reactiveVars.js
import { makeVar, useReactiveVar } from '@apollo/client';
// Create reactive variables
export const cartItemsVar = makeVar([]);
export const themeVar = makeVar('light');
export const isModalOpenVar = makeVar(false);
export const notificationsVar = makeVar([]);
// Helper functions
export function addToCart(item) {
const cart = cartItemsVar();
cartItemsVar([...cart, item]);
}
export function removeFromCart(itemId) {
const cart = cartItemsVar();
cartItemsVar(cart.filter(item => item.id !== itemId));
}
export function clearCart() {
cartItemsVar([]);
}
export function toggleTheme() {
const current = themeVar();
themeVar(current === 'light' ? 'dark' : 'light');
}
export function addNotification(notification) {
const notifications = notificationsVar();
notificationsVar([...notifications, {
id: Date.now(),
...notification
}]);
}
// React component usage
function Cart() {
const cartItems = useReactiveVar(cartItemsVar);
return (
<div>
<h2>Cart ({cartItems.length})</h2>
{cartItems.map(item => (
<div key={item.id}>
{item.name}
<button onClick={() => removeFromCart(item.id)}>Remove</button>
</di
---
*Content truncated.*
When not to use it
- →When real-time data requires bypassing the cache entirely
- →When the application does not use Apollo Client for data fetching
- →When data freshness needs are not met by available fetch policies
Limitations
- →Cache configuration requires explicit type policies and keyFields definitions
- →Incorrect merge logic can lead to cache inconsistencies
- →Over-caching or memory leaks can occur if not managed properly
How it compares
This skill standardizes Apollo Client's caching mechanisms, providing patterns for managing data fetching and state, unlike manual data handling that lacks built-in normalization and optimistic UI support.
Compared to similar skills
apollo-caching-strategies side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| apollo-caching-strategies (this skill) | 0 | 3mo | Review | Advanced |
| graphql | 6 | 6mo | No flags | Advanced |
| graphql-architect | 1 | 4mo | No flags | Advanced |
| api-design-principles | 72 | 2mo | No flags | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by majiayu000
View all by majiayu000 →You might also like
graphql
davila7
GraphQL gives clients exactly the data they need - no more, no less. One endpoint, typed schema, introspection. But the flexibility that makes it powerful also makes it dangerous. Without proper controls, clients can craft queries that bring down your server. This skill covers schema design, resolvers, DataLoader for N+1 prevention, federation for microservices, and client integration with Apollo/urql. Key insight: GraphQL is a contract. The schema is the API documentation. Design it carefully.
graphql-architect
sickn33
Master modern GraphQL with federation, performance optimization, and enterprise security. Build scalable schemas, implement advanced caching, and design real-time systems. Use PROACTIVELY for GraphQL architecture or performance optimization.
api-design-principles
wshobson
Master REST and GraphQL API design principles to build intuitive, scalable, and maintainable APIs that delight developers. Use when designing new APIs, reviewing API specifications, or establishing API design standards.
nodejs-backend-patterns
wshobson
Build production-ready Node.js backend services with Express/Fastify, implementing middleware patterns, error handling, authentication, database integration, and API design best practices. Use when creating Node.js servers, REST APIs, GraphQL backends, or microservices architectures.
api-contract-sync-manager
ananddtyagi
Validate OpenAPI, Swagger, and GraphQL schemas match backend implementation. Detect breaking changes, generate TypeScript clients, and ensure API documentation stays synchronized. Use when working with API spec files (.yaml, .json, .graphql), reviewing API changes, generating frontend types, or validating endpoint implementations.
rdc-graphql-setup
reactive
Set up @data-client/graphql for GraphQL APIs. Configures GQLEndpoint with auth and custom options. Use after rdc-setup detects GraphQL patterns.