Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

(graphcache) - Invalidating entities (#566)

* add the possibility of invalidating an entity through the store

* add changeset

* add help entry

* run linter

* rename to invalidate and reformat error message

* Update stupid-lizards-return.md

* prevent crash when entity is a string

* respect rename to invalidate and lint

Co-authored-by: Phil Plückthun <phil@kitten.sh>

authored by

Jovi De Croock
Phil Plückthun
and committed by
GitHub
9401909e 3f83c4e4

+56 -1
+5
.changeset/stupid-lizards-return.md
··· 1 + --- 2 + '@urql/exchange-graphcache': minor 3 + --- 4 + 5 + Add `cache.invalidate` to invalidate an entity directly to remove it from the cache and all subsequent cache results, e.g. `cache.invalidate({ __typename: 'Todo', id: 1 })`
+10
docs/graphcache/help.md
··· 267 267 268 268 Please open an issue if it happens on a query that you expect to be supported 269 269 by the `populateExchange`. 270 + 271 + ## (19) Can't generate a key for invalidate(...) 272 + 273 + > Can't generate a key for invalidate(...). 274 + > You need to pass in a valid key (__typename:id) or an object with the "__typename" property and an "id" or "_id" property. 275 + 276 + You probably have called `cache.invalidate` with data that the cache can't generate a key for. 277 + 278 + This may either happen because you're missing the `__typename` and `id` or `_id` field or if the last two 279 + aren't applicable to this entity a custom `keys` entry.
+8
exchanges/graphcache/src/store/store.test.ts
··· 406 406 todos: todosData.todos, 407 407 }); 408 408 }); 409 + 410 + describe('Invalidating an entity', () => { 411 + it('removes an entity from a list.', () => { 412 + store.invalidate(todosData.todos[1]); 413 + const { data } = query(store, { query: Todos }); 414 + expect(data).toBe(null); 415 + }); 416 + }); 409 417 }); 410 418 411 419 describe('Store with storage', () => {
+31
exchanges/graphcache/src/store/store.ts
··· 20 20 KeyingConfig, 21 21 DataFields, 22 22 } from '../types'; 23 + import { invariant } from '../helpers/help'; 23 24 24 25 import { read, readFragment } from '../operations/query'; 25 26 import { writeFragment, startWrite } from '../operations/write'; ··· 154 155 invalidate(this, createRequest(query, variables)); 155 156 } 156 157 158 + invalidate(entity: Data | string) { 159 + const entityKey = 160 + typeof entity === 'string' ? entity : this.keyOfEntity(entity); 161 + 162 + invariant( 163 + entityKey, 164 + "Can't generate a key for invalidate(...).\n" + 165 + 'You have to pass an id or _id field or create a custom `keys` field for `' + 166 + typeof entity === 167 + 'object' 168 + ? (entity as Data).__typename 169 + : entity + '`.', 170 + 19 171 + ); 172 + 173 + const fields = this.inspectFields(entityKey); 174 + for (const field of fields) { 175 + if (InMemoryData.readLink(entityKey as string, field.fieldKey)) { 176 + InMemoryData.writeLink(entityKey as string, field.fieldKey, undefined); 177 + } else { 178 + InMemoryData.writeRecord( 179 + entityKey as string, 180 + field.fieldKey, 181 + undefined 182 + ); 183 + } 184 + } 185 + } 186 + 157 187 inspectFields(entity: Data | string | null): FieldInfo[] { 158 188 const entityKey = 159 189 entity !== null && typeof entity !== 'string' 160 190 ? this.keyOfEntity(entity) 161 191 : entity; 192 + 162 193 return entityKey !== null ? InMemoryData.inspectFields(entityKey) : []; 163 194 } 164 195
+2 -1
exchanges/graphcache/src/types.ts
··· 200 200 | 15 201 201 | 16 202 202 | 17 203 - | 18; 203 + | 18 204 + | 19;