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

tanstack-query-entity-graph

v0.2.0

Published

A smart cache invalidation system for TanStack Query that automatically manages query invalidations based on entity relationships.

Readme

TanStack Query Entity Graph 🎯

A smart cache invalidation system for TanStack Query that automatically manages query invalidations based on entity relationships.

npm version License: MIT

🌟 Features

  • 📊 Declarative entity relationships
  • 🔄 Automatic cache invalidation
  • 🧹 Smart cache reset
  • 🎯 Type-safe entity configuration
  • 🔗 Direct dependency resolution
  • 🎨 Simple and intuitive API
  • 🔀 Cross-root invalidation support

📦 Installation

npm install tanstack-query-entity-graph

🚀 Usage Guide

1. Setting Up Entity Configuration

Define relationships between your entities:

const entityConfig = {
   user: {
      name: 'user',
      invalidate: ['post'], // Entities to invalidate when user is invalidated
      reset: ['stats']      // Entities to reset when user is reset
   },
   post: {
      name: 'post',
      invalidate: ['comment']
   }
};

2. Setting Up the Provider

import { EntityQueryClientProvider } from 'tanstack-query-entity-graph';

const queryClient = new QueryClient();

function App() {
  return (
    <EntityQueryClientProvider
      client={queryClient}
      entityConfig={entityConfig}
    >
      <YourApp />
    </EntityQueryClientProvider>
  );
}

3. Use Cases

Basic Usage - Simple Entity Invalidation

useMutation({
  mutationFn: updateUser,
  meta: {
      affects: ['user']
  } // Will invalidate 'user' and all its related entities
})

Specifying Action Type

useMutation({
  mutationFn: updateUser, 
  meta: {
    affects: [{
        name: 'user',
        action: 'reset' // 'invalidate' or 'reset'
    }]
  }
})

Using Specific Query Key for Main Entity

useMutation({
  mutationFn: updateUser,
  meta: {
      affects: [{
          name: 'user',
          action: 'invalidate',
          queryKey: userKeys.detail({ id: 123 }) // Specific query key for the main entity
      }]
  }
})

Granular Control Over Related Entities

useMutation({
  mutationFn: updateUser,
  meta: {
      affects: [{
          name: 'user',
          action: 'invalidate',
          invalidate: [
              {
                  entity: 'post',
                  queryKey: postKeys.list({ userId: 123 })
              }
          ],
          reset: [
              {
                  entity: 'stats',
                  queryKey: statsKeys.detail({ userId: 123 })
              }
          ]
      }]
  }
})

Multiple Entities in One Mutation

useMutation({
  mutationFn: updateTeam,
  meta: {
      affects: [
          {
              name: 'team',
              action: 'invalidate'
          },
          {
              name: 'user',
              action: 'reset',
              queryKey: userKeys.list({ teamId: 123 })
          }
      ]
  }
})

Complex Configuration Example - User Update Affecting Comments

useMutation({
   mutationFn: updateUser,
   meta: {
       affects: [{
           name: 'user',
           action: 'invalidate',
           queryKey: userKeys.detail({ id: 123 }), // Specific user detail
           invalidate: [
               {
                   entity: 'comment',
                   queryKey: commentKeys.byUser({ userId: 123 }) // All comments need update because they display user's name
               }
           ]
       }]
   }
})

⚠️ Important Notes

  1. Entity names must be lowercase
  2. Query keys must start with the entity name
  3. An entity specified in the main config cannot appear in its own invalidate/reset arrays
  4. Each entity's dependency is processed only one level deep

🔍 Query Key Format

// ✅ Correct query key format
useQuery(['user', id], fetchUser);
useQuery(['post', 'list'], fetchPosts);

// ❌ Won't be invalidated
useQuery(['userPost', id], fetchPost);
useQuery(['user-post', id], fetchPost);

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📝 License

This project is licensed under the MIT License - see the LICENSE.md file for details.