-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathstoreUtils.ts
More file actions
66 lines (62 loc) · 1.6 KB
/
Copy pathstoreUtils.ts
File metadata and controls
66 lines (62 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Representation of a reference object inside the cache.
*/
export interface Reference {
readonly __ref: string;
}
/**
* Determines if a given object is a reference object.
*
* @param obj - The object to check if its a reference object
*
* @example
*
* ```ts
* import { isReference } from "@apollo/client/utilities";
*
* isReference({ __ref: "User:1" }); // true
* isReference({ __typename: "User", id: 1 }); // false
* ```
*/
export function isReference(obj: any): obj is Reference {
return Boolean(
obj && typeof obj === "object" && typeof obj.__ref === "string"
);
}
/**
* Represents the union of valid values that can be stored in the cache.
*/
export type StoreValue =
| number
| string
| string[]
| Reference
| Reference[]
| null
| undefined
| void
| Object;
/**
* Represents an object that is stored in the cache.
*/
export interface StoreObject {
__typename?: string;
[storeFieldName: string]: StoreValue;
}
/**
* Workaround for a TypeScript quirk:
* types per default have an implicit index signature that makes them
* assignable to `StoreObject`.
* interfaces do not have that implicit index signature, so they cannot
* be assigned to `StoreObject`.
* This type just maps over a type or interface that is passed in,
* implicitly adding the index signature.
* That way, the result can be assigned to `StoreObject`.
*
* This is important if some user-defined interface is used e.g.
* in cache.modify, where the `toReference` method expects a
* `StoreObject` as input.
*/
export type AsStoreObject<T extends { __typename?: string }> = {
[K in keyof T]: T[K];
};