@jsb188/graphql 1.1.4 → 1.1.6
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 +13 -7
- package/cache/index.ts +355 -144
- package/gql/fragments/accountAuthFragments.ts +3 -9
- package/gql/fragments/accountFragments.ts +11 -0
- package/gql/fragments/actionFragments.ts +16 -0
- package/gql/fragments/logFragments.ts +75 -6
- package/gql/fragments/organizationFragments.ts +70 -4
- package/gql/fragments/otherFragments.ts +10 -8
- package/gql/fragments/productFragments.ts +63 -0
- package/gql/fragments/reportFragments.ts +47 -0
- package/gql/mutations/accountMutations.ts +31 -0
- package/gql/mutations/aiChatMutations.ts +1 -0
- package/gql/mutations/authMutations.ts +80 -22
- package/gql/mutations/index.ts +8 -0
- package/gql/mutations/logMutations.ts +56 -1
- package/gql/mutations/organizationMutations.ts +42 -0
- package/gql/mutations/otherMutations.ts +0 -17
- package/gql/mutations/userAuthMutations.ts +0 -116
- package/gql/queries/aiChatQueries.ts +4 -0
- package/gql/queries/appQueries.ts +2 -1
- package/gql/queries/index.ts +6 -0
- package/gql/queries/logQueries.ts +60 -11
- package/gql/queries/organizationQueries.ts +62 -1
- package/gql/queries/productQueries.ts +70 -0
- package/gql/queries/reportQueries.ts +60 -0
- package/hooks/index.ts +259 -113
- package/hooks/use-account-mtn.ts +68 -0
- package/hooks/use-aiChat-mtn.ts +112 -0
- package/hooks/use-aiChat-qry.ts +81 -10
- package/hooks/use-auth-mtn.ts +114 -63
- package/hooks/use-log-mtn.ts +85 -0
- package/hooks/use-log-qry.ts +71 -18
- package/hooks/use-organization-mtn.ts +105 -0
- package/hooks/use-organization-qry.ts +119 -15
- package/hooks/use-product-qry.ts +109 -0
- package/hooks/use-report-qry.ts +69 -0
- package/package.json +15 -25
- package/sse.ts +11 -0
- package/types.d.ts +1 -0
- package/gql/fragments/cardFragments.ts +0 -15
- package/gql/fragments/chatFragments.ts +0 -98
- package/gql/fragments/userAuthFragments.ts +0 -8
- package/gql/fragments/userFragments.ts +0 -69
- package/gql/mutations/chatMutations.ts +0 -116
- package/gql/mutations/userMutations.ts +0 -89
- package/gql/queries/cardQueries.ts +0 -24
- package/gql/queries/chatQueries.ts +0 -86
- package/gql/queries/otherQueries.ts +0 -12
- package/gql/queries/userQueries.ts +0 -104
package/cache/index.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { makeVariablesKey } from '@jsb188/app/utils/logic';
|
|
2
|
+
import { cloneArrayLike, mapArrayLikeObjects } from '@jsb188/app/utils/object';
|
|
2
3
|
import type { UpdateObserversFn } from '../types.d';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const DO_LOG = [
|
|
6
|
-
false, // [0] Log name
|
|
7
|
-
false, // [1] Log data
|
|
8
|
-
];
|
|
4
|
+
import { PARTIALS_MAP, RULES } from './config';
|
|
9
5
|
|
|
10
6
|
const QUERIES = new Map();
|
|
11
7
|
const FRAGMENTS = new Map();
|
|
8
|
+
const RESET_TIME = new Map();
|
|
12
9
|
|
|
13
10
|
/**
|
|
14
11
|
* JS Map will sometimes convert nested Array into Array-like Object
|
|
@@ -62,35 +59,6 @@ export function mergeNestedObjects(obj1: any, obj2: any) {
|
|
|
62
59
|
return newObj;
|
|
63
60
|
}
|
|
64
61
|
|
|
65
|
-
/**
|
|
66
|
-
* Get a single string to represent all variables for a query
|
|
67
|
-
* NOTE: This isn't perfect because variables could have extra or unnnecessary keys/values
|
|
68
|
-
* The correct way to do this would be to compare it against the query {variableDefinitions}
|
|
69
|
-
* But this will work as long as each variables object is used consistently to match the used query.
|
|
70
|
-
*
|
|
71
|
-
* NOTE: Same logic is used in SSE [publish.ts]
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
export function makeVariablesKey(variables?: any) {
|
|
75
|
-
if (variables) {
|
|
76
|
-
const key = Object.keys(variables).sort().reduce((acc, key) => {
|
|
77
|
-
const value = variables[key];
|
|
78
|
-
if (value || value === 0 || value === false) {
|
|
79
|
-
return `${acc}$${key}:${value}`;
|
|
80
|
-
}
|
|
81
|
-
return acc;
|
|
82
|
-
}, '');
|
|
83
|
-
|
|
84
|
-
// NOTE: I can't think of a query where capitalization matters;
|
|
85
|
-
// So I'm doing lowerCase() here.
|
|
86
|
-
// Also, I don't think there's any query where null/undefined matters;
|
|
87
|
-
// So I'm omitting all of those.
|
|
88
|
-
|
|
89
|
-
return key.toLowerCase();
|
|
90
|
-
}
|
|
91
|
-
return 'none';
|
|
92
|
-
}
|
|
93
|
-
|
|
94
62
|
/**
|
|
95
63
|
* Make Object for fragment, omitting what you don't need
|
|
96
64
|
*/
|
|
@@ -117,6 +85,10 @@ function extractFragment(dataObj: any, innerSelection: any[]) {
|
|
|
117
85
|
export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
118
86
|
const references = QUERIES.get(queryKey);
|
|
119
87
|
|
|
88
|
+
// if (testMode) {
|
|
89
|
+
// console.log('references>>>>:', references);
|
|
90
|
+
// }
|
|
91
|
+
|
|
120
92
|
if (referencesOnly) {
|
|
121
93
|
return references;
|
|
122
94
|
} else if (!references) {
|
|
@@ -148,7 +120,9 @@ export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
|
148
120
|
|
|
149
121
|
return mappedCache;
|
|
150
122
|
} else if (references) {
|
|
151
|
-
|
|
123
|
+
// console.log('references single:', references);
|
|
124
|
+
const singleCacheData = appendFragmentToCache(references, true);
|
|
125
|
+
// console.log('singleCacheData >>>>:', singleCacheData);
|
|
152
126
|
return singleCacheData;
|
|
153
127
|
}
|
|
154
128
|
}
|
|
@@ -158,7 +132,86 @@ export function loadQuery(queryKey: string, referencesOnly?: boolean) {
|
|
|
158
132
|
*/
|
|
159
133
|
|
|
160
134
|
export function loadFragment(id: string) {
|
|
161
|
-
|
|
135
|
+
// console.log('load fragment:', id, FRAGMENTS.get(id));
|
|
136
|
+
|
|
137
|
+
const fragment = enforceArrayType(FRAGMENTS.get(id));
|
|
138
|
+
if (!fragment) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const spreads = FRAGMENTS.get(`${id}.spreads`);
|
|
143
|
+
if (!spreads) {
|
|
144
|
+
return fragment;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// const isTest = id.startsWith('$logEntryFragment:9479647167133727') && false;
|
|
148
|
+
// if (isTest) {
|
|
149
|
+
// ..
|
|
150
|
+
// }
|
|
151
|
+
|
|
152
|
+
let fragmentWithSpreads = { ...fragment };
|
|
153
|
+
for (const key in spreads) {
|
|
154
|
+
|
|
155
|
+
if (Object.prototype.hasOwnProperty.call(spreads, key)) {
|
|
156
|
+
const spreadValue = spreads[key];
|
|
157
|
+
|
|
158
|
+
// if (isTest) {
|
|
159
|
+
// console.log('SPREAD:', key, '->', spreadValue);
|
|
160
|
+
// }
|
|
161
|
+
|
|
162
|
+
let spreadData;
|
|
163
|
+
if (typeof spreadValue === 'string') {
|
|
164
|
+
spreadData = enforceArrayType(FRAGMENTS.get(spreadValue));
|
|
165
|
+
} else if (Array.isArray(spreadValue)) {
|
|
166
|
+
spreadData = spreadValue.reduce((acc, value) => {
|
|
167
|
+
if (typeof value === 'string') {
|
|
168
|
+
const fragmentData = enforceArrayType(FRAGMENTS.get(value));
|
|
169
|
+
|
|
170
|
+
// if (isTest && key === 'details') {
|
|
171
|
+
// console.log('1:', value, fragmentData);
|
|
172
|
+
// }
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
...acc,
|
|
176
|
+
...fragmentData,
|
|
177
|
+
};
|
|
178
|
+
} else if (Array.isArray(value)) {
|
|
179
|
+
const [namespace, innerValue] = value;
|
|
180
|
+
// console.log('2:', namespace, value);
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
...acc,
|
|
184
|
+
[namespace]: innerValue,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return acc;
|
|
189
|
+
}, {});
|
|
190
|
+
} else if (spreadValue?.__list === true) {
|
|
191
|
+
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
|
+
if (typeof val === 'string') {
|
|
197
|
+
return loadFragment(val);
|
|
198
|
+
} else if (Array.isArray(val) && typeof val[0] === 'string' && val[0].startsWith('$')) {
|
|
199
|
+
return loadFragment(val[0]);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Unfinished work
|
|
203
|
+
console.warn('Unfinished GraphQL cache scenario!: ', val)
|
|
204
|
+
return null;
|
|
205
|
+
}).filter(Boolean);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (spreadData) {
|
|
209
|
+
fragmentWithSpreads[key] = spreadData;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return fragmentWithSpreads;
|
|
162
215
|
}
|
|
163
216
|
|
|
164
217
|
/**
|
|
@@ -166,6 +219,7 @@ export function loadFragment(id: string) {
|
|
|
166
219
|
*/
|
|
167
220
|
|
|
168
221
|
function mapSelections(data: any, innerSelections: any[], updatedKeys: string[] = []) {
|
|
222
|
+
|
|
169
223
|
return innerSelections.map((inner) => {
|
|
170
224
|
|
|
171
225
|
let fragmentKey;
|
|
@@ -175,15 +229,32 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
175
229
|
const frgMap = PARTIALS_MAP[frgName];
|
|
176
230
|
fragmentKey = `$${frgMap || frgName}:${data.id || 'none'}`;
|
|
177
231
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
232
|
+
} else if (inner.kind === 'Field' && inner.selectionSet && inner.selectionSet.selections?.length) {
|
|
233
|
+
|
|
234
|
+
// These are nested fragments, they need to be cached as a fragment
|
|
235
|
+
|
|
236
|
+
const innerData = data?.[inner.name.value];
|
|
237
|
+
const innerSelections = inner.selectionSet.selections?.filter((sel: any) => sel.kind === 'FragmentSpread');
|
|
238
|
+
|
|
239
|
+
if (innerData && Array.isArray(innerSelections)) {
|
|
240
|
+
if (Array.isArray(innerData)) {
|
|
241
|
+
for (const item of innerData) {
|
|
242
|
+
mapSelections(item, inner.selectionSet.selections, updatedKeys);
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
mapSelections(innerData, inner.selectionSet.selections, updatedKeys);
|
|
246
|
+
}
|
|
183
247
|
}
|
|
248
|
+
|
|
184
249
|
} else if (inner.kind === 'InlineFragment') {
|
|
185
250
|
// This is Union, etc
|
|
186
251
|
const frgNamesJoined = inner.selectionSet.selections.map((sel: any) => PARTIALS_MAP[sel.name.value] || sel.name.value).join('.');
|
|
252
|
+
|
|
253
|
+
if (frgNamesJoined.toLowerCase().indexOf(data.__typename.toLowerCase()) < 0) {
|
|
254
|
+
// This is untested, but should be fine
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
|
|
187
258
|
fragmentKey = `$${frgNamesJoined}:${data.id || 'none'}`;
|
|
188
259
|
}
|
|
189
260
|
|
|
@@ -192,7 +263,12 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
192
263
|
// Because partials might have data that's not in the main fragment
|
|
193
264
|
const currentData = loadFragment(fragmentKey);
|
|
194
265
|
|
|
195
|
-
//
|
|
266
|
+
// if (fragmentKey.startsWith('$accountFragment:')) {
|
|
267
|
+
// console.log('>>>>', fragmentKey);
|
|
268
|
+
// console.log(data);
|
|
269
|
+
// console.log(extractFragment(data, innerSelections));
|
|
270
|
+
// }
|
|
271
|
+
|
|
196
272
|
FRAGMENTS.set(fragmentKey, mergeNestedObjects(currentData, extractFragment(data, innerSelections)));
|
|
197
273
|
updatedKeys.push(fragmentKey);
|
|
198
274
|
|
|
@@ -208,7 +284,7 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
|
|
|
208
284
|
// name: inner.name.value,
|
|
209
285
|
// value: data[key]?.[inner.name.value]
|
|
210
286
|
// };
|
|
211
|
-
});
|
|
287
|
+
}).filter(Boolean);
|
|
212
288
|
}
|
|
213
289
|
|
|
214
290
|
/**
|
|
@@ -226,10 +302,6 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
226
302
|
continue;
|
|
227
303
|
}
|
|
228
304
|
|
|
229
|
-
// if (DO_LOG[2]) {
|
|
230
|
-
// console.log(sel);
|
|
231
|
-
// }
|
|
232
|
-
|
|
233
305
|
switch (sel.kind) {
|
|
234
306
|
case 'Field':
|
|
235
307
|
{
|
|
@@ -259,13 +331,6 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
259
331
|
const fragmentKey = `$${frgMap || frgName}:${data.id || 'none'}`;
|
|
260
332
|
|
|
261
333
|
if (RULES[frgName] !== false) {
|
|
262
|
-
if (DO_LOG[0]) {
|
|
263
|
-
console.log('SETTING (2): ', fragmentKey);
|
|
264
|
-
}
|
|
265
|
-
if (DO_LOG[1]) {
|
|
266
|
-
console.log('SETTING (2): ', extractFragment(data, selections));
|
|
267
|
-
}
|
|
268
|
-
|
|
269
334
|
// if (testMode) {
|
|
270
335
|
// console.log('Array.isArray(data)');
|
|
271
336
|
// console.log(Array.isArray(data));
|
|
@@ -277,7 +342,7 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
277
342
|
// Because partials might have data that's not in the main fragment
|
|
278
343
|
const currentData = loadFragment(fragmentKey);
|
|
279
344
|
|
|
280
|
-
//
|
|
345
|
+
// console.log('>>>>', fragmentKey);
|
|
281
346
|
FRAGMENTS.set(fragmentKey, mergeNestedObjects(currentData, extractFragment(data, selections)));
|
|
282
347
|
updatedKeys.push(fragmentKey);
|
|
283
348
|
}
|
|
@@ -303,6 +368,48 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
|
|
|
303
368
|
}
|
|
304
369
|
}
|
|
305
370
|
|
|
371
|
+
if (cacheObj.__flat?.data?.[0]) {
|
|
372
|
+
const spreadsFragmentKey = `${cacheObj.__flat.data[0]}.spreads`;
|
|
373
|
+
|
|
374
|
+
let spreads: Record<string, any> | undefined;
|
|
375
|
+
for (const cKey in cacheObj) {
|
|
376
|
+
if (
|
|
377
|
+
cKey !== '__flat' &&
|
|
378
|
+
Object.prototype.hasOwnProperty.call(cacheObj, cKey)
|
|
379
|
+
) {
|
|
380
|
+
if (!spreads) {
|
|
381
|
+
spreads = {};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Spreads here
|
|
385
|
+
|
|
386
|
+
if (cacheObj[cKey]?.data?.length === 1 && typeof cacheObj[cKey]?.data?.[0] === 'string') {
|
|
387
|
+
// Single spread with single fragment
|
|
388
|
+
spreads[cKey] = cacheObj[cKey].data[0];
|
|
389
|
+
} else if (cacheObj[cKey].__list === true) {
|
|
390
|
+
// List of spreads
|
|
391
|
+
spreads[cKey] = {
|
|
392
|
+
__list: true,
|
|
393
|
+
data: cacheObj[cKey].data
|
|
394
|
+
};
|
|
395
|
+
} else if (Array.isArray(cacheObj[cKey]?.data)) {
|
|
396
|
+
// Spread with more spreads inside
|
|
397
|
+
spreads[cKey] = cacheObj[cKey].data;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (spreads) {
|
|
403
|
+
// if (spreadsFragmentKey.startsWith('$emailFragment:')) {
|
|
404
|
+
// console.log('>>>>', spreadsFragmentKey);
|
|
405
|
+
// console.log(data);
|
|
406
|
+
// console.log(spreads);
|
|
407
|
+
// }
|
|
408
|
+
|
|
409
|
+
FRAGMENTS.set(spreadsFragmentKey, spreads);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
306
413
|
return [cacheObj, updatedKeys];
|
|
307
414
|
}
|
|
308
415
|
|
|
@@ -362,6 +469,7 @@ function saveQueryToMemory(
|
|
|
362
469
|
if (saveQueryToCache) {
|
|
363
470
|
const customCacheData = cacheMap(data, cacheData, selections, variables);
|
|
364
471
|
// console.log('1--->', queryId);
|
|
472
|
+
// console.log('UPDATING (1):', typeof customCacheData === 'undefined' ? null : customCacheData);
|
|
365
473
|
QUERIES.set(queryId, typeof customCacheData === 'undefined' ? null : customCacheData);
|
|
366
474
|
|
|
367
475
|
if (updateObservers) {
|
|
@@ -374,6 +482,7 @@ function saveQueryToMemory(
|
|
|
374
482
|
if (saveQueryToCache) {
|
|
375
483
|
// console.log('2--->', queryId);
|
|
376
484
|
// console.log('2--->', cacheData);
|
|
485
|
+
// console.log('UPDATING (2):', cacheData);
|
|
377
486
|
QUERIES.set(queryId, cacheData);
|
|
378
487
|
}
|
|
379
488
|
|
|
@@ -438,39 +547,63 @@ export function setDataToCache(
|
|
|
438
547
|
|
|
439
548
|
export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
|
|
440
549
|
let newObj = cloneArrayLike(cacheData);
|
|
550
|
+
let flatObj: any;
|
|
551
|
+
|
|
552
|
+
// if (testMode) {
|
|
553
|
+
// console.log('appendFragmentToCache:', newObj);
|
|
554
|
+
// }
|
|
441
555
|
|
|
442
556
|
for (const key in newObj) {
|
|
443
|
-
|
|
557
|
+
|
|
558
|
+
// if (testMode) {
|
|
559
|
+
// console.log('******* key:', key);
|
|
560
|
+
// console.log('******* ??:', newObj[key]);
|
|
561
|
+
// }
|
|
562
|
+
|
|
563
|
+
if (newObj[key] && typeof newObj[key] === 'object') {
|
|
444
564
|
const isFlat = key === '__flat';
|
|
565
|
+
const isCache = newObj[key].__cache && newObj[key].data;
|
|
566
|
+
|
|
567
|
+
if (isCache) {
|
|
568
|
+
let cachedObj = {};
|
|
569
|
+
let cacheIsValid = true;
|
|
570
|
+
|
|
571
|
+
if (newObj[key].__list) {
|
|
572
|
+
cachedObj = newObj[key].data.map((di: any[]) => {
|
|
573
|
+
|
|
574
|
+
let item = {};
|
|
575
|
+
di.forEach((d) => {
|
|
576
|
+
let mapCacheId;
|
|
577
|
+
if (Array.isArray(d)) {
|
|
578
|
+
if (d.length === 1 && d?.[0]?.startsWith('$')) {
|
|
579
|
+
mapCacheId = d[0];
|
|
580
|
+
} else {
|
|
581
|
+
item[d[0]] = d[1];
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
} else if (typeof d === 'string') {
|
|
585
|
+
mapCacheId = d;
|
|
586
|
+
}
|
|
445
587
|
|
|
446
|
-
|
|
447
|
-
|
|
588
|
+
// if (Array.isArray(d)) {
|
|
589
|
+
// item[d[0]] = d[1];
|
|
590
|
+
// return;
|
|
591
|
+
// } else if (typeof d === 'string') {
|
|
592
|
+
// mapCacheId = d;
|
|
593
|
+
// }
|
|
448
594
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
let item = {};
|
|
452
|
-
di.forEach((d) => {
|
|
453
|
-
if (Array.isArray(d)) {
|
|
454
|
-
item[d[0]] = d[1];
|
|
455
|
-
} else if (typeof d === 'string') {
|
|
456
|
-
const frgIndex = d.indexOf(':');
|
|
457
|
-
const frgName = d.substring(1, frgIndex);
|
|
595
|
+
const frgIndex = mapCacheId.indexOf(':');
|
|
596
|
+
const frgName = mapCacheId.substring(1, frgIndex);
|
|
458
597
|
const frgMap = PARTIALS_MAP[frgName];
|
|
459
598
|
|
|
460
599
|
let fragmentKey;
|
|
461
600
|
if (frgMap) {
|
|
462
|
-
fragmentKey = `$${frgMap}:${
|
|
601
|
+
fragmentKey = `$${frgMap}:${mapCacheId.substring(frgIndex + 1)}`;
|
|
463
602
|
} else {
|
|
464
|
-
fragmentKey =
|
|
603
|
+
fragmentKey = mapCacheId;
|
|
465
604
|
}
|
|
466
605
|
|
|
467
606
|
const cachedFragment = loadFragment(fragmentKey);
|
|
468
|
-
if (DO_LOG[0]) {
|
|
469
|
-
console.log('LOAD (1): ', d);
|
|
470
|
-
}
|
|
471
|
-
if (DO_LOG[1]) {
|
|
472
|
-
console.log('LOAD (1): ', cachedFragment);
|
|
473
|
-
}
|
|
474
607
|
|
|
475
608
|
if (cachedFragment) {
|
|
476
609
|
item = {
|
|
@@ -481,70 +614,102 @@ export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
|
|
|
481
614
|
// Cache was invalidated
|
|
482
615
|
cacheIsValid = false;
|
|
483
616
|
}
|
|
484
|
-
}
|
|
485
|
-
});
|
|
486
|
-
|
|
487
|
-
return item;
|
|
488
|
-
});
|
|
489
|
-
} else {
|
|
490
|
-
newObj[key].data.forEach((d: any) => {
|
|
617
|
+
});
|
|
491
618
|
|
|
619
|
+
return item;
|
|
620
|
+
});
|
|
621
|
+
} else {
|
|
622
|
+
newObj[key].data.forEach((d: any) => {
|
|
492
623
|
|
|
493
624
|
// if (testMode) {
|
|
494
625
|
// console.log('cachedObj', cachedObj);
|
|
626
|
+
// console.log('key', key);
|
|
495
627
|
// console.log('d', d);
|
|
496
628
|
// console.log('d', typeof d);
|
|
497
629
|
// }
|
|
498
630
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
631
|
+
if (Array.isArray(d)) {
|
|
632
|
+
cachedObj[d[0]] = d[1];
|
|
633
|
+
} else if (typeof d === 'string') {
|
|
634
|
+
const frgIndex = d.indexOf(':');
|
|
635
|
+
const frgName = d.substring(1, frgIndex);
|
|
636
|
+
const frgMap = PARTIALS_MAP[frgName];
|
|
505
637
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
638
|
+
let fragmentKey;
|
|
639
|
+
if (frgMap) {
|
|
640
|
+
fragmentKey = `$${frgMap}:${d.substring(frgIndex + 1)}`;
|
|
641
|
+
} else {
|
|
642
|
+
fragmentKey = d;
|
|
643
|
+
}
|
|
512
644
|
|
|
513
|
-
|
|
514
|
-
if (DO_LOG[0]) {
|
|
515
|
-
console.log('LOAD (2): ', d);
|
|
516
|
-
}
|
|
517
|
-
if (DO_LOG[1]) {
|
|
518
|
-
console.log('LOAD (2): ', cachedFragment);
|
|
519
|
-
}
|
|
645
|
+
const cachedFragment = loadFragment(fragmentKey);
|
|
520
646
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
647
|
+
if (cachedFragment) {
|
|
648
|
+
if (isFlat) {
|
|
649
|
+
// if (testMode) {
|
|
650
|
+
// console.log('///////');
|
|
651
|
+
// console.log('///////');
|
|
652
|
+
// console.log('newObj before:', newObj);
|
|
653
|
+
// console.log('fragmentKey:', fragmentKey);
|
|
654
|
+
// console.log('cachedFragment:', cachedFragment);
|
|
655
|
+
// }
|
|
656
|
+
|
|
657
|
+
// newObj = {
|
|
658
|
+
// ...newObj,
|
|
659
|
+
// ...cachedFragment,
|
|
660
|
+
// };
|
|
661
|
+
|
|
662
|
+
flatObj = cachedFragment,
|
|
663
|
+
delete newObj.__flat;
|
|
664
|
+
} else {
|
|
665
|
+
cachedObj = {
|
|
666
|
+
...cachedObj,
|
|
667
|
+
...cachedFragment,
|
|
668
|
+
};
|
|
669
|
+
}
|
|
528
670
|
} else {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
...cachedFragment,
|
|
532
|
-
};
|
|
671
|
+
// Cache was invalidated
|
|
672
|
+
cacheIsValid = false;
|
|
533
673
|
}
|
|
534
|
-
} else {
|
|
535
|
-
// Cache was invalidated
|
|
536
|
-
cacheIsValid = false;
|
|
537
674
|
}
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
}
|
|
675
|
+
});
|
|
676
|
+
}
|
|
541
677
|
|
|
542
|
-
|
|
543
|
-
|
|
678
|
+
if (!cacheIsValid) {
|
|
679
|
+
return null;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
if (!isFlat) {
|
|
683
|
+
newObj[key] = cachedObj;
|
|
684
|
+
}
|
|
685
|
+
} else {
|
|
686
|
+
|
|
687
|
+
// Fragment spreads recursively loads the fragment data
|
|
688
|
+
// So we have to get the non-spread fields from query cache
|
|
689
|
+
|
|
690
|
+
const cacheInfo = cacheData[key];
|
|
691
|
+
if (newObj[key] && typeof newObj[key] === 'object' && cacheInfo?.__cache && cacheInfo.data) {
|
|
692
|
+
cacheInfo.data.forEach((di: any) => {
|
|
693
|
+
if (
|
|
694
|
+
Array.isArray(di) &&
|
|
695
|
+
di.length === 2 &&
|
|
696
|
+
!di?.[0]?.startsWith('$')
|
|
697
|
+
) {
|
|
698
|
+
newObj[key][di[0]] = di[1];
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
}
|
|
544
702
|
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
545
705
|
|
|
546
|
-
|
|
547
|
-
|
|
706
|
+
// Merge flat data
|
|
707
|
+
|
|
708
|
+
if (flatObj) {
|
|
709
|
+
for (const key in flatObj) {
|
|
710
|
+
// console.log('FLAT:', key, flatObj[key], newObj[key]);
|
|
711
|
+
if (newObj[key] === undefined) {
|
|
712
|
+
newObj[key] = flatObj[key];
|
|
548
713
|
}
|
|
549
714
|
}
|
|
550
715
|
}
|
|
@@ -562,19 +727,12 @@ export function loadDataFromCache(
|
|
|
562
727
|
variablesKey: string,
|
|
563
728
|
cacheMap?: any,
|
|
564
729
|
eagerFragmentKeyMap?: any,
|
|
730
|
+
testMode?: boolean,
|
|
565
731
|
) {
|
|
566
732
|
// return query[variablesKey];
|
|
567
733
|
const data = { ...requestData };
|
|
568
734
|
const definitions = gqlQuery.definitions;
|
|
569
735
|
|
|
570
|
-
|
|
571
|
-
// console.log('loadDataFromCache()');
|
|
572
|
-
// console.log(data);
|
|
573
|
-
// console.log(definitions);
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
736
|
for (const def of definitions) {
|
|
579
737
|
if (def.kind === 'OperationDefinition') {
|
|
580
738
|
def?.selectionSet?.selections.forEach((selection: any) => {
|
|
@@ -701,6 +859,7 @@ export function removeStaleCache() {
|
|
|
701
859
|
|
|
702
860
|
for (const key of keysToEvict) {
|
|
703
861
|
FRAGMENTS.delete(key);
|
|
862
|
+
FRAGMENTS.delete(`${key}.spreads`);
|
|
704
863
|
}
|
|
705
864
|
}
|
|
706
865
|
}
|
|
@@ -751,8 +910,36 @@ export function clearGraphQLClientCache() {
|
|
|
751
910
|
FRAGMENTS.clear();
|
|
752
911
|
}
|
|
753
912
|
|
|
913
|
+
/**
|
|
914
|
+
* Check if query was reset (used on mount to check if a new fetch is needed)
|
|
915
|
+
*/
|
|
916
|
+
|
|
917
|
+
export function getQueryRefreshTime(queryName: string, variablesKey: string) {
|
|
918
|
+
// if (RESET_TIME.get(`#${queryName}:`) || RESET_TIME.get(`#${queryName}:${variablesKey}`)) {
|
|
919
|
+
// console.log(`#${queryName}:${variablesKey}`);
|
|
920
|
+
// console.log('1 >>>>>>>', RESET_TIME.get(`#${queryName}:`));
|
|
921
|
+
// console.log('2 >>>>>>>', RESET_TIME.get(`#${queryName}:${variablesKey}`));
|
|
922
|
+
// }
|
|
923
|
+
|
|
924
|
+
return (
|
|
925
|
+
RESET_TIME.get(`#${queryName}:`) ||
|
|
926
|
+
RESET_TIME.get(`#${queryName}:${variablesKey}`)
|
|
927
|
+
);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* Clear the reset status of a query
|
|
932
|
+
*/
|
|
933
|
+
|
|
934
|
+
export function clearQueryResetStatus(queryName: string, variablesKey: string) {
|
|
935
|
+
RESET_TIME.delete(`#${queryName}:`);
|
|
936
|
+
RESET_TIME.delete(`#${queryName}:${variablesKey}`);
|
|
937
|
+
}
|
|
938
|
+
|
|
754
939
|
/**
|
|
755
940
|
* Clear query cache
|
|
941
|
+
* NOTE: If "queryId" is a name of query postfixed by ":" like this => "#queryName:"
|
|
942
|
+
* it will reset all queries everywhere for the same GraphQL query.
|
|
756
943
|
*/
|
|
757
944
|
|
|
758
945
|
export function resetQuery(queryId: string, forceRefetch?: boolean, updateObservers?: UpdateObserversFn) {
|
|
@@ -761,10 +948,31 @@ export function resetQuery(queryId: string, forceRefetch?: boolean, updateObserv
|
|
|
761
948
|
// }
|
|
762
949
|
|
|
763
950
|
if (updateObservers) {
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
951
|
+
if (queryId.startsWith('^')) {
|
|
952
|
+
// Reset using regex
|
|
953
|
+
const regex = new RegExp(queryId);
|
|
954
|
+
|
|
955
|
+
// Loop for each of QUERIES map keys
|
|
956
|
+
for (const key of QUERIES.keys()) {
|
|
957
|
+
if (regex.test(key)) {
|
|
958
|
+
RESET_TIME.set(key, Date.now());
|
|
959
|
+
console.dev('✅ RESET ->', key);
|
|
960
|
+
updateObservers({
|
|
961
|
+
queryId: key,
|
|
962
|
+
forceRefetch,
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
} else {
|
|
967
|
+
// Reset specific query
|
|
968
|
+
RESET_TIME.set(queryId, Date.now());
|
|
969
|
+
console.dev('✅ RESET ->', queryId);
|
|
970
|
+
updateObservers({
|
|
971
|
+
queryId,
|
|
972
|
+
forceRefetch,
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
// console.log('SETTING ->', queryId, RESET_TIME.get(queryId));
|
|
768
976
|
}
|
|
769
977
|
}
|
|
770
978
|
|
|
@@ -778,31 +986,33 @@ export function updateFragment(
|
|
|
778
986
|
replaceId?: string | null,
|
|
779
987
|
doNotTriggerObserver?: boolean,
|
|
780
988
|
updateObservers?: UpdateObserversFn,
|
|
989
|
+
otherUpdatedFragmentKeys?: string[],
|
|
781
990
|
) {
|
|
782
991
|
const cache = loadFragment(updateFragmentKey);
|
|
783
992
|
|
|
784
993
|
if (cache && update) {
|
|
785
994
|
let updateObj;
|
|
786
995
|
if (typeof update === 'function') {
|
|
787
|
-
updateObj = update(cache);
|
|
996
|
+
updateObj = update(mapArrayLikeObjects(cache));
|
|
788
997
|
} else {
|
|
789
998
|
updateObj = update;
|
|
790
999
|
}
|
|
791
1000
|
|
|
792
1001
|
const updatedObj = { ...cache, ...updateObj };
|
|
793
|
-
|
|
794
1002
|
FRAGMENTS.set(updateFragmentKey, updatedObj);
|
|
795
1003
|
|
|
796
1004
|
if (replaceId && replaceId !== updateFragmentKey) {
|
|
797
1005
|
FRAGMENTS.set(replaceId, updatedObj);
|
|
798
|
-
|
|
799
|
-
// Must be deleted later, for the observer to work properly
|
|
800
|
-
// setTimeout(() => FRAGMENTS.delete(id), 1000);
|
|
801
1006
|
}
|
|
802
1007
|
|
|
803
1008
|
if (!doNotTriggerObserver && updateObservers) {
|
|
1009
|
+
let observingFragmentIds = [updateFragmentKey];
|
|
1010
|
+
if (Array.isArray(otherUpdatedFragmentKeys)) {
|
|
1011
|
+
observingFragmentIds = observingFragmentIds.concat(otherUpdatedFragmentKeys);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
804
1014
|
updateObservers({
|
|
805
|
-
fragmentIds:
|
|
1015
|
+
fragmentIds: observingFragmentIds,
|
|
806
1016
|
});
|
|
807
1017
|
|
|
808
1018
|
// FRAGMENT_OBSERVER.value = {
|
|
@@ -829,12 +1039,14 @@ export function updateQuery(
|
|
|
829
1039
|
if (cache && update) {
|
|
830
1040
|
let updateObj;
|
|
831
1041
|
if (typeof update === 'function') {
|
|
832
|
-
updateObj = update(cache);
|
|
1042
|
+
updateObj = update(mapArrayLikeObjects(cache));
|
|
833
1043
|
} else {
|
|
834
1044
|
updateObj = update;
|
|
835
1045
|
}
|
|
836
1046
|
|
|
837
1047
|
const updatedObj = Array.isArray(updateObj) ? updateObj : { ...cache, ...updateObj };
|
|
1048
|
+
|
|
1049
|
+
// console.log('UPDATING (3):', updateObj);
|
|
838
1050
|
QUERIES.set(queryId, updatedObj);
|
|
839
1051
|
|
|
840
1052
|
if (!doNotTriggerObserver && updateObservers) {
|
|
@@ -871,6 +1083,5 @@ export function triggerObserver(ids: (string | null)[], updateObservers?: Update
|
|
|
871
1083
|
*/
|
|
872
1084
|
|
|
873
1085
|
export function addFragmentToCache(id: string, obj: any) {
|
|
874
|
-
// FRAGMENTS.delete(id);
|
|
875
1086
|
FRAGMENTS.set(id, obj);
|
|
876
1087
|
}
|