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

fix(graphcache): ignore current entity during invalidateType (#3560)

Changed files
+23 -5
.changeset
exchanges
graphcache
+5
.changeset/rich-suns-sit.md
···
+
---
+
'@urql/exchange-graphcache': patch
+
---
+
+
When invoking the automatic creation updater ignore the entity we are currently on in the mutation
+6
exchanges/graphcache/src/cacheExchange.test.ts
···
vi.runAllTimers();
expect(response).toHaveBeenCalledTimes(3);
expect(result).toHaveBeenCalledTimes(3);
+
expect(result.mock.calls[1][0].data).toEqual({
+
addAuthor: {
+
id: '2',
+
name: 'Author 2',
+
},
+
});
});
});
+5 -1
exchanges/graphcache/src/operations/invalidate.ts
···
}
};
-
export const invalidateType = (typename: string) => {
+
export const invalidateType = (
+
typename: string,
+
excludedEntities: string[]
+
) => {
const types = InMemoryData.getEntitiesForType(typename);
for (const entity of types) {
+
if (excludedEntities.includes(entity)) continue;
invalidateEntity(entity);
}
};
+6 -3
exchanges/graphcache/src/operations/write.ts
···
// if we don't we'll assume this is a create mutation and invalidate
// the found __typename.
if (fieldValue && Array.isArray(fieldValue)) {
+
const excludedEntities: string[] = fieldValue.map(
+
entity => ctx.store.keyOfEntity(entity) || ''
+
);
for (let i = 0, l = fieldValue.length; i < l; i++) {
-
const key = ctx.store.keyOfEntity(fieldValue[i]);
+
const key = excludedEntities[i];
if (key && fieldValue[i].__typename) {
const resolved = InMemoryData.readRecord(key, '__typename');
const count = InMemoryData!.getRefCount(key);
if (resolved && !count) {
-
invalidateType(fieldValue[i].__typename);
+
invalidateType(fieldValue[i].__typename, excludedEntities);
}
}
}
···
const resolved = InMemoryData.readRecord(key, '__typename');
const count = InMemoryData.getRefCount(key);
if ((!resolved || !count) && fieldValue.__typename) {
-
invalidateType(fieldValue.__typename);
+
invalidateType(fieldValue.__typename, [key]);
}
}
}
+1 -1
exchanges/graphcache/src/store/store.ts
···
!this.resolve(entity, '__typename');
if (shouldInvalidateType) {
-
invalidateType(entity);
+
invalidateType(entity, []);
} else {
invariant(
entityKey,