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

fix(graphcache): backport invalidation fix (#3545)

Changed files
+18 -2
.changeset
exchanges
graphcache
+5
.changeset/ninety-taxis-return.md
···
···
+
---
+
'@urql/exchange-graphcache': patch
+
---
+
+
Fix `invalidate` not applying when using a string to invalidate an entity
+8 -1
exchanges/graphcache/src/store/store.test.ts
···
});
describe('Invalidating an entity', () => {
-
it('removes an entity from a list.', () => {
InMemoryData.initDataState('write', store.data, null);
store.invalidate(todosData.todos[1]);
const { data } = query(store, { query: Todos });
expect(data).toBe(null);
});
···
});
describe('Invalidating an entity', () => {
+
it('removes an entity from a list by object-key.', () => {
InMemoryData.initDataState('write', store.data, null);
store.invalidate(todosData.todos[1]);
+
const { data } = query(store, { query: Todos });
+
expect(data).toBe(null);
+
});
+
+
it('removes an entity from a list by string-key.', () => {
+
InMemoryData.initDataState('write', store.data, null);
+
store.invalidate(store.keyOfEntity(todosData.todos[1]));
const { data } = query(store, { query: Todos });
expect(data).toBe(null);
});
+5 -1
exchanges/graphcache/src/store/store.ts
···
invalidate(entity: Entity, field?: string, args?: FieldArgs) {
const entityKey = this.keyOfEntity(entity);
const shouldInvalidateType =
-
entity && typeof entity === 'string' && !field && !args;
if (shouldInvalidateType) {
invalidateType(entity);
···
invalidate(entity: Entity, field?: string, args?: FieldArgs) {
const entityKey = this.keyOfEntity(entity);
const shouldInvalidateType =
+
entity &&
+
typeof entity === 'string' &&
+
!field &&
+
!args &&
+
!this.resolve(entity, '__typename');
if (shouldInvalidateType) {
invalidateType(entity);