@jsb188/graphql 1.0.1 → 1.0.4
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 +1 -8
- package/cache/index.ts +23 -32
- package/client/hooks.ts +775 -0
- package/client/index.ts +2 -0
- package/client/request.ts +194 -0
- package/gql/fragments/aiChatFragments.ts +16 -0
- package/gql/mutations/aiChatMutations.ts +16 -0
- package/gql/queries/aiChatQueries.ts +41 -0
- package/package.json +13 -4
- package/sse.ts +139 -0
- package/types.d.ts +32 -19
- package/{cache/utils.ts → utils.ts} +29 -1
- package/main.ts +0 -2
package/cache/config.ts
CHANGED
|
@@ -18,6 +18,7 @@ export const RULES = {
|
|
|
18
18
|
|
|
19
19
|
// List of queries that should be cached
|
|
20
20
|
// NOTE: Fragments are always cached
|
|
21
|
+
aiChat: true,
|
|
21
22
|
alerts: true,
|
|
22
23
|
cardsForUser: true,
|
|
23
24
|
chat: true,
|
|
@@ -46,11 +47,3 @@ export const PARTIALS_MAP = {
|
|
|
46
47
|
chatStatusPartial: 'chatFragment',
|
|
47
48
|
userPartial: 'userFragment',
|
|
48
49
|
} as Record<string, string>;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Set up config, so the package can use app's variables
|
|
52
|
-
*/
|
|
53
|
-
|
|
54
|
-
export function configGraphQL(_rules: any) {
|
|
55
|
-
// Do something with rules and partials map here
|
|
56
|
-
}
|
package/cache/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cloneArrayLike } from '@jsb188/app/utils/object';
|
|
1
2
|
import type { UpdateObserversFn } from '../types.d';
|
|
2
3
|
import { RULES, PARTIALS_MAP } from './config';
|
|
3
4
|
|
|
@@ -61,28 +62,13 @@ export function mergeNestedObjects(obj1: any, obj2: any) {
|
|
|
61
62
|
return newObj;
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
/**
|
|
65
|
-
* Check if Object is array-like, then convert to plain Object.
|
|
66
|
-
* NOTE: This happens a lot when using JS Map()
|
|
67
|
-
* NOTE: This assumes that the keys are ordered numbers
|
|
68
|
-
* NOTE: This function always clones the object.
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
export function cloneArrayLike(obj: any) {
|
|
72
|
-
if (
|
|
73
|
-
!Array.isArray(obj) &&
|
|
74
|
-
Object.keys(obj).every((key, i) => key === i.toString())
|
|
75
|
-
) {
|
|
76
|
-
return Object.values(obj);
|
|
77
|
-
}
|
|
78
|
-
return { ...obj };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
65
|
/**
|
|
82
66
|
* Get a single string to represent all variables for a query
|
|
83
67
|
* NOTE: This isn't perfect because variables could have extra or unnnecessary keys/values
|
|
84
68
|
* The correct way to do this would be to compare it against the query {variableDefinitions}
|
|
85
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]
|
|
86
72
|
*/
|
|
87
73
|
|
|
88
74
|
export function makeVariablesKey(variables?: any) {
|
|
@@ -328,6 +314,9 @@ function saveQueryToMemory(
|
|
|
328
314
|
return;
|
|
329
315
|
}
|
|
330
316
|
|
|
317
|
+
// IMPORTANT NOTE:
|
|
318
|
+
// Queries are never cached unless they are manually set in [./config.ts]
|
|
319
|
+
|
|
331
320
|
const data = requestData[qryName];
|
|
332
321
|
const queryId = `#${qryName}:${variablesKey}`;
|
|
333
322
|
const selections = selection?.selectionSet?.selections;
|
|
@@ -362,8 +351,7 @@ function saveQueryToMemory(
|
|
|
362
351
|
// cacheMap[key](data[key], cacheObj[key], sel, variables);
|
|
363
352
|
if (saveQueryToCache) {
|
|
364
353
|
const customCacheData = cacheMap(data, cacheData, selections, variables);
|
|
365
|
-
// console.log('--->', queryId);
|
|
366
|
-
|
|
354
|
+
// console.log('1--->', queryId);
|
|
367
355
|
QUERIES.set(queryId, typeof customCacheData === 'undefined' ? null : customCacheData);
|
|
368
356
|
|
|
369
357
|
if (updateObservers) {
|
|
@@ -374,7 +362,8 @@ function saveQueryToMemory(
|
|
|
374
362
|
}
|
|
375
363
|
} else {
|
|
376
364
|
if (saveQueryToCache) {
|
|
377
|
-
// console.log('--->', queryId);
|
|
365
|
+
// console.log('2--->', queryId);
|
|
366
|
+
// console.log('2--->', cacheData);
|
|
378
367
|
QUERIES.set(queryId, cacheData);
|
|
379
368
|
}
|
|
380
369
|
|
|
@@ -448,10 +437,11 @@ export function appendFragmentToCache(cacheData: any) {
|
|
|
448
437
|
let cacheIsValid = true;
|
|
449
438
|
|
|
450
439
|
if (newObj[key].__list) {
|
|
451
|
-
cachedObj = newObj[key].data.map((di) => {
|
|
440
|
+
cachedObj = newObj[key].data.map((di: any[]) => {
|
|
452
441
|
let item = {};
|
|
453
442
|
di.forEach((d) => {
|
|
454
443
|
if (Array.isArray(d)) {
|
|
444
|
+
// @ts-expect-error
|
|
455
445
|
item[d[0]] = d[1];
|
|
456
446
|
} else if (typeof d === 'string') {
|
|
457
447
|
const frgIndex = d.indexOf(':');
|
|
@@ -488,8 +478,9 @@ export function appendFragmentToCache(cacheData: any) {
|
|
|
488
478
|
return item;
|
|
489
479
|
});
|
|
490
480
|
} else {
|
|
491
|
-
newObj[key].data.forEach((d) => {
|
|
481
|
+
newObj[key].data.forEach((d: any) => {
|
|
492
482
|
if (Array.isArray(d)) {
|
|
483
|
+
// @ts-expect-error
|
|
493
484
|
cachedObj[d[0]] = d[1];
|
|
494
485
|
} else if (typeof d === 'string') {
|
|
495
486
|
const frgIndex = d.indexOf(':');
|
|
@@ -562,7 +553,7 @@ export function loadDataFromCache(
|
|
|
562
553
|
|
|
563
554
|
for (const def of definitions) {
|
|
564
555
|
if (def.kind === 'OperationDefinition') {
|
|
565
|
-
def?.selectionSet?.selections.forEach((selection) => {
|
|
556
|
+
def?.selectionSet?.selections.forEach((selection: any) => {
|
|
566
557
|
const qryName = selection.name?.value;
|
|
567
558
|
if (!data[qryName]) {
|
|
568
559
|
const cacheKey = `#${qryName}:${variablesKey}`;
|
|
@@ -758,13 +749,13 @@ export function resetQuery(queryId: string, forceRefetch?: boolean, updateObserv
|
|
|
758
749
|
*/
|
|
759
750
|
|
|
760
751
|
export function updateFragment(
|
|
761
|
-
|
|
762
|
-
update: any | ((cache: any) =>
|
|
752
|
+
updateFragmentKey: string,
|
|
753
|
+
update: any | ((cache: any) => any),
|
|
763
754
|
replaceId?: string | null,
|
|
764
755
|
doNotTriggerObserver?: boolean,
|
|
765
756
|
updateObservers?: UpdateObserversFn,
|
|
766
757
|
) {
|
|
767
|
-
const cache = loadFragment(
|
|
758
|
+
const cache = loadFragment(updateFragmentKey);
|
|
768
759
|
|
|
769
760
|
if (cache && update) {
|
|
770
761
|
let updateObj;
|
|
@@ -776,9 +767,9 @@ export function updateFragment(
|
|
|
776
767
|
|
|
777
768
|
const updatedObj = { ...cache, ...updateObj };
|
|
778
769
|
|
|
779
|
-
FRAGMENTS.set(
|
|
770
|
+
FRAGMENTS.set(updateFragmentKey, updatedObj);
|
|
780
771
|
|
|
781
|
-
if (replaceId && replaceId !==
|
|
772
|
+
if (replaceId && replaceId !== updateFragmentKey) {
|
|
782
773
|
FRAGMENTS.set(replaceId, updatedObj);
|
|
783
774
|
|
|
784
775
|
// Must be deleted later, for the observer to work properly
|
|
@@ -787,15 +778,15 @@ export function updateFragment(
|
|
|
787
778
|
|
|
788
779
|
if (!doNotTriggerObserver && updateObservers) {
|
|
789
780
|
updateObservers({
|
|
790
|
-
fragmentIds: [
|
|
781
|
+
fragmentIds: [updateFragmentKey],
|
|
791
782
|
});
|
|
792
783
|
|
|
793
784
|
// FRAGMENT_OBSERVER.value = {
|
|
794
785
|
// count: FRAGMENT_OBSERVER.value.count + 1,
|
|
795
|
-
// list: [
|
|
786
|
+
// list: [updateFragmentKey],
|
|
796
787
|
// };
|
|
797
788
|
}
|
|
798
|
-
return
|
|
789
|
+
return updateFragmentKey || null;
|
|
799
790
|
}
|
|
800
791
|
return null;
|
|
801
792
|
}
|
|
@@ -806,7 +797,7 @@ export function updateFragment(
|
|
|
806
797
|
|
|
807
798
|
export function updateQuery(
|
|
808
799
|
queryId: string,
|
|
809
|
-
update: any | ((cache: any) =>
|
|
800
|
+
update: any | ((cache: any) => any),
|
|
810
801
|
doNotTriggerObserver?: boolean,
|
|
811
802
|
updateObservers?: UpdateObserversFn,
|
|
812
803
|
) {
|