@jsb188/graphql 1.1.8 → 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 +478 -102
- 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 +0 -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 +93 -9
- 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));
|
|
@@ -150,6 +396,7 @@ export function loadFragment(id: string) {
|
|
|
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)) {
|
|
@@ -197,40 +444,32 @@ export function loadFragment(id: string) {
|
|
|
197
444
|
}, {});
|
|
198
445
|
} else if (spreadValue?.__list === true) {
|
|
199
446
|
spreadData = spreadValue.data.map((val: any) => {
|
|
200
|
-
|
|
201
|
-
// if (isTest) {
|
|
202
|
-
// console.log('>>> VAL:', val);
|
|
203
|
-
// }
|
|
204
|
-
|
|
205
|
-
// NOTE: I haven't fully finished this work,
|
|
206
|
-
// but it should work for most cases.
|
|
207
|
-
|
|
208
447
|
if (typeof val === 'string') {
|
|
209
448
|
return loadFragment(val);
|
|
210
|
-
} else if (Array.isArray(val)
|
|
211
|
-
|
|
212
|
-
const innerFragment = loadFragment(innerFragmentKey);
|
|
213
|
-
|
|
214
|
-
if (innerFragment) {
|
|
215
|
-
for (const ri of restInner) {
|
|
216
|
-
if (Array.isArray(ri) && ri.length === 2) {
|
|
217
|
-
// ri[0] === 'files' && ri[1].length === 1 && console.log(ri);
|
|
218
|
-
// innerFragment[ri[0]] = ri[1];
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
return innerFragment;
|
|
449
|
+
} else if (Array.isArray(val)) {
|
|
450
|
+
return mapCachedListItem(val, loadFragment);
|
|
224
451
|
}
|
|
225
452
|
|
|
226
|
-
// Unfinished work
|
|
227
|
-
console.warn('Unfinished GraphQL cache scenario!: ', val)
|
|
228
453
|
return null;
|
|
229
454
|
}).filter(Boolean);
|
|
230
455
|
}
|
|
231
456
|
|
|
232
457
|
if (spreadData) {
|
|
233
|
-
|
|
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
|
+
}
|
|
234
473
|
}
|
|
235
474
|
}
|
|
236
475
|
}
|
|
@@ -242,7 +481,12 @@ export function loadFragment(id: string) {
|
|
|
242
481
|
* Map each inner selection under a graphql query object
|
|
243
482
|
*/
|
|
244
483
|
|
|
245
|
-
function mapSelections(
|
|
484
|
+
function mapSelections(
|
|
485
|
+
data: any,
|
|
486
|
+
innerSelections: any[],
|
|
487
|
+
updatedKeys: string[] = [],
|
|
488
|
+
fallbackScope?: string,
|
|
489
|
+
) {
|
|
246
490
|
|
|
247
491
|
return innerSelections.map((inner) => {
|
|
248
492
|
|
|
@@ -252,7 +496,7 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
252
496
|
const frgName = inner.name.value;
|
|
253
497
|
const frgMap = PARTIALS_MAP[frgName];
|
|
254
498
|
|
|
255
|
-
fragmentKey =
|
|
499
|
+
fragmentKey = makeFragmentCacheKey(frgMap || frgName, data, fallbackScope);
|
|
256
500
|
|
|
257
501
|
} else if (inner.kind === 'Field' && inner.selectionSet && inner.selectionSet.selections?.length) {
|
|
258
502
|
|
|
@@ -263,11 +507,22 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
263
507
|
|
|
264
508
|
if (innerData && Array.isArray(innerSelections)) {
|
|
265
509
|
if (Array.isArray(innerData)) {
|
|
266
|
-
|
|
267
|
-
|
|
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
|
+
);
|
|
268
518
|
}
|
|
269
519
|
} else {
|
|
270
|
-
mapSelections(
|
|
520
|
+
mapSelections(
|
|
521
|
+
innerData,
|
|
522
|
+
inner.selectionSet.selections,
|
|
523
|
+
updatedKeys,
|
|
524
|
+
`${getFragmentCacheScope(data, fallbackScope)}.${inner.name.value}`,
|
|
525
|
+
);
|
|
271
526
|
}
|
|
272
527
|
}
|
|
273
528
|
|
|
@@ -280,7 +535,7 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
280
535
|
return null;
|
|
281
536
|
}
|
|
282
537
|
|
|
283
|
-
fragmentKey =
|
|
538
|
+
fragmentKey = makeFragmentCacheKey(frgNamesJoined, data, fallbackScope);
|
|
284
539
|
}
|
|
285
540
|
|
|
286
541
|
if (fragmentKey) {
|
|
@@ -328,7 +583,7 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
328
583
|
|
|
329
584
|
for (const sel of selections) {
|
|
330
585
|
const key = sel.name.value;
|
|
331
|
-
if (
|
|
586
|
+
if (data[key] === undefined && sel.kind !== 'FragmentSpread') {
|
|
332
587
|
// console.log('~~~', key, sel.kind);
|
|
333
588
|
continue;
|
|
334
589
|
}
|
|
@@ -342,12 +597,14 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
342
597
|
cacheObj[key] = {
|
|
343
598
|
__cache: true,
|
|
344
599
|
__list: true,
|
|
345
|
-
data: data[key].map((item) =>
|
|
600
|
+
data: data[key].map((item, itemIndex) =>
|
|
601
|
+
mapSelections(item, innerSelections, updatedKeys, `${key}.${itemIndex}`)
|
|
602
|
+
),
|
|
346
603
|
};
|
|
347
604
|
} else {
|
|
348
605
|
cacheObj[key] = {
|
|
349
606
|
__cache: true,
|
|
350
|
-
data: mapSelections(data[key], innerSelections, updatedKeys),
|
|
607
|
+
data: mapSelections(data[key], innerSelections, updatedKeys, key),
|
|
351
608
|
};
|
|
352
609
|
}
|
|
353
610
|
} else {
|
|
@@ -359,7 +616,7 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
359
616
|
{
|
|
360
617
|
const frgName = sel.name.value;
|
|
361
618
|
const frgMap = PARTIALS_MAP[frgName];
|
|
362
|
-
const fragmentKey =
|
|
619
|
+
const fragmentKey = makeFragmentCacheKey(frgMap || frgName, data);
|
|
363
620
|
|
|
364
621
|
if (RULES[frgName] !== false) {
|
|
365
622
|
// if (testMode) {
|
|
@@ -445,7 +702,8 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
445
702
|
// console.log(data);
|
|
446
703
|
// console.log(spreads);
|
|
447
704
|
// }
|
|
448
|
-
FRAGMENTS.
|
|
705
|
+
const currentSpreads = FRAGMENTS.get(spreadsFragmentKey);
|
|
706
|
+
FRAGMENTS.set(spreadsFragmentKey, mergeSpreads(currentSpreads, spreads));
|
|
449
707
|
}
|
|
450
708
|
}
|
|
451
709
|
|
|
@@ -604,70 +862,16 @@ export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
|
|
|
604
862
|
const isCache = newObj[key].__cache && newObj[key].data;
|
|
605
863
|
|
|
606
864
|
if (isCache) {
|
|
607
|
-
let cachedObj = {};
|
|
865
|
+
let cachedObj: any = {};
|
|
608
866
|
let cacheIsValid = true;
|
|
609
867
|
|
|
610
868
|
if (newObj[key].__list) {
|
|
611
869
|
cachedObj = newObj[key].data.map((di: any[]) => {
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
if (d.length === 1 && d?.[0]?.startsWith('$')) {
|
|
618
|
-
mapCacheId = d[0];
|
|
619
|
-
} else if (Array.isArray(d[1])) {
|
|
620
|
-
// Use __reactiveFragmentName to make client-side data up to date
|
|
621
|
-
item[d[0]] = d[1].map(dr => {
|
|
622
|
-
if (dr.__reactiveFragmentName) {
|
|
623
|
-
const crFKey = `${dr.__reactiveFragmentName}:${dr.id}`;
|
|
624
|
-
return FRAGMENTS.get(crFKey) || dr;
|
|
625
|
-
}
|
|
626
|
-
return dr;
|
|
627
|
-
});
|
|
628
|
-
return;
|
|
629
|
-
} else if (d[1]?.__reactiveFragmentName) {
|
|
630
|
-
// Use __reactiveFragmentName to make client-side data up to date
|
|
631
|
-
const clientReactiveFragmentKey = `${d[1].__reactiveFragmentName}:${d[1].id}`;
|
|
632
|
-
item[d[0]] = FRAGMENTS.get(clientReactiveFragmentKey) || d[1];
|
|
633
|
-
return;
|
|
634
|
-
} else {
|
|
635
|
-
item[d[0]] = d[1];
|
|
636
|
-
return;
|
|
637
|
-
}
|
|
638
|
-
} else if (typeof d === 'string') {
|
|
639
|
-
mapCacheId = d;
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
// if (Array.isArray(d)) {
|
|
643
|
-
// item[d[0]] = d[1];
|
|
644
|
-
// return;
|
|
645
|
-
// } else if (typeof d === 'string') {
|
|
646
|
-
// mapCacheId = d;
|
|
647
|
-
// }
|
|
648
|
-
|
|
649
|
-
const frgIndex = mapCacheId.indexOf(':');
|
|
650
|
-
const frgName = mapCacheId.substring(1, frgIndex);
|
|
651
|
-
const frgMap = PARTIALS_MAP[frgName];
|
|
652
|
-
|
|
653
|
-
let fragmentKey;
|
|
654
|
-
if (frgMap) {
|
|
655
|
-
fragmentKey = `$${frgMap}:${mapCacheId.substring(frgIndex + 1)}`;
|
|
656
|
-
} else {
|
|
657
|
-
fragmentKey = mapCacheId;
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
const cachedFragment = loadFragment(fragmentKey);
|
|
661
|
-
if (cachedFragment) {
|
|
662
|
-
item = {
|
|
663
|
-
...item,
|
|
664
|
-
...cachedFragment,
|
|
665
|
-
};
|
|
666
|
-
} else {
|
|
667
|
-
// Cache was invalidated
|
|
668
|
-
cacheIsValid = false;
|
|
669
|
-
}
|
|
670
|
-
});
|
|
870
|
+
const item = mapCachedListItem(di, loadFragment);
|
|
871
|
+
if (!item) {
|
|
872
|
+
cacheIsValid = false;
|
|
873
|
+
return {};
|
|
874
|
+
}
|
|
671
875
|
|
|
672
876
|
return item;
|
|
673
877
|
});
|
|
@@ -927,12 +1131,132 @@ export function removeStaleCache() {
|
|
|
927
1131
|
}
|
|
928
1132
|
}
|
|
929
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
|
+
|
|
930
1253
|
/**
|
|
931
1254
|
* Check if there is cache
|
|
932
1255
|
*/
|
|
933
1256
|
|
|
934
1257
|
export function fetchCachedData(gqlQuery: any, variablesKey: string, updateObservers: UpdateObserversFn) {
|
|
935
1258
|
const definitions = gqlQuery.definitions;
|
|
1259
|
+
const fragmentDefinitions = makeFragmentDefinitionMap(definitions);
|
|
936
1260
|
|
|
937
1261
|
for (const def of definitions) {
|
|
938
1262
|
if (def.kind === 'OperationDefinition') {
|
|
@@ -955,6 +1279,17 @@ export function fetchCachedData(gqlQuery: any, variablesKey: string, updateObser
|
|
|
955
1279
|
|
|
956
1280
|
return null;
|
|
957
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
|
+
|
|
958
1293
|
return { [qryName]: cachedData };
|
|
959
1294
|
}
|
|
960
1295
|
}
|
|
@@ -998,17 +1333,58 @@ export function clearQueryResetStatus(queryName: string, variablesKey: string) {
|
|
|
998
1333
|
RESET_TIME.delete(`#${queryName}:${variablesKey}`);
|
|
999
1334
|
}
|
|
1000
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
|
+
|
|
1001
1367
|
/**
|
|
1002
1368
|
* Clear query cache
|
|
1003
1369
|
* NOTE: If "queryId" is a name of query postfixed by ":" like this => "#queryName:"
|
|
1004
1370
|
* it will reset all queries everywhere for the same GraphQL query.
|
|
1005
1371
|
*/
|
|
1006
1372
|
|
|
1007
|
-
export function resetQuery(queryId: string, forceRefetch?: boolean, updateObservers?: UpdateObserversFn) {
|
|
1373
|
+
export function resetQuery(queryId: string, forceRefetch?: boolean, updateObservers?: UpdateObserversFn, resetOnly?: boolean) {
|
|
1008
1374
|
// if (forceRefetch) {
|
|
1009
1375
|
// QUERIES.delete(queryId);
|
|
1010
1376
|
// }
|
|
1011
1377
|
|
|
1378
|
+
if (resetOnly) {
|
|
1379
|
+
deleteMatchingQueryCache(queryId);
|
|
1380
|
+
updateObservers?.({
|
|
1381
|
+
queryId,
|
|
1382
|
+
forceRefetch,
|
|
1383
|
+
resetOnly,
|
|
1384
|
+
});
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1012
1388
|
if (updateObservers) {
|
|
1013
1389
|
if (queryId.startsWith('^')) {
|
|
1014
1390
|
// Reset using regex
|