Skip to content

Commit

Permalink
refactor(cache): add generic parameter for ModifyOptions
Browse files Browse the repository at this point in the history
A generic parameter is easier to discover and set than
`fields: { ... } satisfies Modifiers<...>`
  • Loading branch information
Gelio committed May 30, 2023
1 parent a27b0ed commit 09a0528
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
return [];
}

public modify(options: Cache.ModifyOptions): boolean {
public modify<Entity = any>(options: Cache.ModifyOptions<Entity>): boolean {
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions src/cache/core/types/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ export namespace Cache {
discardWatches?: boolean;
}

export interface ModifyOptions {
export interface ModifyOptions<Entity = any> {
id?: string;
fields: Modifiers<Record<string, any>> | Modifier<any>;
fields: Entity extends Record<string, unknown>
? Modifiers<Entity> | Modifier<Entity>
: Modifier<Entity>;
optimistic?: boolean;
broadcast?: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions src/cache/inmemory/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4069,7 +4069,7 @@ describe('TypedDocumentNode<Data, Variables>', () => {
): ExpectedType extends ActualType ? void : never =>
void 0 as any;

cache.modify({
cache.modify<Book>({
id: cache.identify(ffplBook),
fields: {
isbn: (value) => {
Expand All @@ -4090,7 +4090,7 @@ describe('TypedDocumentNode<Data, Variables>', () => {

return DELETE;
},
} satisfies Modifiers<Book>,
},
});
});
});
2 changes: 1 addition & 1 deletion src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
}
}

public modify(options: Cache.ModifyOptions): boolean {
public modify<Entity = any>(options: Cache.ModifyOptions<Entity>): 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
Expand Down
3 changes: 2 additions & 1 deletion src/cache/inmemory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export interface NormalizedCache {
merge(olderId: string, newerObject: StoreObject): void;
merge(olderObject: StoreObject, newerId: string): void;

modify(dataId: string, fields: Modifiers<Record<string, any>> | Modifier<any>): boolean;
modify<Entity extends Record<string, any>>(dataId: string, fields: Modifiers<Entity>): boolean;
modify<Entity>(dataId: string, modifier: Modifier<Entity>): boolean;
delete(dataId: string, fieldName?: string): boolean;
clear(): void;

Expand Down

0 comments on commit 09a0528

Please sign in to comment.