···11+---
22+'@urql/exchange-graphcache': major
33+---
44+55+Add a default updater for mutation fields who are lacking an updater and where the returned entity is not present in the cache
···4646 getFieldError,
4747 deferRef,
4848} from './shared';
4949+import { invalidateType } from './invalidate';
49505051export interface WriteResult {
5152 data: null | Data;
···373374374375 data[fieldName] = fieldValue;
375376 updater(data, fieldArgs || {}, ctx.store, ctx);
377377+ } else if (
378378+ typename === ctx.store.rootFields['mutation'] &&
379379+ !ctx.optimistic
380380+ ) {
381381+ // If we're on a mutation that doesn't have an updater, we'll see
382382+ // whether we can find the entity returned by the mutation in the cache.
383383+ // if we don't we'll assume this is a create mutation and invalidate
384384+ // the found __typename.
385385+ if (fieldValue && Array.isArray(fieldValue)) {
386386+ for (let i = 0, l = fieldValue.length; i < l; i++) {
387387+ const key = ctx.store.keyOfEntity(fieldValue[i]);
388388+ if (key && fieldValue[i].__typename) {
389389+ const resolved = InMemoryData.readRecord(key, '__typename');
390390+ const count = InMemoryData!.getRefCount(key);
391391+ if (resolved && !count) {
392392+ invalidateType(fieldValue[i].__typename);
393393+ }
394394+ }
395395+ }
396396+ } else if (fieldValue && typeof fieldValue === 'object') {
397397+ const key = ctx.store.keyOfEntity(fieldValue as any);
398398+ if (key) {
399399+ const resolved = InMemoryData.readRecord(key, '__typename');
400400+ const count = InMemoryData.getRefCount(key);
401401+ if ((!resolved || !count) && fieldValue.__typename) {
402402+ invalidateType(fieldValue.__typename);
403403+ }
404404+ }
405405+ }
376406 }
377407378408 // After processing the field, remove the current alias from the path again
+6-2
exchanges/graphcache/src/store/data.ts
···338338 return node !== undefined ? node[fieldKey] : undefined;
339339};
340340341341+export function getRefCount(entityKey: string): number {
342342+ return currentData!.refCount.get(entityKey) || 0;
343343+}
344344+341345/** Adjusts the reference count of an entity on a refCount dict by "by" and updates the gc */
342346const updateRCForEntity = (entityKey: string, by: number): void => {
343347 // Retrieve the reference count and adjust it by "by"
344344- const count = currentData!.refCount.get(entityKey) || 0;
348348+ const count = getRefCount(entityKey);
345349 const newCount = count + by > 0 ? count + by : 0;
346350 currentData!.refCount.set(entityKey, newCount);
347351 // Add it to the garbage collection batch if it needs to be deleted or remove it
···410414411415 // Check first whether the entity has any references,
412416 // if so, we skip it from the GC run
413413- const rc = currentData!.refCount.get(entityKey) || 0;
417417+ const rc = getRefCount(entityKey);
414418 if (rc > 0) continue;
415419416420 const record = currentData!.records.base.get(entityKey);