From 09a052830880bde2bad0ec240c1f86e4ce79b584 Mon Sep 17 00:00:00 2001 From: Grzegorz Rozdzialik Date: Tue, 30 May 2023 18:28:10 +0200 Subject: [PATCH] refactor(cache): add generic parameter for ModifyOptions A generic parameter is easier to discover and set than `fields: { ... } satisfies Modifiers<...>` --- src/cache/core/cache.ts | 2 +- src/cache/core/types/Cache.ts | 6 ++++-- src/cache/inmemory/__tests__/cache.ts | 4 ++-- src/cache/inmemory/inMemoryCache.ts | 2 +- src/cache/inmemory/types.ts | 3 ++- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cache/core/cache.ts b/src/cache/core/cache.ts index b87f9da95eb..bdc6dfaf9db 100644 --- a/src/cache/core/cache.ts +++ b/src/cache/core/cache.ts @@ -117,7 +117,7 @@ export abstract class ApolloCache implements DataProxy { return []; } - public modify(options: Cache.ModifyOptions): boolean { + public modify(options: Cache.ModifyOptions): boolean { return false; } diff --git a/src/cache/core/types/Cache.ts b/src/cache/core/types/Cache.ts index 79704962b25..85c6f70aabb 100644 --- a/src/cache/core/types/Cache.ts +++ b/src/cache/core/types/Cache.ts @@ -57,9 +57,11 @@ export namespace Cache { discardWatches?: boolean; } - export interface ModifyOptions { + export interface ModifyOptions { id?: string; - fields: Modifiers> | Modifier; + fields: Entity extends Record + ? Modifiers | Modifier + : Modifier; optimistic?: boolean; broadcast?: boolean; } diff --git a/src/cache/inmemory/__tests__/cache.ts b/src/cache/inmemory/__tests__/cache.ts index 985989dc466..1ce6bba07d0 100644 --- a/src/cache/inmemory/__tests__/cache.ts +++ b/src/cache/inmemory/__tests__/cache.ts @@ -4069,7 +4069,7 @@ describe('TypedDocumentNode', () => { ): ExpectedType extends ActualType ? void : never => void 0 as any; - cache.modify({ + cache.modify({ id: cache.identify(ffplBook), fields: { isbn: (value) => { @@ -4090,7 +4090,7 @@ describe('TypedDocumentNode', () => { return DELETE; }, - } satisfies Modifiers, + }, }); }); }); diff --git a/src/cache/inmemory/inMemoryCache.ts b/src/cache/inmemory/inMemoryCache.ts index 9dd57fab005..f2efc756730 100644 --- a/src/cache/inmemory/inMemoryCache.ts +++ b/src/cache/inmemory/inMemoryCache.ts @@ -204,7 +204,7 @@ export class InMemoryCache extends ApolloCache { } } - public modify(options: Cache.ModifyOptions): boolean { + public modify(options: Cache.ModifyOptions): boolean { if (hasOwn.call(options, "id") && !options.id) { // To my knowledge, TypeScript does not currently provide a way to // enforce that an optional property?:type must *not* be undefined diff --git a/src/cache/inmemory/types.ts b/src/cache/inmemory/types.ts index 57437436e63..8605e0e842f 100644 --- a/src/cache/inmemory/types.ts +++ b/src/cache/inmemory/types.ts @@ -49,7 +49,8 @@ export interface NormalizedCache { merge(olderId: string, newerObject: StoreObject): void; merge(olderObject: StoreObject, newerId: string): void; - modify(dataId: string, fields: Modifiers> | Modifier): boolean; + modify>(dataId: string, fields: Modifiers): boolean; + modify(dataId: string, modifier: Modifier): boolean; delete(dataId: string, fieldName?: string): boolean; clear(): void;