@jsb188/graphql 1.1.7 → 1.1.10
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 +14 -4
- package/cache/index.test.ts +319 -0
- package/cache/index.ts +522 -85
- package/client/index.ts +2 -2
- package/client/request.ts +48 -9
- package/deno.test.json +6 -0
- package/gql/fragments/accountFragments.ts +1 -0
- package/gql/fragments/aiChatFragments.ts +1 -0
- package/gql/fragments/dataTableFragments.ts +212 -0
- package/gql/fragments/emailFragments.ts +26 -0
- package/gql/fragments/inboundContactFragments.ts +15 -0
- package/gql/fragments/integrationFragments.ts +22 -0
- package/gql/fragments/logFragments.ts +40 -12
- package/gql/fragments/organizationFragments.ts +59 -24
- package/gql/fragments/productFragments.ts +0 -24
- package/gql/fragments/reportFragments.ts +52 -10
- package/gql/fragments/sheetFragments.ts +145 -0
- package/gql/fragments/storageFragments.ts +3 -1
- package/gql/fragments/workflowFragments.ts +43 -0
- package/gql/mutations/accountMutations.ts +24 -2
- package/gql/mutations/aiChatMutations.ts +28 -1
- package/gql/mutations/authMutations.ts +3 -3
- package/gql/mutations/billingMutations.ts +1 -1
- package/gql/mutations/dataTableMutations.ts +84 -0
- package/gql/mutations/inboundContactMutations.ts +24 -0
- package/gql/mutations/index.ts +12 -8
- package/gql/mutations/integrationMutations.ts +25 -0
- package/gql/mutations/logMutations.ts +15 -1
- package/gql/mutations/organizationMutations.ts +151 -2
- package/gql/mutations/sheetMutations.ts +168 -0
- package/gql/mutations/storageMutations.ts +1 -2
- package/gql/queries/aiChatQueries.ts +1 -1
- package/gql/queries/billingQueries.ts +1 -1
- package/gql/queries/dataTableQueries.ts +45 -0
- package/gql/queries/emailQueries.ts +44 -0
- package/gql/queries/inboundContactQueries.ts +40 -0
- package/gql/queries/index.ts +11 -6
- package/gql/queries/logQueries.ts +47 -20
- package/gql/queries/organizationQueries.ts +62 -8
- package/gql/queries/productQueries.ts +1 -40
- package/gql/queries/reportQueries.ts +81 -13
- package/gql/queries/sheetQueries.ts +72 -0
- package/gql/queries/workflowQueries.ts +30 -0
- package/hooks/index.ts +208 -58
- package/hooks/use-account-mtn.ts +52 -4
- package/hooks/use-aiChat-mtn.ts +62 -6
- package/hooks/use-aiChat-qry.ts +76 -24
- package/hooks/use-app-qry.ts +2 -2
- package/hooks/use-auth-mtn.ts +5 -5
- package/hooks/use-dataTable-mtn.ts +87 -0
- package/hooks/use-dataTable-qry.ts +172 -0
- package/hooks/use-email-qry.ts +112 -0
- package/hooks/use-inboundContact-mtn.ts +31 -0
- package/hooks/use-inboundContact-qry.ts +88 -0
- package/hooks/use-integration-mtn.ts +59 -0
- package/hooks/use-integration-qry.ts +15 -0
- package/hooks/use-log-mtn.ts +6 -6
- package/hooks/use-log-qry.ts +38 -35
- package/hooks/use-organization-mtn.ts +288 -16
- package/hooks/use-organization-qry.ts +71 -22
- package/hooks/use-product-qry.ts +4 -46
- package/hooks/use-report-qry.ts +94 -10
- package/hooks/use-sheet-mtn.ts +181 -0
- package/hooks/use-sheet-qry.ts +157 -0
- package/hooks/use-storage-mtn.ts +6 -5
- package/hooks/use-storage-qry.ts +1 -1
- package/hooks/use-workflow-qry.ts +72 -0
- package/package.json +23 -4
- package/sse.ts +2 -2
- package/tsconfig.json +12 -0
- package/types.d.ts +2 -1
- package/utils.ts +2 -4
- package/gql/fragments/actionFragments.ts +0 -16
package/cache/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { makeVariablesKey } from '@jsb188/app/utils/logic';
|
|
2
|
-
import { cloneArrayLike, mapArrayLikeObjects, uniq } from '@jsb188/app/utils/object';
|
|
3
|
-
import type { UpdateObserversFn } from '../types.d';
|
|
4
|
-
import { PARTIALS_MAP, RULES } from './config';
|
|
1
|
+
import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
|
|
2
|
+
import { cloneArrayLike, mapArrayLikeObjects, uniq } from '@jsb188/app/utils/object.ts';
|
|
3
|
+
import type { UpdateObserversFn } from '../types.d.ts';
|
|
4
|
+
import { PARTIALS_MAP, RULES } from './config.ts';
|
|
5
5
|
|
|
6
6
|
const QUERIES = new Map();
|
|
7
7
|
const FRAGMENTS = new Map();
|
|
@@ -41,7 +41,23 @@ function enforceArrayType(obj: any): any {
|
|
|
41
41
|
*/
|
|
42
42
|
|
|
43
43
|
function mergeNestedObjects(obj1: any, obj2: any) {
|
|
44
|
+
if (Array.isArray(obj1) || Array.isArray(obj2)) {
|
|
45
|
+
if (Array.isArray(obj2)) {
|
|
46
|
+
return [...obj2];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (Array.isArray(obj1)) {
|
|
50
|
+
return [...obj1];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return obj2;
|
|
54
|
+
}
|
|
55
|
+
|
|
44
56
|
if (!obj1) {
|
|
57
|
+
if (!obj2 || typeof obj2 !== 'object') {
|
|
58
|
+
return obj2;
|
|
59
|
+
}
|
|
60
|
+
|
|
45
61
|
return { ...obj2 };
|
|
46
62
|
}
|
|
47
63
|
|
|
@@ -59,6 +75,146 @@ function mergeNestedObjects(obj1: any, obj2: any) {
|
|
|
59
75
|
return newObj;
|
|
60
76
|
}
|
|
61
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Check whether a value is a plain object.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
function isPlainObject(value: any) {
|
|
83
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Build priority tree from dot-path fields.
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
function makePrioritizedFieldTree(fields: string[]) {
|
|
91
|
+
const priorityTree: Record<string, any> = {};
|
|
92
|
+
|
|
93
|
+
for (const fieldPath of fields) {
|
|
94
|
+
if (!fieldPath || typeof fieldPath !== 'string') {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const pathParts = fieldPath.split('.').filter(Boolean);
|
|
99
|
+
if (pathParts.length === 0) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let currentNode = priorityTree;
|
|
104
|
+
for (const part of pathParts) {
|
|
105
|
+
currentNode[part] = currentNode[part] || {};
|
|
106
|
+
currentNode = currentNode[part];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
currentNode.__self = true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return priorityTree;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Merge values while prioritizing main fragment data for selected field paths.
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
function mergeWithPriorityPaths(innerValue: any, mainValue: any, priorityNode: any) {
|
|
120
|
+
if (!priorityNode) {
|
|
121
|
+
return innerValue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (priorityNode.__self) {
|
|
125
|
+
if (isPlainObject(innerValue) && isPlainObject(mainValue)) {
|
|
126
|
+
return mergeNestedObjects(innerValue, mainValue);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return mainValue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!isPlainObject(innerValue) || !isPlainObject(mainValue)) {
|
|
133
|
+
return innerValue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const mergedValue = { ...innerValue };
|
|
137
|
+
for (const key in priorityNode) {
|
|
138
|
+
if (key !== '__self' && Object.prototype.hasOwnProperty.call(priorityNode, key)) {
|
|
139
|
+
if (mainValue[key] !== undefined) {
|
|
140
|
+
mergedValue[key] = mergeWithPriorityPaths(innerValue[key], mainValue[key], priorityNode[key]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return mergedValue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Get spread value richness score by counting known mapped entries.
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
function getSpreadValueScore(spreadValue: any) {
|
|
153
|
+
if (!spreadValue) {
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (typeof spreadValue === 'string') {
|
|
158
|
+
return 1;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (Array.isArray(spreadValue)) {
|
|
162
|
+
return spreadValue.length;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (spreadValue?.__list && Array.isArray(spreadValue.data)) {
|
|
166
|
+
return spreadValue.data.length;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Merge two spread values while preferring richer existing spread metadata.
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
function mergeSpreadValue(currentValue: any, nextValue: any) {
|
|
177
|
+
if (!currentValue) {
|
|
178
|
+
return nextValue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (!nextValue) {
|
|
182
|
+
return currentValue;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const currentScore = getSpreadValueScore(currentValue);
|
|
186
|
+
const nextScore = getSpreadValueScore(nextValue);
|
|
187
|
+
|
|
188
|
+
if (nextScore >= currentScore) {
|
|
189
|
+
return nextValue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return currentValue;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Merge spread maps so partial mutation payloads do not drop nested cached fields.
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
function mergeSpreads(currentSpreads: any, nextSpreads: any) {
|
|
200
|
+
if (!currentSpreads) {
|
|
201
|
+
return nextSpreads;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (!nextSpreads) {
|
|
205
|
+
return currentSpreads;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const mergedSpreads = { ...currentSpreads };
|
|
209
|
+
for (const key in nextSpreads) {
|
|
210
|
+
if (Object.prototype.hasOwnProperty.call(nextSpreads, key)) {
|
|
211
|
+
mergedSpreads[key] = mergeSpreadValue(currentSpreads[key], nextSpreads[key]);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return mergedSpreads;
|
|
216
|
+
}
|
|
217
|
+
|
|
62
218
|
/**
|
|
63
219
|
* Make Object for fragment, omitting what you don't need
|
|
64
220
|
*/
|
|
@@ -78,6 +234,96 @@ function extractFragment(dataObj: any, innerSelection: any[], isTest?: boolean)
|
|
|
78
234
|
return fragmentObj;
|
|
79
235
|
}
|
|
80
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Build a stable cache scope for fragment data that may not have an id.
|
|
239
|
+
*/
|
|
240
|
+
|
|
241
|
+
function getFragmentCacheScope(
|
|
242
|
+
data: any,
|
|
243
|
+
fallbackScope?: string,
|
|
244
|
+
) {
|
|
245
|
+
return data?.id || fallbackScope || 'none';
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Build a fragment cache key using id when available, or a scoped fallback.
|
|
250
|
+
*/
|
|
251
|
+
|
|
252
|
+
function makeFragmentCacheKey(
|
|
253
|
+
fragmentName: string,
|
|
254
|
+
data: any,
|
|
255
|
+
fallbackScope?: string,
|
|
256
|
+
) {
|
|
257
|
+
return `$${fragmentName}:${getFragmentCacheScope(data, fallbackScope)}`;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Rebuild one cached list item from fragment refs and inline field tuples.
|
|
262
|
+
*/
|
|
263
|
+
|
|
264
|
+
function mapCachedListItem(
|
|
265
|
+
cachedItemData: any[],
|
|
266
|
+
loadFragmentData: (fragmentKey: string) => any,
|
|
267
|
+
) {
|
|
268
|
+
let item: Record<string, any> = {};
|
|
269
|
+
let cacheIsValid = true;
|
|
270
|
+
|
|
271
|
+
cachedItemData.forEach((cachedValue: any) => {
|
|
272
|
+
let mapCacheId;
|
|
273
|
+
if (Array.isArray(cachedValue)) {
|
|
274
|
+
if (cachedValue.length === 1 && cachedValue?.[0]?.startsWith('$')) {
|
|
275
|
+
mapCacheId = cachedValue[0];
|
|
276
|
+
} else if (Array.isArray(cachedValue[1])) {
|
|
277
|
+
item[cachedValue[0]] = cachedValue[1].map((value: any) => {
|
|
278
|
+
if (value?.__reactiveFragmentName) {
|
|
279
|
+
const clientReactiveFragmentKey = `${value.__reactiveFragmentName}:${value.id}`;
|
|
280
|
+
return FRAGMENTS.get(clientReactiveFragmentKey) || value;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return value;
|
|
284
|
+
});
|
|
285
|
+
return;
|
|
286
|
+
} else if (cachedValue[1]?.__reactiveFragmentName) {
|
|
287
|
+
const clientReactiveFragmentKey = `${cachedValue[1].__reactiveFragmentName}:${cachedValue[1].id}`;
|
|
288
|
+
item[cachedValue[0]] = FRAGMENTS.get(clientReactiveFragmentKey) || cachedValue[1];
|
|
289
|
+
return;
|
|
290
|
+
} else {
|
|
291
|
+
item[cachedValue[0]] = cachedValue[1];
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
} else if (typeof cachedValue === 'string') {
|
|
295
|
+
mapCacheId = cachedValue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (!mapCacheId) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const frgIndex = mapCacheId.indexOf(':');
|
|
303
|
+
const frgName = mapCacheId.substring(1, frgIndex);
|
|
304
|
+
const frgMap = PARTIALS_MAP[frgName];
|
|
305
|
+
const fragmentKey = frgMap
|
|
306
|
+
? `$${frgMap}:${mapCacheId.substring(frgIndex + 1)}`
|
|
307
|
+
: mapCacheId;
|
|
308
|
+
|
|
309
|
+
const cachedFragment = loadFragmentData(fragmentKey);
|
|
310
|
+
if (cachedFragment) {
|
|
311
|
+
item = {
|
|
312
|
+
...item,
|
|
313
|
+
...cachedFragment,
|
|
314
|
+
};
|
|
315
|
+
} else {
|
|
316
|
+
cacheIsValid = false;
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
if (!cacheIsValid) {
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return item;
|
|
325
|
+
}
|
|
326
|
+
|
|
81
327
|
/**
|
|
82
328
|
* Load query cache
|
|
83
329
|
*/
|
|
@@ -131,7 +377,7 @@ export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
|
131
377
|
* Load fragment cache
|
|
132
378
|
*/
|
|
133
379
|
|
|
134
|
-
export function loadFragment(id: string) {
|
|
380
|
+
export function loadFragment(id: string, mainPrioritizedFields: string[] = []) {
|
|
135
381
|
// console.log('load fragment:', id, FRAGMENTS.get(id));
|
|
136
382
|
|
|
137
383
|
const fragment = enforceArrayType(FRAGMENTS.get(id));
|
|
@@ -144,19 +390,24 @@ export function loadFragment(id: string) {
|
|
|
144
390
|
return fragment;
|
|
145
391
|
}
|
|
146
392
|
|
|
147
|
-
// const isTest = id.startsWith('$
|
|
393
|
+
// const isTest = id.startsWith('$reportFragment:3789505824043091-4255281447699102-3789105884863193');
|
|
148
394
|
// if (isTest) {
|
|
149
395
|
// ..
|
|
150
396
|
// }
|
|
151
397
|
|
|
152
398
|
let fragmentWithSpreads = { ...fragment };
|
|
399
|
+
const prioritizedFieldTree = makePrioritizedFieldTree(mainPrioritizedFields);
|
|
153
400
|
for (const key in spreads) {
|
|
154
401
|
|
|
155
402
|
if (Object.prototype.hasOwnProperty.call(spreads, key)) {
|
|
156
403
|
const spreadValue = spreads[key];
|
|
157
404
|
|
|
405
|
+
// if (isInnerTest) {
|
|
406
|
+
// console.log('SPREAD:', key, '->', spreadValue, typeof spreadValue);
|
|
407
|
+
// }
|
|
408
|
+
|
|
158
409
|
// if (isTest) {
|
|
159
|
-
// console.log('SPREAD:', key, '->', spreadValue);
|
|
410
|
+
// console.log('SPREAD:', key, '->', spreadValue, typeof spreadValue);
|
|
160
411
|
// }
|
|
161
412
|
|
|
162
413
|
let spreadData;
|
|
@@ -164,10 +415,16 @@ export function loadFragment(id: string) {
|
|
|
164
415
|
spreadData = enforceArrayType(FRAGMENTS.get(spreadValue));
|
|
165
416
|
} else if (Array.isArray(spreadValue)) {
|
|
166
417
|
spreadData = spreadValue.reduce((acc, value) => {
|
|
418
|
+
|
|
419
|
+
// if (isTest) {
|
|
420
|
+
// console.log('????', value);
|
|
421
|
+
// }
|
|
422
|
+
|
|
423
|
+
|
|
167
424
|
if (typeof value === 'string') {
|
|
168
425
|
const fragmentData = enforceArrayType(FRAGMENTS.get(value));
|
|
169
426
|
|
|
170
|
-
// if (isTest
|
|
427
|
+
// if (isTest) {
|
|
171
428
|
// console.log('1:', value, fragmentData);
|
|
172
429
|
// }
|
|
173
430
|
|
|
@@ -177,8 +434,6 @@ export function loadFragment(id: string) {
|
|
|
177
434
|
};
|
|
178
435
|
} else if (Array.isArray(value)) {
|
|
179
436
|
const [namespace, innerValue] = value;
|
|
180
|
-
// console.log('2:', namespace, value);
|
|
181
|
-
|
|
182
437
|
return {
|
|
183
438
|
...acc,
|
|
184
439
|
[namespace]: innerValue,
|
|
@@ -189,24 +444,32 @@ export function loadFragment(id: string) {
|
|
|
189
444
|
}, {});
|
|
190
445
|
} else if (spreadValue?.__list === true) {
|
|
191
446
|
spreadData = spreadValue.data.map((val: any) => {
|
|
192
|
-
|
|
193
|
-
// NOTE: I haven't fully finished this work,
|
|
194
|
-
// but it should work for most cases.
|
|
195
|
-
|
|
196
447
|
if (typeof val === 'string') {
|
|
197
448
|
return loadFragment(val);
|
|
198
|
-
} else if (Array.isArray(val)
|
|
199
|
-
return
|
|
449
|
+
} else if (Array.isArray(val)) {
|
|
450
|
+
return mapCachedListItem(val, loadFragment);
|
|
200
451
|
}
|
|
201
452
|
|
|
202
|
-
// Unfinished work
|
|
203
|
-
console.warn('Unfinished GraphQL cache scenario!: ', val)
|
|
204
453
|
return null;
|
|
205
454
|
}).filter(Boolean);
|
|
206
455
|
}
|
|
207
456
|
|
|
208
457
|
if (spreadData) {
|
|
209
|
-
|
|
458
|
+
const priorityNode = prioritizedFieldTree[key];
|
|
459
|
+
if (priorityNode && fragmentWithSpreads[key] !== undefined) {
|
|
460
|
+
fragmentWithSpreads[key] = mergeWithPriorityPaths(
|
|
461
|
+
spreadData,
|
|
462
|
+
fragmentWithSpreads[key],
|
|
463
|
+
priorityNode
|
|
464
|
+
);
|
|
465
|
+
} else if (fragmentWithSpreads[key] !== undefined) {
|
|
466
|
+
fragmentWithSpreads[key] = mergeNestedObjects(
|
|
467
|
+
spreadData,
|
|
468
|
+
fragmentWithSpreads[key],
|
|
469
|
+
);
|
|
470
|
+
} else {
|
|
471
|
+
fragmentWithSpreads[key] = spreadData;
|
|
472
|
+
}
|
|
210
473
|
}
|
|
211
474
|
}
|
|
212
475
|
}
|
|
@@ -218,7 +481,12 @@ export function loadFragment(id: string) {
|
|
|
218
481
|
* Map each inner selection under a graphql query object
|
|
219
482
|
*/
|
|
220
483
|
|
|
221
|
-
function mapSelections(
|
|
484
|
+
function mapSelections(
|
|
485
|
+
data: any,
|
|
486
|
+
innerSelections: any[],
|
|
487
|
+
updatedKeys: string[] = [],
|
|
488
|
+
fallbackScope?: string,
|
|
489
|
+
) {
|
|
222
490
|
|
|
223
491
|
return innerSelections.map((inner) => {
|
|
224
492
|
|
|
@@ -228,7 +496,7 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
228
496
|
const frgName = inner.name.value;
|
|
229
497
|
const frgMap = PARTIALS_MAP[frgName];
|
|
230
498
|
|
|
231
|
-
fragmentKey =
|
|
499
|
+
fragmentKey = makeFragmentCacheKey(frgMap || frgName, data, fallbackScope);
|
|
232
500
|
|
|
233
501
|
} else if (inner.kind === 'Field' && inner.selectionSet && inner.selectionSet.selections?.length) {
|
|
234
502
|
|
|
@@ -239,11 +507,22 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
239
507
|
|
|
240
508
|
if (innerData && Array.isArray(innerSelections)) {
|
|
241
509
|
if (Array.isArray(innerData)) {
|
|
242
|
-
|
|
243
|
-
|
|
510
|
+
const parentScope = getFragmentCacheScope(data, fallbackScope);
|
|
511
|
+
for (const [itemIndex, item] of innerData.entries()) {
|
|
512
|
+
mapSelections(
|
|
513
|
+
item,
|
|
514
|
+
inner.selectionSet.selections,
|
|
515
|
+
updatedKeys,
|
|
516
|
+
`${parentScope}.${inner.name.value}.${itemIndex}`,
|
|
517
|
+
);
|
|
244
518
|
}
|
|
245
519
|
} else {
|
|
246
|
-
mapSelections(
|
|
520
|
+
mapSelections(
|
|
521
|
+
innerData,
|
|
522
|
+
inner.selectionSet.selections,
|
|
523
|
+
updatedKeys,
|
|
524
|
+
`${getFragmentCacheScope(data, fallbackScope)}.${inner.name.value}`,
|
|
525
|
+
);
|
|
247
526
|
}
|
|
248
527
|
}
|
|
249
528
|
|
|
@@ -256,7 +535,7 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
256
535
|
return null;
|
|
257
536
|
}
|
|
258
537
|
|
|
259
|
-
fragmentKey =
|
|
538
|
+
fragmentKey = makeFragmentCacheKey(frgNamesJoined, data, fallbackScope);
|
|
260
539
|
}
|
|
261
540
|
|
|
262
541
|
if (fragmentKey) {
|
|
@@ -264,8 +543,13 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
264
543
|
// Because partials might have data that's not in the main fragment
|
|
265
544
|
const currentData = loadFragment(fragmentKey);
|
|
266
545
|
|
|
267
|
-
//
|
|
268
|
-
//
|
|
546
|
+
// console.log(fragmentKey);
|
|
547
|
+
// if (fragmentKey.startsWith('$reportFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
548
|
+
// console.log('>>>>');
|
|
549
|
+
// console.log('>>>>');
|
|
550
|
+
// console.log('>>>>');
|
|
551
|
+
// console.log('>>>>');
|
|
552
|
+
// console.log(currentData);
|
|
269
553
|
// console.log(data);
|
|
270
554
|
// console.log(extractFragment(data, innerSelections));
|
|
271
555
|
// }
|
|
@@ -299,7 +583,7 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
299
583
|
|
|
300
584
|
for (const sel of selections) {
|
|
301
585
|
const key = sel.name.value;
|
|
302
|
-
if (
|
|
586
|
+
if (data[key] === undefined && sel.kind !== 'FragmentSpread') {
|
|
303
587
|
// console.log('~~~', key, sel.kind);
|
|
304
588
|
continue;
|
|
305
589
|
}
|
|
@@ -313,12 +597,14 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
313
597
|
cacheObj[key] = {
|
|
314
598
|
__cache: true,
|
|
315
599
|
__list: true,
|
|
316
|
-
data: data[key].map((item) =>
|
|
600
|
+
data: data[key].map((item, itemIndex) =>
|
|
601
|
+
mapSelections(item, innerSelections, updatedKeys, `${key}.${itemIndex}`)
|
|
602
|
+
),
|
|
317
603
|
};
|
|
318
604
|
} else {
|
|
319
605
|
cacheObj[key] = {
|
|
320
606
|
__cache: true,
|
|
321
|
-
data: mapSelections(data[key], innerSelections, updatedKeys),
|
|
607
|
+
data: mapSelections(data[key], innerSelections, updatedKeys, key),
|
|
322
608
|
};
|
|
323
609
|
}
|
|
324
610
|
} else {
|
|
@@ -330,7 +616,7 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
330
616
|
{
|
|
331
617
|
const frgName = sel.name.value;
|
|
332
618
|
const frgMap = PARTIALS_MAP[frgName];
|
|
333
|
-
const fragmentKey =
|
|
619
|
+
const fragmentKey = makeFragmentCacheKey(frgMap || frgName, data);
|
|
334
620
|
|
|
335
621
|
if (RULES[frgName] !== false) {
|
|
336
622
|
// if (testMode) {
|
|
@@ -344,6 +630,15 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
344
630
|
// Because partials might have data that's not in the main fragment
|
|
345
631
|
const currentData = loadFragment(fragmentKey);
|
|
346
632
|
|
|
633
|
+
// if (fragmentKey.startsWith('$reportFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
634
|
+
// console.log('>>>>');
|
|
635
|
+
// console.log('>>>>');
|
|
636
|
+
// console.log('>>>>');
|
|
637
|
+
// console.log('>>>>');
|
|
638
|
+
// console.log(currentData);
|
|
639
|
+
// console.log(data);
|
|
640
|
+
// }
|
|
641
|
+
|
|
347
642
|
// console.log('>>>>', fragmentKey);
|
|
348
643
|
FRAGMENTS.set(fragmentKey, mergeNestedObjects(currentData, extractFragment(data, selections)));
|
|
349
644
|
updatedKeys.push(fragmentKey);
|
|
@@ -407,7 +702,8 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
407
702
|
// console.log(data);
|
|
408
703
|
// console.log(spreads);
|
|
409
704
|
// }
|
|
410
|
-
FRAGMENTS.
|
|
705
|
+
const currentSpreads = FRAGMENTS.get(spreadsFragmentKey);
|
|
706
|
+
FRAGMENTS.set(spreadsFragmentKey, mergeSpreads(currentSpreads, spreads));
|
|
411
707
|
}
|
|
412
708
|
}
|
|
413
709
|
|
|
@@ -566,56 +862,16 @@ export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
|
|
|
566
862
|
const isCache = newObj[key].__cache && newObj[key].data;
|
|
567
863
|
|
|
568
864
|
if (isCache) {
|
|
569
|
-
let cachedObj = {};
|
|
865
|
+
let cachedObj: any = {};
|
|
570
866
|
let cacheIsValid = true;
|
|
571
867
|
|
|
572
868
|
if (newObj[key].__list) {
|
|
573
869
|
cachedObj = newObj[key].data.map((di: any[]) => {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (d.length === 1 && d?.[0]?.startsWith('$')) {
|
|
580
|
-
mapCacheId = d[0];
|
|
581
|
-
} else {
|
|
582
|
-
item[d[0]] = d[1];
|
|
583
|
-
return;
|
|
584
|
-
}
|
|
585
|
-
} else if (typeof d === 'string') {
|
|
586
|
-
mapCacheId = d;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
// if (Array.isArray(d)) {
|
|
590
|
-
// item[d[0]] = d[1];
|
|
591
|
-
// return;
|
|
592
|
-
// } else if (typeof d === 'string') {
|
|
593
|
-
// mapCacheId = d;
|
|
594
|
-
// }
|
|
595
|
-
|
|
596
|
-
const frgIndex = mapCacheId.indexOf(':');
|
|
597
|
-
const frgName = mapCacheId.substring(1, frgIndex);
|
|
598
|
-
const frgMap = PARTIALS_MAP[frgName];
|
|
599
|
-
|
|
600
|
-
let fragmentKey;
|
|
601
|
-
if (frgMap) {
|
|
602
|
-
fragmentKey = `$${frgMap}:${mapCacheId.substring(frgIndex + 1)}`;
|
|
603
|
-
} else {
|
|
604
|
-
fragmentKey = mapCacheId;
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
const cachedFragment = loadFragment(fragmentKey);
|
|
608
|
-
|
|
609
|
-
if (cachedFragment) {
|
|
610
|
-
item = {
|
|
611
|
-
...item,
|
|
612
|
-
...cachedFragment,
|
|
613
|
-
};
|
|
614
|
-
} else {
|
|
615
|
-
// Cache was invalidated
|
|
616
|
-
cacheIsValid = false;
|
|
617
|
-
}
|
|
618
|
-
});
|
|
870
|
+
const item = mapCachedListItem(di, loadFragment);
|
|
871
|
+
if (!item) {
|
|
872
|
+
cacheIsValid = false;
|
|
873
|
+
return {};
|
|
874
|
+
}
|
|
619
875
|
|
|
620
876
|
return item;
|
|
621
877
|
});
|
|
@@ -790,6 +1046,15 @@ export function loadDataFromCache(
|
|
|
790
1046
|
const fragmentKey = eagerFragmentKeyMap[qryName];
|
|
791
1047
|
const cachedFragment = loadFragment(fragmentKey);
|
|
792
1048
|
|
|
1049
|
+
// if (fragmentKey.startsWith('$reportFragment:3789505824043091-4255281447699102-3789105884863193')) {
|
|
1050
|
+
// console.log('>>>>');
|
|
1051
|
+
// console.log('>>>>');
|
|
1052
|
+
// console.log('>>>>');
|
|
1053
|
+
// console.log('>>>>');
|
|
1054
|
+
// console.log(cachedFragment);
|
|
1055
|
+
// console.log(data);
|
|
1056
|
+
// }
|
|
1057
|
+
|
|
793
1058
|
if (cachedFragment) {
|
|
794
1059
|
data[qryName] = cachedFragment;
|
|
795
1060
|
}
|
|
@@ -866,12 +1131,132 @@ export function removeStaleCache() {
|
|
|
866
1131
|
}
|
|
867
1132
|
}
|
|
868
1133
|
|
|
1134
|
+
/**
|
|
1135
|
+
* Build a lookup map for fragment definitions in a GraphQL document.
|
|
1136
|
+
*/
|
|
1137
|
+
|
|
1138
|
+
function makeFragmentDefinitionMap(definitions: any[]) {
|
|
1139
|
+
return definitions.reduce((acc, def) => {
|
|
1140
|
+
if (def.kind === 'FragmentDefinition' && def.name?.value) {
|
|
1141
|
+
acc[def.name.value] = def;
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
return acc;
|
|
1145
|
+
}, {} as Record<string, any>);
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* Check whether an inline fragment applies to the current cached object type.
|
|
1150
|
+
*/
|
|
1151
|
+
function inlineFragmentMatchesCachedType(
|
|
1152
|
+
value: any,
|
|
1153
|
+
selection: any,
|
|
1154
|
+
) {
|
|
1155
|
+
const typeName = selection.typeCondition?.name?.value;
|
|
1156
|
+
if (!typeName || !value?.__typename) {
|
|
1157
|
+
return true;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
return value.__typename === typeName;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* Check whether a cached value contains all fields requested by a selection.
|
|
1165
|
+
*/
|
|
1166
|
+
|
|
1167
|
+
function hasCompleteSelectionData(
|
|
1168
|
+
value: any,
|
|
1169
|
+
selection: any,
|
|
1170
|
+
fragmentDefinitions: Record<string, any>,
|
|
1171
|
+
): boolean {
|
|
1172
|
+
if (value === null) {
|
|
1173
|
+
return true;
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
if (value === undefined) {
|
|
1177
|
+
return false;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
if (selection.kind === 'FragmentSpread') {
|
|
1181
|
+
const fragmentDef = fragmentDefinitions[selection.name?.value];
|
|
1182
|
+
return fragmentDef
|
|
1183
|
+
? hasCompleteSelectionSetData(
|
|
1184
|
+
value,
|
|
1185
|
+
fragmentDef.selectionSet?.selections || [],
|
|
1186
|
+
fragmentDefinitions,
|
|
1187
|
+
)
|
|
1188
|
+
: true;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
if (selection.kind === 'InlineFragment') {
|
|
1192
|
+
if (!inlineFragmentMatchesCachedType(value, selection)) {
|
|
1193
|
+
return true;
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
return hasCompleteSelectionSetData(
|
|
1197
|
+
value,
|
|
1198
|
+
selection.selectionSet?.selections || [],
|
|
1199
|
+
fragmentDefinitions,
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
if (selection.kind !== 'Field') {
|
|
1204
|
+
return true;
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
const fieldName = selection.name?.value;
|
|
1208
|
+
const fieldValue = value?.[fieldName];
|
|
1209
|
+
if (fieldValue === undefined) {
|
|
1210
|
+
return false;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
const selections = selection.selectionSet?.selections;
|
|
1214
|
+
if (!selections || fieldValue === null) {
|
|
1215
|
+
return true;
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
if (Array.isArray(fieldValue)) {
|
|
1219
|
+
return fieldValue.every((item) =>
|
|
1220
|
+
hasCompleteSelectionSetData(item, selections, fragmentDefinitions)
|
|
1221
|
+
);
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
return hasCompleteSelectionSetData(
|
|
1225
|
+
fieldValue,
|
|
1226
|
+
selections,
|
|
1227
|
+
fragmentDefinitions,
|
|
1228
|
+
);
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
/**
|
|
1232
|
+
* Check whether a cached object contains all fields requested by a selection set.
|
|
1233
|
+
*/
|
|
1234
|
+
|
|
1235
|
+
function hasCompleteSelectionSetData(
|
|
1236
|
+
value: any,
|
|
1237
|
+
selections: any[],
|
|
1238
|
+
fragmentDefinitions: Record<string, any>,
|
|
1239
|
+
): boolean {
|
|
1240
|
+
if (value === null) {
|
|
1241
|
+
return true;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
if (value === undefined) {
|
|
1245
|
+
return false;
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
return selections.every((selection) =>
|
|
1249
|
+
hasCompleteSelectionData(value, selection, fragmentDefinitions)
|
|
1250
|
+
);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
869
1253
|
/**
|
|
870
1254
|
* Check if there is cache
|
|
871
1255
|
*/
|
|
872
1256
|
|
|
873
1257
|
export function fetchCachedData(gqlQuery: any, variablesKey: string, updateObservers: UpdateObserversFn) {
|
|
874
1258
|
const definitions = gqlQuery.definitions;
|
|
1259
|
+
const fragmentDefinitions = makeFragmentDefinitionMap(definitions);
|
|
875
1260
|
|
|
876
1261
|
for (const def of definitions) {
|
|
877
1262
|
if (def.kind === 'OperationDefinition') {
|
|
@@ -894,6 +1279,17 @@ export function fetchCachedData(gqlQuery: any, variablesKey: string, updateObser
|
|
|
894
1279
|
|
|
895
1280
|
return null;
|
|
896
1281
|
}
|
|
1282
|
+
|
|
1283
|
+
if (!hasCompleteSelectionData({ [qryName]: cachedData }, selection, fragmentDefinitions)) {
|
|
1284
|
+
QUERIES.delete(queryId);
|
|
1285
|
+
|
|
1286
|
+
updateObservers({
|
|
1287
|
+
queryId,
|
|
1288
|
+
});
|
|
1289
|
+
|
|
1290
|
+
return null;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
897
1293
|
return { [qryName]: cachedData };
|
|
898
1294
|
}
|
|
899
1295
|
}
|
|
@@ -937,17 +1333,58 @@ export function clearQueryResetStatus(queryName: string, variablesKey: string) {
|
|
|
937
1333
|
RESET_TIME.delete(`#${queryName}:${variablesKey}`);
|
|
938
1334
|
}
|
|
939
1335
|
|
|
1336
|
+
/**
|
|
1337
|
+
* Delete cached query data without setting query reset times.
|
|
1338
|
+
*/
|
|
1339
|
+
|
|
1340
|
+
function deleteMatchingQueryCache(queryId: string) {
|
|
1341
|
+
if (queryId.startsWith('^')) {
|
|
1342
|
+
const regex = new RegExp(queryId);
|
|
1343
|
+
|
|
1344
|
+
for (const key of QUERIES.keys()) {
|
|
1345
|
+
if (regex.test(key)) {
|
|
1346
|
+
QUERIES.delete(key);
|
|
1347
|
+
console.dev('✅ RESET ONLY ->', key);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
if (queryId.endsWith(':')) {
|
|
1354
|
+
for (const key of QUERIES.keys()) {
|
|
1355
|
+
if (key.startsWith(queryId)) {
|
|
1356
|
+
QUERIES.delete(key);
|
|
1357
|
+
console.dev('✅ RESET ONLY ->', key);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
QUERIES.delete(queryId);
|
|
1364
|
+
console.dev('✅ RESET ONLY ->', queryId);
|
|
1365
|
+
}
|
|
1366
|
+
|
|
940
1367
|
/**
|
|
941
1368
|
* Clear query cache
|
|
942
1369
|
* NOTE: If "queryId" is a name of query postfixed by ":" like this => "#queryName:"
|
|
943
1370
|
* it will reset all queries everywhere for the same GraphQL query.
|
|
944
1371
|
*/
|
|
945
1372
|
|
|
946
|
-
export function resetQuery(queryId: string, forceRefetch?: boolean, updateObservers?: UpdateObserversFn) {
|
|
1373
|
+
export function resetQuery(queryId: string, forceRefetch?: boolean, updateObservers?: UpdateObserversFn, resetOnly?: boolean) {
|
|
947
1374
|
// if (forceRefetch) {
|
|
948
1375
|
// QUERIES.delete(queryId);
|
|
949
1376
|
// }
|
|
950
1377
|
|
|
1378
|
+
if (resetOnly) {
|
|
1379
|
+
deleteMatchingQueryCache(queryId);
|
|
1380
|
+
updateObservers?.({
|
|
1381
|
+
queryId,
|
|
1382
|
+
forceRefetch,
|
|
1383
|
+
resetOnly,
|
|
1384
|
+
});
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
951
1388
|
if (updateObservers) {
|
|
952
1389
|
if (queryId.startsWith('^')) {
|
|
953
1390
|
// Reset using regex
|
|
@@ -999,13 +1436,13 @@ export function updateFragment(
|
|
|
999
1436
|
updateObj = update;
|
|
1000
1437
|
}
|
|
1001
1438
|
|
|
1002
|
-
const
|
|
1439
|
+
const newUpdatedObj = { ...cache, ...updateObj };
|
|
1003
1440
|
|
|
1004
|
-
FRAGMENTS.set(updateFragmentKey,
|
|
1005
|
-
console.dev('✅ FRAGMENT UPDATED ->', updateFragmentKey,
|
|
1441
|
+
FRAGMENTS.set(updateFragmentKey, newUpdatedObj);
|
|
1442
|
+
console.dev('✅ FRAGMENT UPDATED ->', updateFragmentKey, newUpdatedObj);
|
|
1006
1443
|
|
|
1007
1444
|
if (replacementFragmentKey && replacementFragmentKey !== updateFragmentKey) {
|
|
1008
|
-
FRAGMENTS.set(replacementFragmentKey,
|
|
1445
|
+
FRAGMENTS.set(replacementFragmentKey, newUpdatedObj);
|
|
1009
1446
|
}
|
|
1010
1447
|
|
|
1011
1448
|
if (!doNotTriggerObserver && updateObservers) {
|
|
@@ -1047,10 +1484,10 @@ export function updateQuery(
|
|
|
1047
1484
|
updateObj = update;
|
|
1048
1485
|
}
|
|
1049
1486
|
|
|
1050
|
-
const
|
|
1487
|
+
const newUpdatedObj = Array.isArray(updateObj) ? updateObj : { ...cache, ...updateObj };
|
|
1051
1488
|
|
|
1052
1489
|
// console.log('UPDATING (3):', updateObj);
|
|
1053
|
-
QUERIES.set(queryId,
|
|
1490
|
+
QUERIES.set(queryId, newUpdatedObj);
|
|
1054
1491
|
|
|
1055
1492
|
if (!doNotTriggerObserver && updateObservers) {
|
|
1056
1493
|
updateObservers({
|