From 751802a9a59ab8d685e4d6a0ececc9f3f1873e9a Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 20 Aug 2021 11:29:33 -0400 Subject: [PATCH] Make cache.identify return undefined instead of throwing. Should help with `cache.identify`-related cases of issue #6673. --- src/cache/inmemory/__tests__/entityStore.ts | 16 +++++++++++++--- src/cache/inmemory/inMemoryCache.ts | 9 +++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/cache/inmemory/__tests__/entityStore.ts b/src/cache/inmemory/__tests__/entityStore.ts index a02febc9dfe..9c6a8e7e9fb 100644 --- a/src/cache/inmemory/__tests__/entityStore.ts +++ b/src/cache/inmemory/__tests__/entityStore.ts @@ -1772,9 +1772,19 @@ describe('EntityStore', () => { c: 3, })).toBe('ABCs:{"b":2,"a":1,"c":3}'); - expect(() => cache.identify(ABCs)).toThrowError( - "Missing field 'b' while computing key fields", - ); + { // TODO Extact this to a helper function. + const consoleWarnSpy = jest.spyOn(console, "warn"); + consoleWarnSpy.mockImplementation(() => {}); + try { + expect(cache.identify(ABCs)).toBeUndefined(); + expect(consoleWarnSpy).toHaveBeenCalledTimes(1); + expect(consoleWarnSpy).toHaveBeenCalledWith( + new Error("Missing field 'b' while computing key fields") + ); + } finally { + consoleWarnSpy.mockRestore(); + } + } expect(cache.readFragment({ id: cache.identify({ diff --git a/src/cache/inmemory/inMemoryCache.ts b/src/cache/inmemory/inMemoryCache.ts index f48a210f071..c021250bb84 100644 --- a/src/cache/inmemory/inMemoryCache.ts +++ b/src/cache/inmemory/inMemoryCache.ts @@ -4,6 +4,7 @@ import './fixPolyfills'; import { DocumentNode } from 'graphql'; import { OptimisticWrapperFunction, wrap } from 'optimism'; import { equal } from '@wry/equality'; +import { invariant } from 'ts-invariant'; import { ApolloCache } from '../core/cache'; import { Cache } from '../core/types/Cache'; @@ -334,8 +335,12 @@ export class InMemoryCache extends ApolloCache { // sure that none of the primary key fields have been renamed by aliasing. // If you pass a Reference object, its __ref ID string will be returned. public identify(object: StoreObject | Reference): string | undefined { - return isReference(object) ? object.__ref : - this.policies.identify(object)[0]; + if (isReference(object)) return object.__ref; + try { + return this.policies.identify(object)[0]; + } catch (e) { + invariant.warn(e); + } } public evict(options: Cache.EvictOptions): boolean {