@jsb188/graphql 1.1.10 → 1.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache/config.ts +64 -50
- package/cache/index.test.ts +671 -109
- package/cache/index.ts +1700 -1240
- package/cache/persist.ts +721 -0
- package/cache/store.ts +437 -0
- package/client/data-table-realtime-subscriptions.test.ts +740 -0
- package/client/data-table-realtime-subscriptions.ts +955 -0
- package/client/data-table-realtime.test.ts +792 -0
- package/client/data-table-realtime.ts +1218 -0
- package/client/fragment-realtime-sequence.ts +52 -0
- package/client/index.ts +2 -0
- package/client/open-sheet.ts +137 -0
- package/client/request-managed-session.test.ts +574 -0
- package/client/request-no-store.test.ts +157 -0
- package/client/request.ts +954 -163
- package/client/sse-ticket.test.ts +26 -0
- package/deno.lock +170 -0
- package/deno.test.json +8 -4
- package/drafts/index.ts +149 -0
- package/drafts/store.ts +397 -0
- package/gql/eagerQueryMap.ts +12 -0
- package/gql/fragments/accountAuthFragments.ts +5 -0
- package/gql/fragments/accountFragments.ts +0 -3
- package/gql/fragments/accountSessionFragments.ts +16 -0
- package/gql/fragments/agentDocFragments.ts +46 -0
- package/gql/fragments/agentMemoryFragments.ts +19 -0
- package/gql/fragments/aiChatFragments.ts +16 -0
- package/gql/fragments/billingFragments.ts +0 -11
- package/gql/fragments/campaignFragments.ts +277 -0
- package/gql/fragments/dataTableFragments.ts +18 -128
- package/gql/fragments/emailFragments.ts +19 -9
- package/gql/fragments/fieldDocFragments.ts +82 -0
- package/gql/fragments/logFragments.ts +20 -79
- package/gql/fragments/organizationFragments.ts +43 -5
- package/gql/fragments/scheduleFragments.ts +155 -0
- package/gql/fragments/sheetFragments.ts +552 -22
- package/gql/fragments/storageFragments.ts +21 -0
- package/gql/fragments/workflowFragments.ts +28 -4
- package/gql/mutations/accountMutations.ts +0 -2
- package/gql/mutations/agentDocMutations.ts +177 -0
- package/gql/mutations/agentMemoryMutations.ts +36 -0
- package/gql/mutations/aiChatMutations.ts +17 -4
- package/gql/mutations/authMutations.ts +158 -17
- package/gql/mutations/campaignMutations.ts +215 -0
- package/gql/mutations/dataTableMutations.ts +48 -26
- package/gql/mutations/fieldDocMutations.ts +24 -0
- package/gql/mutations/index.ts +6 -1
- package/gql/mutations/logMutations.ts +6 -29
- package/gql/mutations/organizationMutations.ts +13 -4
- package/gql/mutations/scheduleMutations.ts +423 -0
- package/gql/mutations/sheetMutations.ts +273 -56
- package/gql/mutations/storageMutations.ts +20 -0
- package/gql/mutations/workflowMutations.ts +42 -0
- package/gql/queries/agentDocQueries.ts +120 -0
- package/gql/queries/agentMemoryQueries.ts +22 -0
- package/gql/queries/aiChatQueries.ts +18 -2
- package/gql/queries/authQueries.ts +41 -0
- package/gql/queries/billingQueries.ts +23 -0
- package/gql/queries/campaignQueries.ts +127 -0
- package/gql/queries/dataTableQueries.ts +100 -11
- package/gql/queries/emailQueries.ts +55 -3
- package/gql/queries/fieldDocQueries.ts +158 -0
- package/gql/queries/index.ts +8 -2
- package/gql/queries/logQueries.ts +11 -28
- package/gql/queries/organizationQueries.ts +52 -1
- package/gql/queries/scheduleQueries.ts +315 -0
- package/gql/queries/sheetQueries.ts +336 -25
- package/gql/queries/storageQueries.ts +38 -0
- package/gql/queries/workflowQueries.ts +72 -3
- package/hooks/index.ts +2065 -971
- package/hooks/use-account-mtn.ts +76 -81
- package/hooks/use-agentDoc-mtn.ts +285 -0
- package/hooks/use-agentDoc-qry.ts +208 -0
- package/hooks/use-agentMemory-mtn.ts +73 -0
- package/hooks/use-agentMemory-qry.ts +44 -0
- package/hooks/use-aiChat-mtn.ts +232 -137
- package/hooks/use-aiChat-qry.ts +480 -222
- package/hooks/use-app-qry.ts +12 -9
- package/hooks/use-auth-mtn.ts +406 -252
- package/hooks/use-auth-qry.ts +51 -0
- package/hooks/use-campaign-mtn.ts +377 -0
- package/hooks/use-campaign-qry.ts +257 -0
- package/hooks/use-dataTable-mtn.ts +116 -33
- package/hooks/use-dataTable-qry.ts +859 -35
- package/hooks/use-email-qry.ts +194 -79
- package/hooks/use-fieldDoc-mtn.ts +50 -0
- package/hooks/use-fieldDoc-qry.ts +180 -0
- package/hooks/use-inboundContact-mtn.ts +19 -16
- package/hooks/use-inboundContact-qry.ts +63 -55
- package/hooks/use-integration-mtn.ts +56 -38
- package/hooks/use-integration-qry.ts +24 -9
- package/hooks/use-log-mtn.ts +72 -56
- package/hooks/use-log-qry.ts +93 -69
- package/hooks/use-organization-mtn.ts +334 -277
- package/hooks/use-organization-qry.ts +199 -129
- package/hooks/use-schedule-mtn.ts +416 -0
- package/hooks/use-schedule-qry.ts +157 -0
- package/hooks/use-sheet-mtn.ts +233 -47
- package/hooks/use-sheet-qry.ts +353 -70
- package/hooks/use-storage-mtn.ts +81 -41
- package/hooks/use-storage-qry.ts +104 -13
- package/hooks/use-workflow-mtn.ts +55 -0
- package/hooks/use-workflow-qry.ts +206 -48
- package/loaders/campaign.ts +137 -0
- package/package.json +94 -71
- package/sse.ts +157 -104
- package/tsconfig.json +11 -10
- package/types.d.ts +60 -30
- package/utils.ts +78 -49
- package/gql/fragments/productFragments.ts +0 -39
- package/gql/fragments/reportFragments.ts +0 -90
- package/gql/mutations/billingMutations.ts +0 -34
- package/gql/queries/productQueries.ts +0 -31
- package/gql/queries/reportQueries.ts +0 -134
- package/hooks/use-product-qry.ts +0 -67
- package/hooks/use-report-qry.ts +0 -153
package/cache/index.ts
CHANGED
|
@@ -1,11 +1,59 @@
|
|
|
1
1
|
import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
cloneArrayLike,
|
|
4
|
+
mapArrayLikeObjects,
|
|
5
|
+
uniq,
|
|
6
|
+
} from '@jsb188/app/utils/object.ts';
|
|
7
|
+
import {
|
|
8
|
+
getQueryResetTargetId,
|
|
9
|
+
queryCacheKeyMatchesResetTarget,
|
|
10
|
+
type QueryResetTarget,
|
|
11
|
+
} from '@jsb188/app/utils/queryReset.ts';
|
|
3
12
|
import type { UpdateObserversFn } from '../types.d.ts';
|
|
4
|
-
import { PARTIALS_MAP, RULES } from './config.ts';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
13
|
+
import { PARTIAL_NULL_PRESERVE_FIELDS, PARTIALS_MAP, RULES } from './config.ts';
|
|
14
|
+
import { cacheStore } from './store.ts';
|
|
15
|
+
import { clearGraphQLCachePersistence } from './persist.ts';
|
|
16
|
+
import { clearAllDrafts } from '../drafts/index.ts';
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
initGraphQLCachePersistence,
|
|
20
|
+
isGraphQLCachePersistenceEnabled,
|
|
21
|
+
} from './persist.ts';
|
|
22
|
+
|
|
23
|
+
// Versioned store namespaces (Map-compatible facades): reads record memo
|
|
24
|
+
// dependencies + LRU stamps, writes bump versions only on real data changes
|
|
25
|
+
// and notify persistence subscribers.
|
|
26
|
+
const QUERIES = cacheStore.queries;
|
|
27
|
+
const FRAGMENTS = cacheStore.fragments;
|
|
8
28
|
const RESET_TIME = new Map();
|
|
29
|
+
const QUERY_DOCUMENT_CACHE_IDS = new WeakMap<object, number>();
|
|
30
|
+
let NEXT_QUERY_DOCUMENT_CACHE_ID = 1;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Return a stable runtime id for one parsed GraphQL document.
|
|
34
|
+
*/
|
|
35
|
+
function getQueryDocumentCacheId(gqlQuery: any): number {
|
|
36
|
+
if (!gqlQuery || typeof gqlQuery !== 'object') {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const currentId = QUERY_DOCUMENT_CACHE_IDS.get(gqlQuery);
|
|
41
|
+
if (currentId) {
|
|
42
|
+
return currentId;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const nextId = NEXT_QUERY_DOCUMENT_CACHE_ID;
|
|
46
|
+
NEXT_QUERY_DOCUMENT_CACHE_ID += 1;
|
|
47
|
+
QUERY_DOCUMENT_CACHE_IDS.set(gqlQuery, nextId);
|
|
48
|
+
return nextId;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Return the response key for a GraphQL field, honoring root aliases.
|
|
53
|
+
*/
|
|
54
|
+
function getGraphQLSelectionResponseName(selection: any) {
|
|
55
|
+
return selection.alias?.value || selection.name?.value;
|
|
56
|
+
}
|
|
9
57
|
|
|
10
58
|
/**
|
|
11
59
|
* JS Map will sometimes convert nested Array into Array-like Object
|
|
@@ -17,23 +65,26 @@ const RESET_TIME = new Map();
|
|
|
17
65
|
*/
|
|
18
66
|
|
|
19
67
|
function enforceArrayType(obj: any): any {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
68
|
+
if (obj && typeof obj === 'object') {
|
|
69
|
+
if (Array.isArray(obj)) {
|
|
70
|
+
return obj.map(enforceArrayType);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const keys = Object.keys(obj);
|
|
74
|
+
if (keys.every((key, i) => key === i.toString())) {
|
|
75
|
+
return keys.map((key) => obj[key]);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for (const key in obj) {
|
|
79
|
+
if (
|
|
80
|
+
Object.prototype.hasOwnProperty.call(obj, key) &&
|
|
81
|
+
typeof obj[key] === 'object'
|
|
82
|
+
) {
|
|
83
|
+
obj[key] = enforceArrayType(obj[key]);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return obj;
|
|
37
88
|
}
|
|
38
89
|
|
|
39
90
|
/**
|
|
@@ -41,38 +92,66 @@ function enforceArrayType(obj: any): any {
|
|
|
41
92
|
*/
|
|
42
93
|
|
|
43
94
|
function mergeNestedObjects(obj1: any, obj2: any) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
95
|
+
if (Array.isArray(obj1) || Array.isArray(obj2)) {
|
|
96
|
+
if (Array.isArray(obj2)) {
|
|
97
|
+
return [...obj2];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (Array.isArray(obj1)) {
|
|
101
|
+
return [...obj1];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return obj2;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!obj1) {
|
|
108
|
+
if (!obj2 || typeof obj2 !== 'object') {
|
|
109
|
+
return obj2;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { ...obj2 };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const newObj = { ...obj1 };
|
|
116
|
+
for (const key in obj2) {
|
|
117
|
+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
|
118
|
+
if (typeof obj2[key] === 'object' && obj2[key] !== null) {
|
|
119
|
+
newObj[key] = mergeNestedObjects(obj1[key], obj2[key]);
|
|
120
|
+
} else {
|
|
121
|
+
newObj[key] = obj2[key];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return newObj;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
* Omit configured null state fields from partial-fragment writes while
|
|
131
|
+
* retaining nulls for volatile fields that must clear stale one-shot data.
|
|
132
|
+
*/
|
|
133
|
+
function getPartialFragmentMergeData(
|
|
134
|
+
fragmentName: string | undefined,
|
|
135
|
+
data: any,
|
|
136
|
+
) {
|
|
137
|
+
const preserveFields = fragmentName
|
|
138
|
+
? PARTIAL_NULL_PRESERVE_FIELDS[fragmentName]
|
|
139
|
+
: null;
|
|
140
|
+
if (!preserveFields?.length || !data || typeof data !== 'object') {
|
|
141
|
+
return data;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let mergeData = data;
|
|
145
|
+
preserveFields.forEach((fieldName) => {
|
|
146
|
+
if (data[fieldName] !== null) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (mergeData === data) {
|
|
150
|
+
mergeData = { ...data };
|
|
151
|
+
}
|
|
152
|
+
delete mergeData[fieldName];
|
|
153
|
+
});
|
|
154
|
+
return mergeData;
|
|
76
155
|
}
|
|
77
156
|
|
|
78
157
|
/**
|
|
@@ -80,7 +159,98 @@ function mergeNestedObjects(obj1: any, obj2: any) {
|
|
|
80
159
|
*/
|
|
81
160
|
|
|
82
161
|
function isPlainObject(value: any) {
|
|
83
|
-
|
|
162
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Return whether one object is an optimistic UI placeholder.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
function isOptimisticUIObject(value: any) {
|
|
170
|
+
return isPlainObject(value) && value.__optimisticUI === true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Return one object's comparable id key, when it has an id.
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
function getObjectIdKey(value: any) {
|
|
178
|
+
if (!isPlainObject(value) || value.id === undefined || value.id === null) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return String(value.id);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Return a copy of one top-level array without optimistic UI objects.
|
|
187
|
+
*/
|
|
188
|
+
|
|
189
|
+
function removeOptimisticUIObjectsFromArray(value: any[]) {
|
|
190
|
+
return value.filter((item) => !isOptimisticUIObject(item));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Return a fragment with optimistic UI objects removed from top-level arrays.
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
function removeOptimisticUIObjectsFromArrayFields(fragment: any) {
|
|
198
|
+
if (!isPlainObject(fragment)) {
|
|
199
|
+
return fragment;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let nextFragment: Record<string, any> | null = null;
|
|
203
|
+
|
|
204
|
+
for (const key in fragment) {
|
|
205
|
+
if (
|
|
206
|
+
!Object.prototype.hasOwnProperty.call(fragment, key) ||
|
|
207
|
+
!Array.isArray(fragment[key])
|
|
208
|
+
) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const cleanedArray = removeOptimisticUIObjectsFromArray(fragment[key]);
|
|
213
|
+
if (cleanedArray.length === fragment[key].length) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const writableFragment: Record<string, any> = nextFragment ?? {
|
|
218
|
+
...fragment,
|
|
219
|
+
};
|
|
220
|
+
writableFragment[key] = cleanedArray;
|
|
221
|
+
nextFragment = writableFragment;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return nextFragment || fragment;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Return spread-backed array data plus unmatched optimistic UI objects.
|
|
229
|
+
*/
|
|
230
|
+
|
|
231
|
+
function mergeSpreadBackedArrayWithOptimisticUIObjects(
|
|
232
|
+
spreadData: any[],
|
|
233
|
+
fragmentData: any[],
|
|
234
|
+
) {
|
|
235
|
+
const spreadIds = new Set(
|
|
236
|
+
spreadData.map(getObjectIdKey).filter((id): id is string => id !== null),
|
|
237
|
+
);
|
|
238
|
+
const mergedData = [...spreadData];
|
|
239
|
+
|
|
240
|
+
fragmentData.forEach((item, index) => {
|
|
241
|
+
if (!isOptimisticUIObject(item)) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const itemId = getObjectIdKey(item);
|
|
246
|
+
if (itemId !== null && spreadIds.has(itemId)) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
mergedData.splice(Math.min(index, mergedData.length), 0, item);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
return mergedData;
|
|
84
254
|
}
|
|
85
255
|
|
|
86
256
|
/**
|
|
@@ -88,61 +258,72 @@ function isPlainObject(value: any) {
|
|
|
88
258
|
*/
|
|
89
259
|
|
|
90
260
|
function makePrioritizedFieldTree(fields: string[]) {
|
|
91
|
-
|
|
261
|
+
const priorityTree: Record<string, any> = {};
|
|
92
262
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
263
|
+
for (const fieldPath of fields) {
|
|
264
|
+
if (!fieldPath || typeof fieldPath !== 'string') {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
97
267
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
268
|
+
const pathParts = fieldPath.split('.').filter(Boolean);
|
|
269
|
+
if (pathParts.length === 0) {
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
102
272
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
273
|
+
let currentNode = priorityTree;
|
|
274
|
+
for (const part of pathParts) {
|
|
275
|
+
currentNode[part] = currentNode[part] || {};
|
|
276
|
+
currentNode = currentNode[part];
|
|
277
|
+
}
|
|
108
278
|
|
|
109
|
-
|
|
110
|
-
|
|
279
|
+
currentNode.__self = true;
|
|
280
|
+
}
|
|
111
281
|
|
|
112
|
-
|
|
282
|
+
return priorityTree;
|
|
113
283
|
}
|
|
114
284
|
|
|
115
285
|
/**
|
|
116
286
|
* Merge values while prioritizing main fragment data for selected field paths.
|
|
117
287
|
*/
|
|
118
288
|
|
|
119
|
-
function mergeWithPriorityPaths(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
289
|
+
function mergeWithPriorityPaths(
|
|
290
|
+
innerValue: any,
|
|
291
|
+
mainValue: any,
|
|
292
|
+
priorityNode: any,
|
|
293
|
+
) {
|
|
294
|
+
if (!priorityNode) {
|
|
295
|
+
return innerValue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (priorityNode.__self) {
|
|
299
|
+
if (isPlainObject(innerValue) && isPlainObject(mainValue)) {
|
|
300
|
+
return mergeNestedObjects(innerValue, mainValue);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return mainValue;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (!isPlainObject(innerValue) || !isPlainObject(mainValue)) {
|
|
307
|
+
return innerValue;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const mergedValue = { ...innerValue };
|
|
311
|
+
for (const key in priorityNode) {
|
|
312
|
+
if (
|
|
313
|
+
key !== '__self' &&
|
|
314
|
+
Object.prototype.hasOwnProperty.call(priorityNode, key)
|
|
315
|
+
) {
|
|
316
|
+
if (mainValue[key] !== undefined) {
|
|
317
|
+
mergedValue[key] = mergeWithPriorityPaths(
|
|
318
|
+
innerValue[key],
|
|
319
|
+
mainValue[key],
|
|
320
|
+
priorityNode[key],
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return mergedValue;
|
|
146
327
|
}
|
|
147
328
|
|
|
148
329
|
/**
|
|
@@ -150,23 +331,23 @@ function mergeWithPriorityPaths(innerValue: any, mainValue: any, priorityNode: a
|
|
|
150
331
|
*/
|
|
151
332
|
|
|
152
333
|
function getSpreadValueScore(spreadValue: any) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
334
|
+
if (!spreadValue) {
|
|
335
|
+
return 0;
|
|
336
|
+
}
|
|
156
337
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
338
|
+
if (typeof spreadValue === 'string') {
|
|
339
|
+
return 1;
|
|
340
|
+
}
|
|
160
341
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
342
|
+
if (Array.isArray(spreadValue)) {
|
|
343
|
+
return spreadValue.length;
|
|
344
|
+
}
|
|
164
345
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
346
|
+
if (spreadValue?.__list && Array.isArray(spreadValue.data)) {
|
|
347
|
+
return spreadValue.data.length;
|
|
348
|
+
}
|
|
168
349
|
|
|
169
|
-
|
|
350
|
+
return 0;
|
|
170
351
|
}
|
|
171
352
|
|
|
172
353
|
/**
|
|
@@ -174,22 +355,22 @@ function getSpreadValueScore(spreadValue: any) {
|
|
|
174
355
|
*/
|
|
175
356
|
|
|
176
357
|
function mergeSpreadValue(currentValue: any, nextValue: any) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
358
|
+
if (!currentValue) {
|
|
359
|
+
return nextValue;
|
|
360
|
+
}
|
|
180
361
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
362
|
+
if (!nextValue) {
|
|
363
|
+
return currentValue;
|
|
364
|
+
}
|
|
184
365
|
|
|
185
|
-
|
|
186
|
-
|
|
366
|
+
const currentScore = getSpreadValueScore(currentValue);
|
|
367
|
+
const nextScore = getSpreadValueScore(nextValue);
|
|
187
368
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
369
|
+
if (nextScore >= currentScore) {
|
|
370
|
+
return nextValue;
|
|
371
|
+
}
|
|
191
372
|
|
|
192
|
-
|
|
373
|
+
return currentValue;
|
|
193
374
|
}
|
|
194
375
|
|
|
195
376
|
/**
|
|
@@ -197,52 +378,56 @@ function mergeSpreadValue(currentValue: any, nextValue: any) {
|
|
|
197
378
|
*/
|
|
198
379
|
|
|
199
380
|
function mergeSpreads(currentSpreads: any, nextSpreads: any) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
381
|
+
if (!currentSpreads) {
|
|
382
|
+
return nextSpreads;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (!nextSpreads) {
|
|
386
|
+
return currentSpreads;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const mergedSpreads = { ...currentSpreads };
|
|
390
|
+
for (const key in nextSpreads) {
|
|
391
|
+
if (Object.prototype.hasOwnProperty.call(nextSpreads, key)) {
|
|
392
|
+
mergedSpreads[key] = mergeSpreadValue(
|
|
393
|
+
currentSpreads[key],
|
|
394
|
+
nextSpreads[key],
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return mergedSpreads;
|
|
216
400
|
}
|
|
217
401
|
|
|
218
402
|
/**
|
|
219
403
|
* Make Object for fragment, omitting what you don't need
|
|
220
404
|
*/
|
|
221
405
|
|
|
222
|
-
function extractFragment(
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
406
|
+
function extractFragment(
|
|
407
|
+
dataObj: any,
|
|
408
|
+
innerSelection: any[],
|
|
409
|
+
isTest?: boolean,
|
|
410
|
+
) {
|
|
411
|
+
if (innerSelection.length <= 1) {
|
|
412
|
+
return dataObj;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const fragmentObj = { ...dataObj };
|
|
416
|
+
for (const sel of innerSelection) {
|
|
417
|
+
if (sel.kind === 'Field') {
|
|
418
|
+
delete fragmentObj[sel.name.value];
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return fragmentObj;
|
|
235
423
|
}
|
|
236
424
|
|
|
237
425
|
/**
|
|
238
426
|
* Build a stable cache scope for fragment data that may not have an id.
|
|
239
427
|
*/
|
|
240
428
|
|
|
241
|
-
function getFragmentCacheScope(
|
|
242
|
-
|
|
243
|
-
fallbackScope?: string,
|
|
244
|
-
) {
|
|
245
|
-
return data?.id || fallbackScope || 'none';
|
|
429
|
+
function getFragmentCacheScope(data: any, fallbackScope?: string) {
|
|
430
|
+
return data?.id || fallbackScope || 'none';
|
|
246
431
|
}
|
|
247
432
|
|
|
248
433
|
/**
|
|
@@ -250,11 +435,11 @@ function getFragmentCacheScope(
|
|
|
250
435
|
*/
|
|
251
436
|
|
|
252
437
|
function makeFragmentCacheKey(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
438
|
+
fragmentName: string,
|
|
439
|
+
data: any,
|
|
440
|
+
fallbackScope?: string,
|
|
256
441
|
) {
|
|
257
|
-
|
|
442
|
+
return `$${fragmentName}:${getFragmentCacheScope(data, fallbackScope)}`;
|
|
258
443
|
}
|
|
259
444
|
|
|
260
445
|
/**
|
|
@@ -262,66 +447,67 @@ function makeFragmentCacheKey(
|
|
|
262
447
|
*/
|
|
263
448
|
|
|
264
449
|
function mapCachedListItem(
|
|
265
|
-
|
|
266
|
-
|
|
450
|
+
cachedItemData: any[],
|
|
451
|
+
loadFragmentData: (fragmentKey: string) => any,
|
|
267
452
|
) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
453
|
+
let item: Record<string, any> = {};
|
|
454
|
+
let cacheIsValid = true;
|
|
455
|
+
|
|
456
|
+
cachedItemData.forEach((cachedValue: any) => {
|
|
457
|
+
let mapCacheId;
|
|
458
|
+
if (Array.isArray(cachedValue)) {
|
|
459
|
+
if (cachedValue.length === 1 && cachedValue?.[0]?.startsWith('$')) {
|
|
460
|
+
mapCacheId = cachedValue[0];
|
|
461
|
+
} else if (Array.isArray(cachedValue[1])) {
|
|
462
|
+
item[cachedValue[0]] = cachedValue[1].map((value: any) => {
|
|
463
|
+
if (value?.__reactiveFragmentName) {
|
|
464
|
+
const clientReactiveFragmentKey = `${value.__reactiveFragmentName}:${value.id}`;
|
|
465
|
+
return FRAGMENTS.get(clientReactiveFragmentKey) || value;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return value;
|
|
469
|
+
});
|
|
470
|
+
return;
|
|
471
|
+
} else if (cachedValue[1]?.__reactiveFragmentName) {
|
|
472
|
+
const clientReactiveFragmentKey = `${cachedValue[1].__reactiveFragmentName}:${cachedValue[1].id}`;
|
|
473
|
+
item[cachedValue[0]] =
|
|
474
|
+
FRAGMENTS.get(clientReactiveFragmentKey) || cachedValue[1];
|
|
475
|
+
return;
|
|
476
|
+
} else {
|
|
477
|
+
item[cachedValue[0]] = cachedValue[1];
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
} else if (typeof cachedValue === 'string') {
|
|
481
|
+
mapCacheId = cachedValue;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (!mapCacheId) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const frgIndex = mapCacheId.indexOf(':');
|
|
489
|
+
const frgName = mapCacheId.substring(1, frgIndex);
|
|
490
|
+
const frgMap = PARTIALS_MAP[frgName];
|
|
491
|
+
const fragmentKey = frgMap
|
|
492
|
+
? `$${frgMap}:${mapCacheId.substring(frgIndex + 1)}`
|
|
493
|
+
: mapCacheId;
|
|
494
|
+
|
|
495
|
+
const cachedFragment = loadFragmentData(fragmentKey);
|
|
496
|
+
if (cachedFragment) {
|
|
497
|
+
item = {
|
|
498
|
+
...item,
|
|
499
|
+
...cachedFragment,
|
|
500
|
+
};
|
|
501
|
+
} else {
|
|
502
|
+
cacheIsValid = false;
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
if (!cacheIsValid) {
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return item;
|
|
325
511
|
}
|
|
326
512
|
|
|
327
513
|
/**
|
|
@@ -329,48 +515,68 @@ function mapCachedListItem(
|
|
|
329
515
|
*/
|
|
330
516
|
|
|
331
517
|
export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
518
|
+
if (referencesOnly) {
|
|
519
|
+
return QUERIES.get(queryKey);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// Memoized: while the query references and every fragment they touch are
|
|
523
|
+
// unchanged, the exact same assembled object reference is returned.
|
|
524
|
+
return cacheStore.memoizeRead(`Q|${queryKey}`, () =>
|
|
525
|
+
loadQueryInternal(queryKey),
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Assemble one cached query result from its stored fragment references.
|
|
531
|
+
*/
|
|
532
|
+
|
|
533
|
+
function loadQueryInternal(queryKey: string) {
|
|
534
|
+
const references = QUERIES.get(queryKey);
|
|
535
|
+
|
|
536
|
+
if (!references) {
|
|
537
|
+
// This is important, not returning null will cause infinite loop
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// Lets nested spread lists be compared against this query cache entry's own
|
|
542
|
+
// write recency instead of always losing to the flat fragment copy
|
|
543
|
+
const queryVersion = QUERIES.version(queryKey);
|
|
544
|
+
|
|
545
|
+
if (Array.isArray(references)) {
|
|
546
|
+
const mappedCache = references.map((cacheObj) =>
|
|
547
|
+
appendFragmentToCache(cacheObj, false, queryVersion),
|
|
548
|
+
);
|
|
549
|
+
if (mappedCache.some((c) => !c)) {
|
|
550
|
+
return undefined;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// let hasInvalidatedCache = false;
|
|
554
|
+
// const mappedCache = references.map((cacheObj) => {
|
|
555
|
+
// if (!hasInvalidatedCache) {
|
|
556
|
+
// const singleCache = appendFragmentToCache(cacheObj);
|
|
557
|
+
|
|
558
|
+
// if (singleCache) {
|
|
559
|
+
// return singleCache;
|
|
560
|
+
// } else {
|
|
561
|
+
// hasInvalidatedCache = true;
|
|
562
|
+
// }
|
|
563
|
+
// }
|
|
564
|
+
// });
|
|
565
|
+
// if (!hasInvalidatedCache) {
|
|
566
|
+
// return mappedCache;
|
|
567
|
+
// }
|
|
568
|
+
|
|
569
|
+
return mappedCache;
|
|
570
|
+
} else if (references) {
|
|
571
|
+
// console.log('references single:', references);
|
|
572
|
+
const singleCacheData = appendFragmentToCache(
|
|
573
|
+
references,
|
|
574
|
+
true,
|
|
575
|
+
queryVersion,
|
|
576
|
+
);
|
|
577
|
+
// console.log('singleCacheData >>>>:', singleCacheData);
|
|
578
|
+
return singleCacheData;
|
|
579
|
+
}
|
|
374
580
|
}
|
|
375
581
|
|
|
376
582
|
/**
|
|
@@ -378,103 +584,129 @@ export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
|
378
584
|
*/
|
|
379
585
|
|
|
380
586
|
export function loadFragment(id: string, mainPrioritizedFields: string[] = []) {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
587
|
+
// Memoized with automatic dependency tracking: while this fragment, its
|
|
588
|
+
// spreads and every transitively loaded fragment are unchanged, the exact
|
|
589
|
+
// same assembled object reference is returned (structural sharing).
|
|
590
|
+
const memoKey = mainPrioritizedFields.length
|
|
591
|
+
? `${id}|${mainPrioritizedFields.join(',')}`
|
|
592
|
+
: id;
|
|
593
|
+
|
|
594
|
+
return cacheStore.memoizeRead(memoKey, () =>
|
|
595
|
+
loadFragmentInternal(id, mainPrioritizedFields),
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Assemble one fragment with its spreads (uncached implementation).
|
|
601
|
+
*/
|
|
602
|
+
|
|
603
|
+
function loadFragmentInternal(
|
|
604
|
+
id: string,
|
|
605
|
+
mainPrioritizedFields: string[] = [],
|
|
606
|
+
) {
|
|
607
|
+
// console.log('load fragment:', id, FRAGMENTS.get(id));
|
|
608
|
+
|
|
609
|
+
const fragment = enforceArrayType(FRAGMENTS.get(id));
|
|
610
|
+
if (!fragment) {
|
|
611
|
+
return null;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const spreads = FRAGMENTS.get(`${id}.spreads`);
|
|
615
|
+
if (!spreads) {
|
|
616
|
+
return fragment;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// const isTest = id.startsWith('$fieldDocFragment:3789505824043091-4255281447699102-3789105884863193');
|
|
620
|
+
// if (isTest) {
|
|
621
|
+
// ..
|
|
622
|
+
// }
|
|
623
|
+
|
|
624
|
+
let fragmentWithSpreads = { ...fragment };
|
|
625
|
+
const prioritizedFieldTree = makePrioritizedFieldTree(mainPrioritizedFields);
|
|
626
|
+
for (const key in spreads) {
|
|
627
|
+
if (Object.prototype.hasOwnProperty.call(spreads, key)) {
|
|
628
|
+
const spreadValue = spreads[key];
|
|
629
|
+
|
|
630
|
+
// if (isInnerTest) {
|
|
631
|
+
// console.log('SPREAD:', key, '->', spreadValue, typeof spreadValue);
|
|
632
|
+
// }
|
|
633
|
+
|
|
634
|
+
// if (isTest) {
|
|
635
|
+
// console.log('SPREAD:', key, '->', spreadValue, typeof spreadValue);
|
|
636
|
+
// }
|
|
637
|
+
|
|
638
|
+
let spreadData;
|
|
639
|
+
if (typeof spreadValue === 'string') {
|
|
640
|
+
spreadData = enforceArrayType(FRAGMENTS.get(spreadValue));
|
|
641
|
+
} else if (Array.isArray(spreadValue)) {
|
|
642
|
+
spreadData = spreadValue.reduce((acc, value) => {
|
|
643
|
+
// if (isTest) {
|
|
644
|
+
// console.log('????', value);
|
|
645
|
+
// }
|
|
646
|
+
|
|
647
|
+
if (typeof value === 'string') {
|
|
648
|
+
const fragmentData = enforceArrayType(FRAGMENTS.get(value));
|
|
649
|
+
|
|
650
|
+
// if (isTest) {
|
|
651
|
+
// console.log('1:', value, fragmentData);
|
|
652
|
+
// }
|
|
653
|
+
|
|
654
|
+
return {
|
|
655
|
+
...acc,
|
|
656
|
+
...fragmentData,
|
|
657
|
+
};
|
|
658
|
+
} else if (Array.isArray(value)) {
|
|
659
|
+
const [namespace, innerValue] = value;
|
|
660
|
+
return {
|
|
661
|
+
...acc,
|
|
662
|
+
[namespace]: innerValue,
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
return acc;
|
|
667
|
+
}, {});
|
|
668
|
+
} else if (spreadValue?.__list === true) {
|
|
669
|
+
spreadData = spreadValue.data
|
|
670
|
+
.map((val: any) => {
|
|
671
|
+
if (typeof val === 'string') {
|
|
672
|
+
return loadFragment(val);
|
|
673
|
+
} else if (Array.isArray(val)) {
|
|
674
|
+
return mapCachedListItem(val, loadFragment);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
return null;
|
|
678
|
+
})
|
|
679
|
+
.filter(Boolean);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
if (spreadData) {
|
|
683
|
+
const spreadDataWithOptimisticUI =
|
|
684
|
+
Array.isArray(spreadData) && Array.isArray(fragmentWithSpreads[key])
|
|
685
|
+
? mergeSpreadBackedArrayWithOptimisticUIObjects(
|
|
686
|
+
spreadData,
|
|
687
|
+
fragmentWithSpreads[key],
|
|
688
|
+
)
|
|
689
|
+
: spreadData;
|
|
690
|
+
const priorityNode = prioritizedFieldTree[key];
|
|
691
|
+
if (priorityNode && fragmentWithSpreads[key] !== undefined) {
|
|
692
|
+
fragmentWithSpreads[key] = mergeWithPriorityPaths(
|
|
693
|
+
spreadDataWithOptimisticUI,
|
|
694
|
+
fragmentWithSpreads[key],
|
|
695
|
+
priorityNode,
|
|
696
|
+
);
|
|
697
|
+
} else if (fragmentWithSpreads[key] !== undefined) {
|
|
698
|
+
fragmentWithSpreads[key] = mergeNestedObjects(
|
|
699
|
+
fragmentWithSpreads[key],
|
|
700
|
+
spreadDataWithOptimisticUI,
|
|
701
|
+
);
|
|
702
|
+
} else {
|
|
703
|
+
fragmentWithSpreads[key] = spreadDataWithOptimisticUI;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
return fragmentWithSpreads;
|
|
478
710
|
}
|
|
479
711
|
|
|
480
712
|
/**
|
|
@@ -482,232 +714,285 @@ export function loadFragment(id: string, mainPrioritizedFields: string[] = []) {
|
|
|
482
714
|
*/
|
|
483
715
|
|
|
484
716
|
function mapSelections(
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
717
|
+
data: any,
|
|
718
|
+
innerSelections: any[],
|
|
719
|
+
updatedKeys: string[] = [],
|
|
720
|
+
fallbackScope?: string,
|
|
489
721
|
) {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
722
|
+
return innerSelections
|
|
723
|
+
.map((inner) => {
|
|
724
|
+
let fragmentKey;
|
|
725
|
+
let sourceFragmentName: string | undefined;
|
|
726
|
+
if (inner.kind === 'FragmentSpread') {
|
|
727
|
+
// These are fragment spreads
|
|
728
|
+
const frgName = inner.name.value;
|
|
729
|
+
sourceFragmentName = frgName;
|
|
730
|
+
const frgMap = PARTIALS_MAP[frgName];
|
|
731
|
+
|
|
732
|
+
fragmentKey = makeFragmentCacheKey(
|
|
733
|
+
frgMap || frgName,
|
|
734
|
+
data,
|
|
735
|
+
fallbackScope,
|
|
736
|
+
);
|
|
737
|
+
} else if (
|
|
738
|
+
inner.kind === 'Field' &&
|
|
739
|
+
inner.selectionSet &&
|
|
740
|
+
inner.selectionSet.selections?.length
|
|
741
|
+
) {
|
|
742
|
+
// These are nested fragments, they need to be cached as a fragment
|
|
743
|
+
|
|
744
|
+
const innerData = data?.[inner.name.value];
|
|
745
|
+
const innerSelections = inner.selectionSet.selections?.filter(
|
|
746
|
+
(sel: any) => sel.kind === 'FragmentSpread',
|
|
747
|
+
);
|
|
748
|
+
|
|
749
|
+
if (innerData && Array.isArray(innerSelections)) {
|
|
750
|
+
if (Array.isArray(innerData)) {
|
|
751
|
+
const parentScope = getFragmentCacheScope(data, fallbackScope);
|
|
752
|
+
for (const [itemIndex, item] of innerData.entries()) {
|
|
753
|
+
mapSelections(
|
|
754
|
+
item,
|
|
755
|
+
inner.selectionSet.selections,
|
|
756
|
+
updatedKeys,
|
|
757
|
+
`${parentScope}.${inner.name.value}.${itemIndex}`,
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
} else {
|
|
761
|
+
mapSelections(
|
|
762
|
+
innerData,
|
|
763
|
+
inner.selectionSet.selections,
|
|
764
|
+
updatedKeys,
|
|
765
|
+
`${getFragmentCacheScope(data, fallbackScope)}.${inner.name.value}`,
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
} else if (inner.kind === 'InlineFragment') {
|
|
770
|
+
// This is Union, etc
|
|
771
|
+
const frgNamesJoined = inner.selectionSet.selections
|
|
772
|
+
.map((sel: any) => PARTIALS_MAP[sel.name.value] || sel.name.value)
|
|
773
|
+
.join('.');
|
|
774
|
+
|
|
775
|
+
if (
|
|
776
|
+
frgNamesJoined.toLowerCase().indexOf(data.__typename.toLowerCase()) <
|
|
777
|
+
0
|
|
778
|
+
) {
|
|
779
|
+
// This is untested, but should be fine
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
fragmentKey = makeFragmentCacheKey(frgNamesJoined, data, fallbackScope);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
if (fragmentKey) {
|
|
787
|
+
// Whether fragment is partial or not, always merge data;
|
|
788
|
+
// Because partials might have data that's not in the main fragment
|
|
789
|
+
const currentData = loadFragment(fragmentKey);
|
|
790
|
+
|
|
791
|
+
// console.log(fragmentKey);
|
|
792
|
+
// if (fragmentKey.startsWith('$fieldDocFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
793
|
+
// console.log('>>>>');
|
|
794
|
+
// console.log('>>>>');
|
|
795
|
+
// console.log('>>>>');
|
|
796
|
+
// console.log('>>>>');
|
|
797
|
+
// console.log(currentData);
|
|
798
|
+
// console.log(data);
|
|
799
|
+
// console.log(extractFragment(data, innerSelections));
|
|
800
|
+
// }
|
|
801
|
+
|
|
802
|
+
const fragmentFromData = getPartialFragmentMergeData(
|
|
803
|
+
sourceFragmentName,
|
|
804
|
+
extractFragment(data, innerSelections),
|
|
805
|
+
);
|
|
806
|
+
FRAGMENTS.set(
|
|
807
|
+
fragmentKey,
|
|
808
|
+
mergeNestedObjects(
|
|
809
|
+
removeOptimisticUIObjectsFromArrayFields(currentData),
|
|
810
|
+
fragmentFromData,
|
|
811
|
+
),
|
|
812
|
+
);
|
|
813
|
+
updatedKeys.push(fragmentKey);
|
|
814
|
+
|
|
815
|
+
return fragmentKey;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// These are non-fragment fields
|
|
819
|
+
return [inner.name.value, data?.[inner.name.value]];
|
|
820
|
+
// return {
|
|
821
|
+
// name: inner.name.value,
|
|
822
|
+
// value: data[key]?.[inner.name.value]
|
|
823
|
+
// };
|
|
824
|
+
})
|
|
825
|
+
.filter(Boolean);
|
|
574
826
|
}
|
|
575
827
|
|
|
576
828
|
/**
|
|
577
829
|
* Make cache object from data and selection object
|
|
578
830
|
*/
|
|
579
831
|
|
|
580
|
-
function makeCacheObject(
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
832
|
+
function makeCacheObject(
|
|
833
|
+
data: any,
|
|
834
|
+
selections: any[],
|
|
835
|
+
variables?: any,
|
|
836
|
+
cacheMap?: any,
|
|
837
|
+
) {
|
|
838
|
+
const cacheObj: Record<string, any> = {};
|
|
839
|
+
const updatedKeys: string[] = [];
|
|
840
|
+
|
|
841
|
+
for (const sel of selections) {
|
|
842
|
+
const key = sel.name.value;
|
|
843
|
+
if (data[key] === undefined && sel.kind !== 'FragmentSpread') {
|
|
844
|
+
// console.log('~~~', key, sel.kind);
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
switch (sel.kind) {
|
|
849
|
+
case 'Field':
|
|
850
|
+
{
|
|
851
|
+
const innerSelections = sel.selectionSet?.selections;
|
|
852
|
+
if (innerSelections) {
|
|
853
|
+
if (Array.isArray(data[key])) {
|
|
854
|
+
cacheObj[key] = {
|
|
855
|
+
__cache: true,
|
|
856
|
+
__list: true,
|
|
857
|
+
data: data[key].map((item, itemIndex) =>
|
|
858
|
+
mapSelections(
|
|
859
|
+
item,
|
|
860
|
+
innerSelections,
|
|
861
|
+
updatedKeys,
|
|
862
|
+
`${key}.${itemIndex}`,
|
|
863
|
+
),
|
|
864
|
+
),
|
|
865
|
+
};
|
|
866
|
+
} else {
|
|
867
|
+
cacheObj[key] = {
|
|
868
|
+
__cache: true,
|
|
869
|
+
data: mapSelections(
|
|
870
|
+
data[key],
|
|
871
|
+
innerSelections,
|
|
872
|
+
updatedKeys,
|
|
873
|
+
key,
|
|
874
|
+
),
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
} else {
|
|
878
|
+
cacheObj[key] = data[key];
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
break;
|
|
882
|
+
case 'FragmentSpread':
|
|
883
|
+
{
|
|
884
|
+
const frgName = sel.name.value;
|
|
885
|
+
const frgMap = PARTIALS_MAP[frgName];
|
|
886
|
+
const fragmentKey = makeFragmentCacheKey(frgMap || frgName, data);
|
|
887
|
+
|
|
888
|
+
if (RULES[frgName] !== false) {
|
|
889
|
+
// if (testMode) {
|
|
890
|
+
// console.log('Array.isArray(data)');
|
|
891
|
+
// console.log(Array.isArray(data));
|
|
892
|
+
// console.log(data);
|
|
893
|
+
// console.log(extractFragment(data, selections));
|
|
894
|
+
// }
|
|
895
|
+
|
|
896
|
+
// Whether fragment is partial or not, always merge data;
|
|
897
|
+
// Because partials might have data that's not in the main fragment
|
|
898
|
+
const currentData = loadFragment(fragmentKey);
|
|
899
|
+
|
|
900
|
+
// if (fragmentKey.startsWith('$fieldDocFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
901
|
+
// console.log('>>>>');
|
|
902
|
+
// console.log('>>>>');
|
|
903
|
+
// console.log('>>>>');
|
|
904
|
+
// console.log('>>>>');
|
|
905
|
+
// console.log(currentData);
|
|
906
|
+
// console.log(data);
|
|
907
|
+
// }
|
|
908
|
+
|
|
909
|
+
// console.log('>>>>', fragmentKey);
|
|
910
|
+
FRAGMENTS.set(
|
|
911
|
+
fragmentKey,
|
|
912
|
+
mergeNestedObjects(
|
|
913
|
+
removeOptimisticUIObjectsFromArrayFields(currentData),
|
|
914
|
+
getPartialFragmentMergeData(
|
|
915
|
+
frgName,
|
|
916
|
+
extractFragment(data, selections),
|
|
917
|
+
),
|
|
918
|
+
),
|
|
919
|
+
);
|
|
920
|
+
updatedKeys.push(fragmentKey);
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
cacheObj.__flat = {
|
|
924
|
+
__cache: true,
|
|
925
|
+
data: [fragmentKey],
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
break;
|
|
929
|
+
default:
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// If there's a cache map, return a custom value
|
|
933
|
+
// This will make the cache object NOT get set into the query cache
|
|
934
|
+
// This means that cache IS handled but the values will be invalid
|
|
935
|
+
// so cacheMap has to handle the cache object itself
|
|
936
|
+
|
|
937
|
+
if (cacheMap?.[key]) {
|
|
938
|
+
const customCacheData = cacheMap[key](
|
|
939
|
+
data[key],
|
|
940
|
+
cacheObj[key],
|
|
941
|
+
sel,
|
|
942
|
+
variables,
|
|
943
|
+
);
|
|
944
|
+
cacheObj[key] =
|
|
945
|
+
typeof customCacheData === 'undefined' ? null : customCacheData;
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
if (cacheObj.__flat?.data?.[0]) {
|
|
951
|
+
const spreadsFragmentKey = `${cacheObj.__flat.data[0]}.spreads`;
|
|
952
|
+
|
|
953
|
+
let spreads: Record<string, any> | undefined;
|
|
954
|
+
for (const cKey in cacheObj) {
|
|
955
|
+
if (
|
|
956
|
+
cKey !== '__flat' &&
|
|
957
|
+
Object.prototype.hasOwnProperty.call(cacheObj, cKey)
|
|
958
|
+
) {
|
|
959
|
+
if (!spreads) {
|
|
960
|
+
spreads = {};
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
// Spreads here
|
|
964
|
+
|
|
965
|
+
if (
|
|
966
|
+
cacheObj[cKey]?.data?.length === 1 &&
|
|
967
|
+
typeof cacheObj[cKey]?.data?.[0] === 'string'
|
|
968
|
+
) {
|
|
969
|
+
// Single spread with single fragment
|
|
970
|
+
spreads[cKey] = cacheObj[cKey].data[0];
|
|
971
|
+
} else if (cacheObj[cKey].__list === true) {
|
|
972
|
+
// List of spreads
|
|
973
|
+
spreads[cKey] = {
|
|
974
|
+
__list: true,
|
|
975
|
+
data: cacheObj[cKey].data,
|
|
976
|
+
};
|
|
977
|
+
} else if (Array.isArray(cacheObj[cKey]?.data)) {
|
|
978
|
+
// Spread with more spreads inside
|
|
979
|
+
spreads[cKey] = cacheObj[cKey].data;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
if (spreads) {
|
|
985
|
+
// if (spreadsFragmentKey.startsWith('$emailFragment:')) {
|
|
986
|
+
// console.log('>>>>', spreadsFragmentKey);
|
|
987
|
+
// console.log(data);
|
|
988
|
+
// console.log(spreads);
|
|
989
|
+
// }
|
|
990
|
+
const currentSpreads = FRAGMENTS.get(spreadsFragmentKey);
|
|
991
|
+
FRAGMENTS.set(spreadsFragmentKey, mergeSpreads(currentSpreads, spreads));
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
return [cacheObj, updatedKeys];
|
|
711
996
|
}
|
|
712
997
|
|
|
713
998
|
/**
|
|
@@ -715,90 +1000,106 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
715
1000
|
*/
|
|
716
1001
|
|
|
717
1002
|
function saveQueryToMemory(
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
1003
|
+
requestData: any,
|
|
1004
|
+
selection: any,
|
|
1005
|
+
variables: any,
|
|
1006
|
+
variablesKey: string,
|
|
1007
|
+
cacheMap?: any,
|
|
1008
|
+
saveQueryToCache?: boolean,
|
|
1009
|
+
updateObservers?: UpdateObserversFn,
|
|
725
1010
|
) {
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
1011
|
+
const qryName = getGraphQLSelectionResponseName(selection);
|
|
1012
|
+
if (!requestData) {
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
// IMPORTANT NOTE:
|
|
1017
|
+
// Queries are never cached unless they are manually set in [./config.ts]
|
|
1018
|
+
|
|
1019
|
+
const data = requestData[qryName];
|
|
1020
|
+
const queryId = `#${qryName}:${variablesKey}`;
|
|
1021
|
+
const selections = selection?.selectionSet?.selections;
|
|
1022
|
+
|
|
1023
|
+
if (!selections) {
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
if (data) {
|
|
1028
|
+
// console.log('=====');
|
|
1029
|
+
// console.log(requestData);
|
|
1030
|
+
// console.log(queryId);
|
|
1031
|
+
// console.log(data);
|
|
1032
|
+
// console.log('=====');
|
|
1033
|
+
|
|
1034
|
+
const cacheMapObj = typeof cacheMap === 'function' ? null : cacheMap;
|
|
1035
|
+
|
|
1036
|
+
let cacheData;
|
|
1037
|
+
let updatedFragmentIds;
|
|
1038
|
+
if (Array.isArray(data)) {
|
|
1039
|
+
const result = data.map((dataObj) =>
|
|
1040
|
+
makeCacheObject(dataObj, selections, variables, cacheMapObj),
|
|
1041
|
+
);
|
|
1042
|
+
cacheData = result.map((r) => r[0]);
|
|
1043
|
+
updatedFragmentIds = result.reduce((acc, r) => acc.concat(r[1]), []);
|
|
1044
|
+
} else {
|
|
1045
|
+
[cacheData, updatedFragmentIds] = makeCacheObject(
|
|
1046
|
+
data,
|
|
1047
|
+
selections,
|
|
1048
|
+
variables,
|
|
1049
|
+
cacheMapObj,
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
// console.log(updatedFragmentIds);
|
|
1054
|
+
// console.log(cacheData);
|
|
1055
|
+
|
|
1056
|
+
if (typeof cacheMap === 'function') {
|
|
1057
|
+
// cacheMap[key](data[key], cacheObj[key], sel, variables);
|
|
1058
|
+
if (saveQueryToCache) {
|
|
1059
|
+
const customCacheData = cacheMap(
|
|
1060
|
+
data,
|
|
1061
|
+
cacheData,
|
|
1062
|
+
selections,
|
|
1063
|
+
variables,
|
|
1064
|
+
);
|
|
1065
|
+
// console.log('1--->', queryId);
|
|
1066
|
+
// console.log('UPDATING (1):', typeof customCacheData === 'undefined' ? null : customCacheData);
|
|
1067
|
+
QUERIES.set(
|
|
1068
|
+
queryId,
|
|
1069
|
+
typeof customCacheData === 'undefined' ? null : customCacheData,
|
|
1070
|
+
);
|
|
1071
|
+
cacheStore.setQueryFetchTime(queryId, Date.now());
|
|
1072
|
+
|
|
1073
|
+
if (updateObservers) {
|
|
1074
|
+
updateObservers({
|
|
1075
|
+
queryId,
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
} else {
|
|
1080
|
+
if (saveQueryToCache) {
|
|
1081
|
+
// console.log('2--->', queryId);
|
|
1082
|
+
// console.log('2--->', cacheData);
|
|
1083
|
+
// console.log('UPDATING (2):', cacheData);
|
|
1084
|
+
QUERIES.set(queryId, cacheData);
|
|
1085
|
+
cacheStore.setQueryFetchTime(queryId, Date.now());
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
if (updateObservers) {
|
|
1089
|
+
updateObservers({
|
|
1090
|
+
fragmentIds: updatedFragmentIds as string[],
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
// I don't think observer is needed for custom cache maps, but if it does, bring it up
|
|
1095
|
+
// FRAGMENT_OBSERVER.value = {
|
|
1096
|
+
// count: FRAGMENT_OBSERVER.value.count + 1,
|
|
1097
|
+
// list: updatedFragmentIds,
|
|
1098
|
+
// };
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
return updatedFragmentIds;
|
|
1102
|
+
}
|
|
802
1103
|
}
|
|
803
1104
|
|
|
804
1105
|
/**
|
|
@@ -810,168 +1111,205 @@ function saveQueryToMemory(
|
|
|
810
1111
|
*/
|
|
811
1112
|
|
|
812
1113
|
export function setDataToCache(
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
1114
|
+
requestData: any,
|
|
1115
|
+
gqlQuery: any,
|
|
1116
|
+
variables?: any,
|
|
1117
|
+
cacheMap?: any,
|
|
1118
|
+
updateObservers?: UpdateObserversFn,
|
|
818
1119
|
) {
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
1120
|
+
// query[variablesKey] = cache;
|
|
1121
|
+
const definitions = gqlQuery.definitions;
|
|
1122
|
+
const variablesKey = makeVariablesKey(variables);
|
|
1123
|
+
|
|
1124
|
+
for (const def of definitions) {
|
|
1125
|
+
if (def.kind === 'OperationDefinition') {
|
|
1126
|
+
def?.selectionSet?.selections.forEach((selection: any) => {
|
|
1127
|
+
const qryName = selection.name?.value;
|
|
1128
|
+
saveQueryToMemory(
|
|
1129
|
+
requestData,
|
|
1130
|
+
selection,
|
|
1131
|
+
variables,
|
|
1132
|
+
variablesKey,
|
|
1133
|
+
cacheMap,
|
|
1134
|
+
RULES[qryName] !== false,
|
|
1135
|
+
updateObservers,
|
|
1136
|
+
);
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
839
1140
|
}
|
|
840
1141
|
|
|
841
1142
|
/**
|
|
842
1143
|
* Append fragment to cache
|
|
843
1144
|
*/
|
|
844
1145
|
|
|
845
|
-
export function appendFragmentToCache(
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
1146
|
+
export function appendFragmentToCache(
|
|
1147
|
+
cacheData: any,
|
|
1148
|
+
testMode?: boolean,
|
|
1149
|
+
queryVersion = 0,
|
|
1150
|
+
) {
|
|
1151
|
+
let newObj = cloneArrayLike(cacheData);
|
|
1152
|
+
let flatObj: any;
|
|
1153
|
+
const flatSpreadKeys = new Set<string>();
|
|
1154
|
+
|
|
1155
|
+
// Keys assembled from a list stored in the query cache, plus how recently the
|
|
1156
|
+
// flat fragment's own spread copy was written. Both sides can hold the newest
|
|
1157
|
+
// list (a mutation writes the fragment spread; append/prepend writes grow the
|
|
1158
|
+
// query list), so the more recent write wins. Version stamps come from one
|
|
1159
|
+
// monotonic counter, which makes them directly comparable.
|
|
1160
|
+
const queryListKeys = new Set<string>();
|
|
1161
|
+
let flatSpreadVersion = 0;
|
|
1162
|
+
|
|
1163
|
+
// if (testMode) {
|
|
1164
|
+
// console.log('appendFragmentToCache:', newObj);
|
|
1165
|
+
// }
|
|
1166
|
+
|
|
1167
|
+
for (const key in newObj) {
|
|
1168
|
+
// if (testMode) {
|
|
1169
|
+
// console.log('******* key:', key);
|
|
1170
|
+
// console.log('******* ??:', newObj[key]);
|
|
1171
|
+
// }
|
|
1172
|
+
|
|
1173
|
+
if (newObj[key] && typeof newObj[key] === 'object') {
|
|
1174
|
+
const isFlat = key === '__flat';
|
|
1175
|
+
const isCache = newObj[key].__cache && newObj[key].data;
|
|
1176
|
+
|
|
1177
|
+
if (isCache) {
|
|
1178
|
+
let cachedObj: any = {};
|
|
1179
|
+
let cacheIsValid = true;
|
|
1180
|
+
|
|
1181
|
+
if (newObj[key].__list) {
|
|
1182
|
+
if (!isFlat) {
|
|
1183
|
+
queryListKeys.add(key);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
cachedObj = newObj[key].data.map((di: any[]) => {
|
|
1187
|
+
const item = mapCachedListItem(di, loadFragment);
|
|
1188
|
+
if (!item) {
|
|
1189
|
+
cacheIsValid = false;
|
|
1190
|
+
return {};
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
return item;
|
|
1194
|
+
});
|
|
1195
|
+
} else {
|
|
1196
|
+
newObj[key].data.forEach((d: any) => {
|
|
1197
|
+
// if (testMode) {
|
|
1198
|
+
// console.log('cachedObj', cachedObj);
|
|
1199
|
+
// console.log('key', key);
|
|
1200
|
+
// console.log('d', d);
|
|
1201
|
+
// console.log('d', typeof d);
|
|
1202
|
+
// }
|
|
1203
|
+
|
|
1204
|
+
if (Array.isArray(d)) {
|
|
1205
|
+
cachedObj[d[0]] = d[1];
|
|
1206
|
+
} else if (typeof d === 'string') {
|
|
1207
|
+
const frgIndex = d.indexOf(':');
|
|
1208
|
+
const frgName = d.substring(1, frgIndex);
|
|
1209
|
+
const frgMap = PARTIALS_MAP[frgName];
|
|
1210
|
+
|
|
1211
|
+
let fragmentKey;
|
|
1212
|
+
if (frgMap) {
|
|
1213
|
+
fragmentKey = `$${frgMap}:${d.substring(frgIndex + 1)}`;
|
|
1214
|
+
} else {
|
|
1215
|
+
fragmentKey = d;
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
const cachedFragment = loadFragment(fragmentKey);
|
|
1219
|
+
|
|
1220
|
+
if (cachedFragment) {
|
|
1221
|
+
if (isFlat) {
|
|
1222
|
+
// if (testMode) {
|
|
1223
|
+
// console.log('///////');
|
|
1224
|
+
// console.log('///////');
|
|
1225
|
+
// console.log('newObj before:', newObj);
|
|
1226
|
+
// console.log('fragmentKey:', fragmentKey);
|
|
1227
|
+
// console.log('cachedFragment:', cachedFragment);
|
|
1228
|
+
// }
|
|
1229
|
+
|
|
1230
|
+
// newObj = {
|
|
1231
|
+
// ...newObj,
|
|
1232
|
+
// ...cachedFragment,
|
|
1233
|
+
// };
|
|
1234
|
+
|
|
1235
|
+
flatObj = cachedFragment;
|
|
1236
|
+
flatSpreadVersion = Math.max(
|
|
1237
|
+
FRAGMENTS.version(fragmentKey),
|
|
1238
|
+
FRAGMENTS.version(`${fragmentKey}.spreads`),
|
|
1239
|
+
);
|
|
1240
|
+
Object.keys(
|
|
1241
|
+
FRAGMENTS.get(`${fragmentKey}.spreads`) || {},
|
|
1242
|
+
).forEach((spreadKey) => {
|
|
1243
|
+
flatSpreadKeys.add(spreadKey);
|
|
1244
|
+
});
|
|
1245
|
+
delete newObj.__flat;
|
|
1246
|
+
} else {
|
|
1247
|
+
cachedObj = {
|
|
1248
|
+
...cachedObj,
|
|
1249
|
+
...cachedFragment,
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
} else {
|
|
1253
|
+
// Cache was invalidated
|
|
1254
|
+
cacheIsValid = false;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
if (!cacheIsValid) {
|
|
1261
|
+
return null;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
if (!isFlat) {
|
|
1265
|
+
newObj[key] = cachedObj;
|
|
1266
|
+
}
|
|
1267
|
+
} else {
|
|
1268
|
+
// Fragment spreads recursively loads the fragment data
|
|
1269
|
+
// So we have to get the non-spread fields from query cache
|
|
1270
|
+
|
|
1271
|
+
const cacheInfo = cacheData[key];
|
|
1272
|
+
if (
|
|
1273
|
+
newObj[key] &&
|
|
1274
|
+
typeof newObj[key] === 'object' &&
|
|
1275
|
+
cacheInfo?.__cache &&
|
|
1276
|
+
cacheInfo.data
|
|
1277
|
+
) {
|
|
1278
|
+
cacheInfo.data.forEach((di: any) => {
|
|
1279
|
+
if (
|
|
1280
|
+
Array.isArray(di) &&
|
|
1281
|
+
di.length === 2 &&
|
|
1282
|
+
!di?.[0]?.startsWith('$')
|
|
1283
|
+
) {
|
|
1284
|
+
newObj[key][di[0]] = di[1];
|
|
1285
|
+
}
|
|
1286
|
+
});
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
// Merge flat data
|
|
1293
|
+
|
|
1294
|
+
if (flatObj) {
|
|
1295
|
+
for (const key in flatObj) {
|
|
1296
|
+
// console.log('FLAT:', key, flatObj[key], newObj[key]);
|
|
1297
|
+
// Spread-backed fields can be refreshed by mutation writes even when the query cache is stale.
|
|
1298
|
+
// A query-cache list is the one contested case: it holds every item appended
|
|
1299
|
+
// since the fragment was written, so the flat copy may only replace it when
|
|
1300
|
+
// that copy is the newer write. Otherwise the list truncates back to
|
|
1301
|
+
// whatever the last full response happened to contain.
|
|
1302
|
+
const flatCopyWins =
|
|
1303
|
+
flatSpreadKeys.has(key) &&
|
|
1304
|
+
(!queryListKeys.has(key) || flatSpreadVersion > queryVersion);
|
|
1305
|
+
|
|
1306
|
+
if (newObj[key] === undefined || flatCopyWins) {
|
|
1307
|
+
newObj[key] = flatObj[key];
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
return newObj;
|
|
975
1313
|
}
|
|
976
1314
|
|
|
977
1315
|
/**
|
|
@@ -979,91 +1317,100 @@ export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
|
|
|
979
1317
|
*/
|
|
980
1318
|
|
|
981
1319
|
export function loadDataFromCache(
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1320
|
+
requestData: any | null,
|
|
1321
|
+
gqlQuery: any,
|
|
1322
|
+
variablesKey: string,
|
|
1323
|
+
cacheMap?: any,
|
|
1324
|
+
eagerFragmentKeyMap?: any,
|
|
1325
|
+
allowQueryCacheFallback = true,
|
|
988
1326
|
) {
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1327
|
+
// return query[variablesKey];
|
|
1328
|
+
const data = { ...requestData };
|
|
1329
|
+
const definitions = gqlQuery.definitions;
|
|
1330
|
+
|
|
1331
|
+
for (const def of definitions) {
|
|
1332
|
+
if (def.kind === 'OperationDefinition') {
|
|
1333
|
+
def?.selectionSet?.selections.forEach((selection: any) => {
|
|
1334
|
+
const qryName = getGraphQLSelectionResponseName(selection);
|
|
1335
|
+
if (!data[qryName] && allowQueryCacheFallback) {
|
|
1336
|
+
const cacheKey = `#${qryName}:${variablesKey}`;
|
|
1337
|
+
const cachedData = loadQuery(cacheKey);
|
|
1338
|
+
|
|
1339
|
+
if (cachedData) {
|
|
1340
|
+
data[qryName] = cachedData;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
// const cacheData = QUERIES.get(cacheKey);
|
|
1344
|
+
|
|
1345
|
+
// if (Array.isArray(cacheData)) {
|
|
1346
|
+
// let hasInvalidatedCache = false;
|
|
1347
|
+
// const mappedCache = cacheData.map((cacheObj, i) => {
|
|
1348
|
+
// if (!hasInvalidatedCache) {
|
|
1349
|
+
// const singleCache = appendFragmentToCache(cacheObj);
|
|
1350
|
+
|
|
1351
|
+
// if (singleCache) {
|
|
1352
|
+
// return singleCache;
|
|
1353
|
+
// } else {
|
|
1354
|
+
// hasInvalidatedCache = true;
|
|
1355
|
+
// }
|
|
1356
|
+
// }
|
|
1357
|
+
// });
|
|
1358
|
+
|
|
1359
|
+
// if (!hasInvalidatedCache) {
|
|
1360
|
+
// data[qryName] = mappedCache;
|
|
1361
|
+
// }
|
|
1362
|
+
// } else if (cacheData) {
|
|
1363
|
+
// const singleCacheData = appendFragmentToCache(cacheData);
|
|
1364
|
+
|
|
1365
|
+
// if (singleCacheData) {
|
|
1366
|
+
// data[qryName] = singleCacheData;
|
|
1367
|
+
// }
|
|
1368
|
+
// }
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
// Set value to "true" because this is meant to be handled by a cacheMap and its standalone logic
|
|
1372
|
+
|
|
1373
|
+
if (data[qryName]) {
|
|
1374
|
+
if (typeof cacheMap === 'function') {
|
|
1375
|
+
data[qryName] = true;
|
|
1376
|
+
} else if (cacheMap) {
|
|
1377
|
+
for (const key in cacheMap) {
|
|
1378
|
+
if (
|
|
1379
|
+
cacheMap?.[key] &&
|
|
1380
|
+
data[qryName]?.[key] !== undefined &&
|
|
1381
|
+
data[qryName][key] !== true
|
|
1382
|
+
) {
|
|
1383
|
+
// Clone before stamping; the source object may be a shared
|
|
1384
|
+
// memoized cache assembly that must stay unmutated.
|
|
1385
|
+
data[qryName] = {
|
|
1386
|
+
...data[qryName],
|
|
1387
|
+
[key]: true,
|
|
1388
|
+
};
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
} else if (eagerFragmentKeyMap?.[qryName]) {
|
|
1393
|
+
const fragmentKey = eagerFragmentKeyMap[qryName];
|
|
1394
|
+
const cachedFragment = loadFragment(fragmentKey);
|
|
1395
|
+
|
|
1396
|
+
// if (fragmentKey.startsWith('$fieldDocFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
1397
|
+
// console.log('>>>>');
|
|
1398
|
+
// console.log('>>>>');
|
|
1399
|
+
// console.log('>>>>');
|
|
1400
|
+
// console.log('>>>>');
|
|
1401
|
+
// console.log(cachedFragment);
|
|
1402
|
+
// console.log(data);
|
|
1403
|
+
// }
|
|
1404
|
+
|
|
1405
|
+
if (cachedFragment) {
|
|
1406
|
+
data[qryName] = cachedFragment;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
return data;
|
|
1067
1414
|
}
|
|
1068
1415
|
|
|
1069
1416
|
/**
|
|
@@ -1071,25 +1418,29 @@ export function loadDataFromCache(
|
|
|
1071
1418
|
*/
|
|
1072
1419
|
|
|
1073
1420
|
export function checkDataCompleteness(requestData: any, gqlQuery: any) {
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1421
|
+
if (!requestData) {
|
|
1422
|
+
return false;
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
const definitions = gqlQuery.definitions;
|
|
1426
|
+
for (const def of definitions) {
|
|
1427
|
+
if (def.kind === 'OperationDefinition') {
|
|
1428
|
+
const selections = def?.selectionSet?.selections;
|
|
1429
|
+
for (const selection of selections) {
|
|
1430
|
+
const qryName = getGraphQLSelectionResponseName(selection);
|
|
1431
|
+
const value = requestData[qryName];
|
|
1432
|
+
// An explicit null is a complete server answer (e.g. a deleted or
|
|
1433
|
+
// invalid id); only a missing key means the field was never fetched.
|
|
1434
|
+
// This must match hasCompleteSelectionSetData()'s null handling, or
|
|
1435
|
+
// useQuery() refetches null results forever.
|
|
1436
|
+
if (value === undefined) {
|
|
1437
|
+
return false;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
return true;
|
|
1093
1444
|
}
|
|
1094
1445
|
|
|
1095
1446
|
/**
|
|
@@ -1097,38 +1448,48 @@ export function checkDataCompleteness(requestData: any, gqlQuery: any) {
|
|
|
1097
1448
|
*/
|
|
1098
1449
|
|
|
1099
1450
|
export function removeStaleCache() {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1451
|
+
// function getByteSize(oMap){
|
|
1452
|
+
// function replacer(key, value) {
|
|
1453
|
+
// if(value instanceof Map) {
|
|
1454
|
+
// return {
|
|
1455
|
+
// dataType: 'Map',
|
|
1456
|
+
// value: [...value],
|
|
1457
|
+
// };
|
|
1458
|
+
// } else {
|
|
1459
|
+
// return value;
|
|
1460
|
+
// }
|
|
1461
|
+
// }
|
|
1462
|
+
// return JSON.stringify(oMap, replacer).length;
|
|
1463
|
+
// }
|
|
1464
|
+
|
|
1465
|
+
// console.log(' Queries cache size (count):', QUERIES.size);
|
|
1466
|
+
// console.log(' Queries cache size (bytes):', getByteSize(QUERIES));
|
|
1467
|
+
// console.log('Fragments cache size (count):', FRAGMENTS.size);
|
|
1468
|
+
// console.log('Fragments cache size (bytes):', getByteSize(FRAGMENTS));
|
|
1469
|
+
|
|
1470
|
+
if (FRAGMENTS.size > RULES.__limit) {
|
|
1471
|
+
const numToEvict = FRAGMENTS.size - RULES.__limit;
|
|
1472
|
+
|
|
1473
|
+
// Least-recently-accessed fragments go first; a fragment and its spreads
|
|
1474
|
+
// sidecar are always evicted together.
|
|
1475
|
+
const lruKeys = FRAGMENTS.keysByLeastRecentAccess(
|
|
1476
|
+
(key) => !key.endsWith('.spreads'),
|
|
1477
|
+
);
|
|
1478
|
+
|
|
1479
|
+
let evictedCount = 0;
|
|
1480
|
+
for (const key of lruKeys) {
|
|
1481
|
+
if (evictedCount >= numToEvict) {
|
|
1482
|
+
break;
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
FRAGMENTS.delete(key);
|
|
1486
|
+
evictedCount += 1;
|
|
1487
|
+
|
|
1488
|
+
if (FRAGMENTS.delete(`${key}.spreads`)) {
|
|
1489
|
+
evictedCount += 1;
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1132
1493
|
}
|
|
1133
1494
|
|
|
1134
1495
|
/**
|
|
@@ -1136,28 +1497,28 @@ export function removeStaleCache() {
|
|
|
1136
1497
|
*/
|
|
1137
1498
|
|
|
1138
1499
|
function makeFragmentDefinitionMap(definitions: any[]) {
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1500
|
+
return definitions.reduce(
|
|
1501
|
+
(acc, def) => {
|
|
1502
|
+
if (def.kind === 'FragmentDefinition' && def.name?.value) {
|
|
1503
|
+
acc[def.name.value] = def;
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
return acc;
|
|
1507
|
+
},
|
|
1508
|
+
{} as Record<string, any>,
|
|
1509
|
+
);
|
|
1146
1510
|
}
|
|
1147
1511
|
|
|
1148
1512
|
/**
|
|
1149
1513
|
* Check whether an inline fragment applies to the current cached object type.
|
|
1150
1514
|
*/
|
|
1151
|
-
function inlineFragmentMatchesCachedType(
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
if (!typeName || !value?.__typename) {
|
|
1157
|
-
return true;
|
|
1158
|
-
}
|
|
1515
|
+
function inlineFragmentMatchesCachedType(value: any, selection: any) {
|
|
1516
|
+
const typeName = selection.typeCondition?.name?.value;
|
|
1517
|
+
if (!typeName || !value?.__typename) {
|
|
1518
|
+
return true;
|
|
1519
|
+
}
|
|
1159
1520
|
|
|
1160
|
-
|
|
1521
|
+
return value.__typename === typeName;
|
|
1161
1522
|
}
|
|
1162
1523
|
|
|
1163
1524
|
/**
|
|
@@ -1165,67 +1526,67 @@ function inlineFragmentMatchesCachedType(
|
|
|
1165
1526
|
*/
|
|
1166
1527
|
|
|
1167
1528
|
function hasCompleteSelectionData(
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1529
|
+
value: any,
|
|
1530
|
+
selection: any,
|
|
1531
|
+
fragmentDefinitions: Record<string, any>,
|
|
1171
1532
|
): boolean {
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1533
|
+
if (value === null) {
|
|
1534
|
+
return true;
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
if (value === undefined) {
|
|
1538
|
+
return false;
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
if (selection.kind === 'FragmentSpread') {
|
|
1542
|
+
const fragmentDef = fragmentDefinitions[selection.name?.value];
|
|
1543
|
+
return fragmentDef
|
|
1544
|
+
? hasCompleteSelectionSetData(
|
|
1545
|
+
value,
|
|
1546
|
+
fragmentDef.selectionSet?.selections || [],
|
|
1547
|
+
fragmentDefinitions,
|
|
1548
|
+
)
|
|
1549
|
+
: true;
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
if (selection.kind === 'InlineFragment') {
|
|
1553
|
+
if (!inlineFragmentMatchesCachedType(value, selection)) {
|
|
1554
|
+
return true;
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
return hasCompleteSelectionSetData(
|
|
1558
|
+
value,
|
|
1559
|
+
selection.selectionSet?.selections || [],
|
|
1560
|
+
fragmentDefinitions,
|
|
1561
|
+
);
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
if (selection.kind !== 'Field') {
|
|
1565
|
+
return true;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
const fieldName = getGraphQLSelectionResponseName(selection);
|
|
1569
|
+
const fieldValue = value?.[fieldName];
|
|
1570
|
+
if (fieldValue === undefined) {
|
|
1571
|
+
return false;
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
const selections = selection.selectionSet?.selections;
|
|
1575
|
+
if (!selections || fieldValue === null) {
|
|
1576
|
+
return true;
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
if (Array.isArray(fieldValue)) {
|
|
1580
|
+
return fieldValue.every((item) =>
|
|
1581
|
+
hasCompleteSelectionSetData(item, selections, fragmentDefinitions),
|
|
1582
|
+
);
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
return hasCompleteSelectionSetData(
|
|
1586
|
+
fieldValue,
|
|
1587
|
+
selections,
|
|
1588
|
+
fragmentDefinitions,
|
|
1589
|
+
);
|
|
1229
1590
|
}
|
|
1230
1591
|
|
|
1231
1592
|
/**
|
|
@@ -1233,78 +1594,136 @@ function hasCompleteSelectionData(
|
|
|
1233
1594
|
*/
|
|
1234
1595
|
|
|
1235
1596
|
function hasCompleteSelectionSetData(
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1597
|
+
value: any,
|
|
1598
|
+
selections: any[],
|
|
1599
|
+
fragmentDefinitions: Record<string, any>,
|
|
1239
1600
|
): boolean {
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1601
|
+
if (value === null) {
|
|
1602
|
+
return true;
|
|
1603
|
+
}
|
|
1243
1604
|
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1605
|
+
if (value === undefined) {
|
|
1606
|
+
return false;
|
|
1607
|
+
}
|
|
1247
1608
|
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1609
|
+
return selections.every((selection) =>
|
|
1610
|
+
hasCompleteSelectionData(value, selection, fragmentDefinitions),
|
|
1611
|
+
);
|
|
1251
1612
|
}
|
|
1252
1613
|
|
|
1253
1614
|
/**
|
|
1254
1615
|
* Check if there is cache
|
|
1255
1616
|
*/
|
|
1256
1617
|
|
|
1257
|
-
export function fetchCachedData(
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1618
|
+
export function fetchCachedData(
|
|
1619
|
+
gqlQuery: any,
|
|
1620
|
+
variablesKey: string,
|
|
1621
|
+
_updateObservers?: UpdateObserversFn,
|
|
1622
|
+
) {
|
|
1623
|
+
const definitions = gqlQuery.definitions;
|
|
1624
|
+
const fragmentDefinitions = makeFragmentDefinitionMap(definitions);
|
|
1625
|
+
const queryDocumentCacheId = getQueryDocumentCacheId(gqlQuery);
|
|
1626
|
+
|
|
1627
|
+
for (const def of definitions) {
|
|
1628
|
+
if (def.kind === 'OperationDefinition') {
|
|
1629
|
+
const selections = def?.selectionSet?.selections;
|
|
1630
|
+
for (const selection of selections) {
|
|
1631
|
+
const qryName = getGraphQLSelectionResponseName(selection);
|
|
1632
|
+
const queryId = `#${qryName}:${variablesKey}`;
|
|
1633
|
+
|
|
1634
|
+
// Completeness validation walks the full selected result tree. Cache
|
|
1635
|
+
// that validated snapshot against the normalized query/fragment
|
|
1636
|
+
// versions so warm route mounts reuse both the work and reference.
|
|
1637
|
+
return cacheStore.memoizeRead(
|
|
1638
|
+
`VQ|${queryDocumentCacheId}|${queryId}`,
|
|
1639
|
+
() => {
|
|
1640
|
+
const cachedData = loadQuery(queryId);
|
|
1641
|
+
if (cachedData === null) {
|
|
1642
|
+
return null;
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
if (!cachedData) {
|
|
1646
|
+
return null;
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
if (
|
|
1650
|
+
!hasCompleteSelectionData(
|
|
1651
|
+
{ [qryName]: cachedData },
|
|
1652
|
+
selection,
|
|
1653
|
+
fragmentDefinitions,
|
|
1654
|
+
)
|
|
1655
|
+
) {
|
|
1656
|
+
return null;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
return { [qryName]: cachedData };
|
|
1660
|
+
},
|
|
1661
|
+
);
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
return null;
|
|
1667
|
+
}
|
|
1282
1668
|
|
|
1283
|
-
|
|
1284
|
-
|
|
1669
|
+
/**
|
|
1670
|
+
* Clear all cache
|
|
1671
|
+
*/
|
|
1285
1672
|
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1673
|
+
export function clearGraphQLClientCache() {
|
|
1674
|
+
cacheStore.clearAll();
|
|
1675
|
+
RESET_TIME.clear();
|
|
1676
|
+
clearGraphQLCachePersistence();
|
|
1289
1677
|
|
|
1290
|
-
|
|
1291
|
-
|
|
1678
|
+
// Locally persisted drafts hold content the user typed; a shared machine
|
|
1679
|
+
// must not keep it after sign-out or an access change
|
|
1680
|
+
clearAllDrafts();
|
|
1681
|
+
}
|
|
1292
1682
|
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1683
|
+
/**
|
|
1684
|
+
* Return when one query's cached data last came from the network (ms epoch),
|
|
1685
|
+
* or undefined when it never did in this session or persisted snapshot.
|
|
1686
|
+
*/
|
|
1297
1687
|
|
|
1298
|
-
|
|
1688
|
+
export function getQueryLastFetchedAt(queryKey: string): number | undefined {
|
|
1689
|
+
return cacheStore.getQueryFetchTime(queryKey);
|
|
1299
1690
|
}
|
|
1300
1691
|
|
|
1301
1692
|
/**
|
|
1302
|
-
*
|
|
1693
|
+
* Build a combined version stamp for fragment keys (and their spreads).
|
|
1694
|
+
* Hooks compare stamps to skip rebuilding merged data when an observer
|
|
1695
|
+
* notification did not actually change any observed fragment.
|
|
1303
1696
|
*/
|
|
1304
1697
|
|
|
1305
|
-
export function
|
|
1306
|
-
|
|
1307
|
-
|
|
1698
|
+
export function getFragmentVersionStamp(fragmentKeys: string[]): string {
|
|
1699
|
+
return cacheStore.getFragmentVersionStamp(fragmentKeys);
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
/*
|
|
1703
|
+
* Return the query observer count a hook should acknowledge after a network
|
|
1704
|
+
* attempt. External force-refetch observers must be acknowledged even when no
|
|
1705
|
+
* cached result existed before the request, otherwise the hook sees the same
|
|
1706
|
+
* observer count again and repeats the request.
|
|
1707
|
+
*/
|
|
1708
|
+
export function getNextQueryLastUpdatedCount(
|
|
1709
|
+
currentLastUpdatedCount: number,
|
|
1710
|
+
observedUpdatedCount: number,
|
|
1711
|
+
ownQueryObserverCount: number,
|
|
1712
|
+
) {
|
|
1713
|
+
const acknowledgedUpdatedCount = Math.max(
|
|
1714
|
+
currentLastUpdatedCount || 0,
|
|
1715
|
+
observedUpdatedCount || 0,
|
|
1716
|
+
);
|
|
1717
|
+
|
|
1718
|
+
if (!ownQueryObserverCount) {
|
|
1719
|
+
return acknowledgedUpdatedCount;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
return Math.max(
|
|
1723
|
+
acknowledgedUpdatedCount,
|
|
1724
|
+
(observedUpdatedCount || currentLastUpdatedCount || 0) +
|
|
1725
|
+
ownQueryObserverCount,
|
|
1726
|
+
);
|
|
1308
1727
|
}
|
|
1309
1728
|
|
|
1310
1729
|
/**
|
|
@@ -1312,16 +1731,16 @@ export function clearGraphQLClientCache() {
|
|
|
1312
1731
|
*/
|
|
1313
1732
|
|
|
1314
1733
|
export function getQueryRefreshTime(queryName: string, variablesKey: string) {
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1734
|
+
// if (RESET_TIME.get(`#${queryName}:`) || RESET_TIME.get(`#${queryName}:${variablesKey}`)) {
|
|
1735
|
+
// console.log(`#${queryName}:${variablesKey}`);
|
|
1736
|
+
// console.log('1 >>>>>>>', RESET_TIME.get(`#${queryName}:`));
|
|
1737
|
+
// console.log('2 >>>>>>>', RESET_TIME.get(`#${queryName}:${variablesKey}`));
|
|
1738
|
+
// }
|
|
1739
|
+
|
|
1740
|
+
return (
|
|
1741
|
+
RESET_TIME.get(`#${queryName}:`) ||
|
|
1742
|
+
RESET_TIME.get(`#${queryName}:${variablesKey}`)
|
|
1743
|
+
);
|
|
1325
1744
|
}
|
|
1326
1745
|
|
|
1327
1746
|
/**
|
|
@@ -1329,8 +1748,8 @@ export function getQueryRefreshTime(queryName: string, variablesKey: string) {
|
|
|
1329
1748
|
*/
|
|
1330
1749
|
|
|
1331
1750
|
export function clearQueryResetStatus(queryName: string, variablesKey: string) {
|
|
1332
|
-
|
|
1333
|
-
|
|
1751
|
+
RESET_TIME.delete(`#${queryName}:`);
|
|
1752
|
+
RESET_TIME.delete(`#${queryName}:${variablesKey}`);
|
|
1334
1753
|
}
|
|
1335
1754
|
|
|
1336
1755
|
/**
|
|
@@ -1338,30 +1757,69 @@ export function clearQueryResetStatus(queryName: string, variablesKey: string) {
|
|
|
1338
1757
|
*/
|
|
1339
1758
|
|
|
1340
1759
|
function deleteMatchingQueryCache(queryId: string) {
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1760
|
+
if (queryId.endsWith(':')) {
|
|
1761
|
+
for (const key of QUERIES.keys()) {
|
|
1762
|
+
if (key.startsWith(queryId)) {
|
|
1763
|
+
QUERIES.delete(key);
|
|
1764
|
+
console.dev('✅ RESET ONLY ->', key);
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
return;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
QUERIES.delete(queryId);
|
|
1771
|
+
console.dev('✅ RESET ONLY ->', queryId);
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
/**
|
|
1775
|
+
* Clear query cache for one structured reset target.
|
|
1776
|
+
*
|
|
1777
|
+
* An unconstrained target is exactly the plain '#queryName:' reset key and is
|
|
1778
|
+
* routed through it deliberately: that path records the reset against the query
|
|
1779
|
+
* name, so a page mounting later still observes it. Only a target that narrows
|
|
1780
|
+
* on variables walks the matching cached pages.
|
|
1781
|
+
*/
|
|
1782
|
+
|
|
1783
|
+
export function resetQueryByTarget(
|
|
1784
|
+
target: QueryResetTarget,
|
|
1785
|
+
forceRefetch?: boolean,
|
|
1786
|
+
updateObservers?: UpdateObserversFn,
|
|
1787
|
+
resetOnly?: boolean,
|
|
1788
|
+
) {
|
|
1789
|
+
if (!target.match || !Object.keys(target.match).length) {
|
|
1790
|
+
resetQuery(`#${target.name}:`, forceRefetch, updateObservers, resetOnly);
|
|
1791
|
+
return;
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
if (resetOnly) {
|
|
1795
|
+
for (const key of QUERIES.keys()) {
|
|
1796
|
+
if (queryCacheKeyMatchesResetTarget(key, target)) {
|
|
1797
|
+
QUERIES.delete(key);
|
|
1798
|
+
console.dev('✅ RESET ONLY ->', key);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
updateObservers?.({
|
|
1803
|
+
queryId: getQueryResetTargetId(target),
|
|
1804
|
+
queryTarget: target,
|
|
1805
|
+
forceRefetch,
|
|
1806
|
+
resetOnly,
|
|
1807
|
+
});
|
|
1808
|
+
return;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
if (updateObservers) {
|
|
1812
|
+
for (const key of QUERIES.keys()) {
|
|
1813
|
+
if (queryCacheKeyMatchesResetTarget(key, target)) {
|
|
1814
|
+
RESET_TIME.set(key, Date.now());
|
|
1815
|
+
console.dev('✅ RESET ->', key);
|
|
1816
|
+
updateObservers({
|
|
1817
|
+
queryId: key,
|
|
1818
|
+
forceRefetch,
|
|
1819
|
+
});
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1365
1823
|
}
|
|
1366
1824
|
|
|
1367
1825
|
/**
|
|
@@ -1370,48 +1828,36 @@ function deleteMatchingQueryCache(queryId: string) {
|
|
|
1370
1828
|
* it will reset all queries everywhere for the same GraphQL query.
|
|
1371
1829
|
*/
|
|
1372
1830
|
|
|
1373
|
-
export function resetQuery(
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
}
|
|
1404
|
-
} else {
|
|
1405
|
-
// Reset specific query
|
|
1406
|
-
RESET_TIME.set(queryId, Date.now());
|
|
1407
|
-
console.dev('✅ RESET ->', queryId);
|
|
1408
|
-
updateObservers({
|
|
1409
|
-
queryId,
|
|
1410
|
-
forceRefetch,
|
|
1411
|
-
});
|
|
1412
|
-
}
|
|
1413
|
-
// console.log('SETTING ->', queryId, RESET_TIME.get(queryId));
|
|
1414
|
-
}
|
|
1831
|
+
export function resetQuery(
|
|
1832
|
+
queryId: string,
|
|
1833
|
+
forceRefetch?: boolean,
|
|
1834
|
+
updateObservers?: UpdateObserversFn,
|
|
1835
|
+
resetOnly?: boolean,
|
|
1836
|
+
) {
|
|
1837
|
+
// if (forceRefetch) {
|
|
1838
|
+
// QUERIES.delete(queryId);
|
|
1839
|
+
// }
|
|
1840
|
+
|
|
1841
|
+
if (resetOnly) {
|
|
1842
|
+
deleteMatchingQueryCache(queryId);
|
|
1843
|
+
updateObservers?.({
|
|
1844
|
+
queryId,
|
|
1845
|
+
forceRefetch,
|
|
1846
|
+
resetOnly,
|
|
1847
|
+
});
|
|
1848
|
+
return;
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
if (updateObservers) {
|
|
1852
|
+
// Reset one exact cache key or an unconstrained query prefix
|
|
1853
|
+
RESET_TIME.set(queryId, Date.now());
|
|
1854
|
+
console.dev('✅ RESET ->', queryId);
|
|
1855
|
+
updateObservers({
|
|
1856
|
+
queryId,
|
|
1857
|
+
forceRefetch,
|
|
1858
|
+
});
|
|
1859
|
+
// console.log('SETTING ->', queryId, RESET_TIME.get(queryId));
|
|
1860
|
+
}
|
|
1415
1861
|
}
|
|
1416
1862
|
|
|
1417
1863
|
/**
|
|
@@ -1419,50 +1865,57 @@ export function resetQuery(queryId: string, forceRefetch?: boolean, updateObserv
|
|
|
1419
1865
|
*/
|
|
1420
1866
|
|
|
1421
1867
|
export function updateFragment(
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1868
|
+
updateFragmentKey: string,
|
|
1869
|
+
update: any | ((cache: any) => any),
|
|
1870
|
+
replacementFragmentKey?: string | null,
|
|
1871
|
+
doNotTriggerObserver?: boolean,
|
|
1872
|
+
updateObservers?: UpdateObserversFn,
|
|
1873
|
+
otherUpdatedFragmentKeys?: string[],
|
|
1428
1874
|
) {
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1875
|
+
const cache = loadFragment(updateFragmentKey);
|
|
1876
|
+
|
|
1877
|
+
if (cache && update) {
|
|
1878
|
+
const cacheWithoutOptimisticUI =
|
|
1879
|
+
removeOptimisticUIObjectsFromArrayFields(cache);
|
|
1880
|
+
let updateObj;
|
|
1881
|
+
if (typeof update === 'function') {
|
|
1882
|
+
updateObj = update(mapArrayLikeObjects(cacheWithoutOptimisticUI));
|
|
1883
|
+
} else {
|
|
1884
|
+
updateObj = update;
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
const newUpdatedObj = { ...cacheWithoutOptimisticUI, ...updateObj };
|
|
1888
|
+
|
|
1889
|
+
FRAGMENTS.set(updateFragmentKey, newUpdatedObj);
|
|
1890
|
+
console.dev('✅ FRAGMENT UPDATED ->', updateFragmentKey, newUpdatedObj);
|
|
1891
|
+
|
|
1892
|
+
if (
|
|
1893
|
+
replacementFragmentKey &&
|
|
1894
|
+
replacementFragmentKey !== updateFragmentKey
|
|
1895
|
+
) {
|
|
1896
|
+
FRAGMENTS.set(replacementFragmentKey, newUpdatedObj);
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
if (!doNotTriggerObserver && updateObservers) {
|
|
1900
|
+
let observingFragmentIds = [updateFragmentKey];
|
|
1901
|
+
if (Array.isArray(otherUpdatedFragmentKeys)) {
|
|
1902
|
+
observingFragmentIds = uniq(
|
|
1903
|
+
observingFragmentIds.concat(otherUpdatedFragmentKeys),
|
|
1904
|
+
);
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
updateObservers({
|
|
1908
|
+
fragmentIds: observingFragmentIds,
|
|
1909
|
+
});
|
|
1910
|
+
|
|
1911
|
+
// FRAGMENT_OBSERVER.value = {
|
|
1912
|
+
// count: FRAGMENT_OBSERVER.value.count + 1,
|
|
1913
|
+
// list: [updateFragmentKey],
|
|
1914
|
+
// };
|
|
1915
|
+
}
|
|
1916
|
+
return updateFragmentKey || null;
|
|
1917
|
+
}
|
|
1918
|
+
return null;
|
|
1466
1919
|
}
|
|
1467
1920
|
|
|
1468
1921
|
/**
|
|
@@ -1470,33 +1923,35 @@ export function updateFragment(
|
|
|
1470
1923
|
*/
|
|
1471
1924
|
|
|
1472
1925
|
export function updateQuery(
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1926
|
+
queryId: string,
|
|
1927
|
+
update: any | ((cache: any) => any),
|
|
1928
|
+
doNotTriggerObserver?: boolean,
|
|
1929
|
+
updateObservers?: UpdateObserversFn,
|
|
1477
1930
|
) {
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1931
|
+
const cache = loadQuery(queryId, true);
|
|
1932
|
+
if (cache && update) {
|
|
1933
|
+
let updateObj;
|
|
1934
|
+
if (typeof update === 'function') {
|
|
1935
|
+
updateObj = update(mapArrayLikeObjects(cache));
|
|
1936
|
+
} else {
|
|
1937
|
+
updateObj = update;
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
const newUpdatedObj = Array.isArray(updateObj)
|
|
1941
|
+
? updateObj
|
|
1942
|
+
: { ...cache, ...updateObj };
|
|
1943
|
+
|
|
1944
|
+
// console.log('UPDATING (3):', updateObj);
|
|
1945
|
+
QUERIES.set(queryId, newUpdatedObj);
|
|
1946
|
+
|
|
1947
|
+
if (!doNotTriggerObserver && updateObservers) {
|
|
1948
|
+
updateObservers({
|
|
1949
|
+
queryId,
|
|
1950
|
+
});
|
|
1951
|
+
}
|
|
1952
|
+
return updateObj || null;
|
|
1953
|
+
}
|
|
1954
|
+
return null;
|
|
1500
1955
|
}
|
|
1501
1956
|
|
|
1502
1957
|
/**
|
|
@@ -1505,17 +1960,22 @@ export function updateQuery(
|
|
|
1505
1960
|
* This will make observer NOT work.
|
|
1506
1961
|
*/
|
|
1507
1962
|
|
|
1508
|
-
export function triggerObserver(
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1963
|
+
export function triggerObserver(
|
|
1964
|
+
ids: (string | null)[],
|
|
1965
|
+
updateObservers?: UpdateObserversFn,
|
|
1966
|
+
) {
|
|
1967
|
+
if (updateObservers) {
|
|
1968
|
+
const observerListIds = ids.filter((id) => id);
|
|
1969
|
+
if (observerListIds.length) {
|
|
1970
|
+
updateObservers({
|
|
1971
|
+
fragmentIds: observerListIds,
|
|
1972
|
+
});
|
|
1973
|
+
}
|
|
1974
|
+
} else {
|
|
1975
|
+
console.warn(
|
|
1976
|
+
'trigerObserver() was called without updateObservers(); This does nothing.',
|
|
1977
|
+
);
|
|
1978
|
+
}
|
|
1519
1979
|
}
|
|
1520
1980
|
|
|
1521
1981
|
/**
|
|
@@ -1523,5 +1983,5 @@ export function triggerObserver(ids: (string | null)[], updateObservers?: Update
|
|
|
1523
1983
|
*/
|
|
1524
1984
|
|
|
1525
1985
|
export function addFragmentToCache(id: string, obj: any) {
|
|
1526
|
-
|
|
1986
|
+
FRAGMENTS.set(id, obj);
|
|
1527
1987
|
}
|