{"id":17172232,"url":"https://github.com/lucasconstantino/apollo-cache-invalidation","last_synced_at":"2025-04-13T16:12:53.139Z","repository":{"id":57121070,"uuid":"85350131","full_name":"lucasconstantino/apollo-cache-invalidation","owner":"lucasconstantino","description":"Experimental cache invalidation tools for Apollo.","archived":false,"fork":false,"pushed_at":"2019-04-16T16:51:08.000Z","size":160,"stargazers_count":66,"open_issues_count":5,"forks_count":6,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-13T16:12:48.153Z","etag":null,"topics":["apollo-client","cache","graphql"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucasconstantino.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-17T20:01:07.000Z","updated_at":"2024-06-04T00:16:32.000Z","dependencies_parsed_at":"2022-08-24T05:40:15.074Z","dependency_job_id":null,"html_url":"https://github.com/lucasconstantino/apollo-cache-invalidation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasconstantino%2Fapollo-cache-invalidation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasconstantino%2Fapollo-cache-invalidation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasconstantino%2Fapollo-cache-invalidation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasconstantino%2Fapollo-cache-invalidation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucasconstantino","download_url":"https://codeload.github.com/lucasconstantino/apollo-cache-invalidation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741195,"owners_count":21154255,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apollo-client","cache","graphql"],"created_at":"2024-10-14T23:36:28.221Z","updated_at":"2025-04-13T16:12:53.080Z","avatar_url":"https://github.com/lucasconstantino.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Apollo Cache Invalidation\n\nA library to simplify cache invalidation for [Apollo clients](https://github.com/apollographql/apollo-client).\n\n![Build status](https://travis-ci.org/lucasconstantino/apollo-cache-invalidation.svg?branch=master)\n[![sponsored by Taller](https://raw.githubusercontent.com/TallerWebSolutions/tallerwebsolutions.github.io/master/sponsored-by-taller.png)](https://taller.net.br/en/)\n\n## Installation\n\n```\nyarn add apollo-cache-invalidation\n```\n\n\u003e Or `npm install --save apollo-cache-invalidation`, if you are still in the old days.\n\n## Motivation\n\nCache control - and most of invalidation - is still a [discussing issue](https://github.com/apollographql/apollo-client/search?utf8=%E2%9C%93\u0026q=cache+invalidation\u0026type=Issues) for the Apollo Client team and the community involved. While participating in [one of those issues](https://github.com/apollographql/apollo-client/issues/621#issuecomment-281809084), I've proposed a way to do field-based cache invalidation. This projects aims to fulfil this need, while something like this isn't implemented in core.\n\n## How does it work\n\nThis project exposes *invalidateFields*: a generator for a mutation [`update function`](https://www.apollographql.com/docs/react/advanced/caching.html#after-mutations) implementation specialized in invalidating cache based on field paths.\n\nIn some cases after a mutation you want to invalidate cache on other queries that might have become outdated, but you can't really update their results from the data provided by the mutation. The *refetchQueries* is often the tool of choice, but it allows no deep field invalidation, meaning you'll have to invalidate the exact and very specific performed queries. *invalidateFields* is an alternative.\n\n## Usage\n\n```js\nimport { invalidateFields, ROOT } from 'apollo-cache-invalidation'\nimport gql from 'graphql-tag'\n\nimport { client } from './client' // Apollo Client instance.\n\nconst mutation = gql`\n  mutation MakeUserHappy($user: ID!) {\n    makeUserHappy(user: $user) {\n      id\n    }\n  }\n`\n\n// Invalidate happyPeople field on the Root Query. Force it to run again.\nconst update = invalidateFields((proxy, result) =\u003e [\n  [ROOT, 'happyPeople']\n])\n\nclient.mutate({ mutation, update, variables: { user: 1 } })\n```\n\nThe function provided to *invalidateFields* will receive a *[DataProxy](http://dev.apollodata.com/core/apollo-client-api.html#DataProxy)* instance and the result for the mutation as arguments. It must then return an array of field paths to invalidate. Each field path consist of an array of keys. Each key can be one of:\n\n- **String:** the key to invalidate;\n- **RegExp:** regex to match keys to invalidate;\n- **Function:** custom matching function to match keys to invalidate.\n\nEach path will be compared individually to the whole cached data, invalidating any matched fields (possibly multiple) along the way.\n\nThe first key in a field path will test against either an object id (as resolved by the [`dataIdFromObject`](http://dev.apollodata.com/core/apollo-client-api.html#apollo-client) Apollo client config) or the *ROOT_QUERY* special key. In that case, you can provide the string `'ROOT_QUERY'`, or better, use the exported `ROOT` constant, as shown above.\n\n### Regex matching sample\n\nImagine you wan't to invalidate field *happy* for every user after a given mutation. Having a `dataIdFromObject` as such:\n\n```js\n// Concatenate \"__typename\" and \"id\" field values to find identification.\n// Do not uniquely identify resource if one of the fields is not provided\n// (will use queried field name and variables, by default).\nconst dataIdFromObject = ({ __typename, id }) =\u003e {\n  if (__typename \u0026\u0026 id) return __typename + id\n  return null\n}\n```\n\nyou can invalidate a given field on all User type cached object with the following:\n\n```js\nconst update = invalidateFields(() =\u003e [[/^User[0-9]+$/, 'happy']])\n\nclient.mutate({ mutation, update })\n```\n\n### Function matching\n\nSimilar to the Regex matching, you can do any customized field matching as so:\n\n```js\nconst randomKeyMatch = key =\u003e Math.random() \u003e= 0.5\n\nconst update = invalidateFields(() =\u003e [\n  [randomKeyMatch, 'happy']\n])\n\nclient.mutate({ mutation, update })\n```\n\n## This package should be temporary\n\nI believe something similar to what is accomplished by this package should be soon added to the [Apollo Client](https://github.com/apollographql/apollo-client) core. If someday that happens, this package will either be deprecated or hold other experimental functionality on the subject of caching and invalidation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasconstantino%2Fapollo-cache-invalidation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucasconstantino%2Fapollo-cache-invalidation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasconstantino%2Fapollo-cache-invalidation/lists"}