@jsb188/graphql 1.1.3 → 1.1.5

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.
Files changed (39) hide show
  1. package/cache/config.ts +3 -3
  2. package/cache/index.ts +173 -85
  3. package/gql/fragments/accountAuthFragments.ts +3 -9
  4. package/gql/fragments/accountFragments.ts +11 -0
  5. package/gql/fragments/logFragments.ts +25 -0
  6. package/gql/fragments/organizationFragments.ts +10 -2
  7. package/gql/mutations/accountMutations.ts +21 -0
  8. package/gql/mutations/aiChatMutations.ts +1 -0
  9. package/gql/mutations/authMutations.ts +171 -0
  10. package/gql/mutations/index.ts +7 -0
  11. package/gql/mutations/logMutations.ts +43 -0
  12. package/gql/mutations/userAuthMutations.ts +0 -163
  13. package/gql/queries/aiChatQueries.ts +1 -0
  14. package/gql/queries/appQueries.ts +2 -1
  15. package/gql/queries/logQueries.ts +37 -0
  16. package/gql/queries/organizationQueries.ts +1 -0
  17. package/hooks/index.ts +134 -75
  18. package/hooks/use-account-mtn.ts +43 -0
  19. package/hooks/use-aiChat-mtn.ts +96 -0
  20. package/hooks/use-aiChat-qry.ts +65 -3
  21. package/hooks/use-auth-mtn.ts +99 -112
  22. package/hooks/use-log-mtn.ts +85 -0
  23. package/hooks/use-log-qry.ts +77 -0
  24. package/package.json +10 -20
  25. package/types.d.ts +1 -0
  26. package/gql/fragments/cardFragments.ts +0 -15
  27. package/gql/fragments/chatFragments.ts +0 -98
  28. package/gql/fragments/journalFragments.ts +0 -14
  29. package/gql/fragments/userAuthFragments.ts +0 -8
  30. package/gql/fragments/userFragments.ts +0 -69
  31. package/gql/mutations/accountAuthMutations.ts +0 -81
  32. package/gql/mutations/chatMutations.ts +0 -116
  33. package/gql/mutations/journalMutations.ts +0 -2
  34. package/gql/mutations/userMutations.ts +0 -89
  35. package/gql/queries/cardQueries.ts +0 -24
  36. package/gql/queries/chatQueries.ts +0 -86
  37. package/gql/queries/journalQueries.ts +0 -28
  38. package/gql/queries/userQueries.ts +0 -104
  39. package/hooks/use-journal-qry.ts +0 -50
package/cache/config.ts CHANGED
@@ -19,6 +19,7 @@ export const RULES = {
19
19
  // List of queries that should be cached
20
20
  // NOTE: Fragments are always cached
21
21
  aiChat: true,
22
+ aiChats: true,
22
23
  aiChatMessages: true,
23
24
  alerts: true,
24
25
  cardsForUser: true,
@@ -26,9 +27,9 @@ export const RULES = {
26
27
  chatsList: true,
27
28
  editChat: true,
28
29
  friend: true,
29
- friendsList: true,
30
- friendRequests: true,
30
+ logEntries: true,
31
31
  myChats: true,
32
+ organizationRelationship: true,
32
33
  platformProducts: true,
33
34
  searchUsers: true,
34
35
  updateNote: true,
@@ -37,7 +38,6 @@ export const RULES = {
37
38
  // All fragments are cached,
38
39
  // but if you want to skip some, add it here
39
40
  accountAuthFragment: false,
40
- userAuthFragment: false,
41
41
  } as {
42
42
  __limit: number;
43
43
  [key: string]: boolean | number;
package/cache/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { cloneArrayLike } from '@jsb188/app/utils/object';
1
+ import { cloneArrayLike, mapArrayLikeObjects } from '@jsb188/app/utils/object';
2
2
  import type { UpdateObserversFn } from '../types.d';
3
- import { RULES, PARTIALS_MAP } from './config';
3
+ import { PARTIALS_MAP, RULES } from './config';
4
4
 
5
5
  const DO_LOG = [
6
6
  false, // [0] Log name
@@ -76,7 +76,7 @@ export function makeVariablesKey(variables?: any) {
76
76
  const key = Object.keys(variables).sort().reduce((acc, key) => {
77
77
  const value = variables[key];
78
78
  if (value || value === 0 || value === false) {
79
- return `${acc}$${key}:${value}`;
79
+ return `${acc}$${key}:${typeof value === 'object' ? JSON.stringify(value) : value}`;
80
80
  }
81
81
  return acc;
82
82
  }, '');
@@ -117,6 +117,10 @@ function extractFragment(dataObj: any, innerSelection: any[]) {
117
117
  export function loadQuery(queryKey: string, referencesOnly?: boolean) {
118
118
  const references = QUERIES.get(queryKey);
119
119
 
120
+ // if (testMode) {
121
+ // console.log('references>>>>:', references);
122
+ // }
123
+
120
124
  if (referencesOnly) {
121
125
  return references;
122
126
  } else if (!references) {
@@ -158,7 +162,31 @@ export function loadQuery(queryKey: string, referencesOnly?: boolean) {
158
162
  */
159
163
 
160
164
  export function loadFragment(id: string) {
161
- return enforceArrayType(FRAGMENTS.get(id));
165
+ // console.log('load fragment:', id, FRAGMENTS.get(id));
166
+
167
+ const fragment = enforceArrayType(FRAGMENTS.get(id));
168
+ if (!fragment) {
169
+ return null;
170
+
171
+ }
172
+
173
+ const spreads = FRAGMENTS.get(`${id}.spreads`);
174
+ if (!spreads) {
175
+ return fragment;
176
+ }
177
+
178
+ let fragmentWithSpreads = { ...fragment };
179
+ for (const key in spreads) {
180
+ if (Object.prototype.hasOwnProperty.call(spreads, key)) {
181
+ const spreadId = spreads[key];
182
+ const spreadData = enforceArrayType(FRAGMENTS.get(spreadId));
183
+ if (spreadData) {
184
+ fragmentWithSpreads[key] = spreadData;
185
+ }
186
+ }
187
+ }
188
+
189
+ return fragmentWithSpreads;
162
190
  }
163
191
 
164
192
  /**
@@ -167,10 +195,13 @@ export function loadFragment(id: string) {
167
195
 
168
196
  function mapSelections(data: any, innerSelections: any[], updatedKeys: string[] = []) {
169
197
  return innerSelections.map((inner) => {
198
+
199
+ let fragmentKey;
170
200
  if (inner.kind === 'FragmentSpread') {
201
+ // These are fragment spreads
171
202
  const frgName = inner.name.value;
172
203
  const frgMap = PARTIALS_MAP[frgName];
173
- const fragmentKey = `$${frgMap || frgName}:${data.id || 'none'}`;
204
+ fragmentKey = `$${frgMap || frgName}:${data.id || 'none'}`;
174
205
 
175
206
  if (DO_LOG[0]) {
176
207
  console.log('SETTING (1): ', fragmentKey);
@@ -178,23 +209,28 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
178
209
  if (DO_LOG[1]) {
179
210
  console.log('SETTING (1): ', extractFragment(data, innerSelections));
180
211
  }
212
+ } else if (inner.kind === 'InlineFragment') {
213
+ // This is Union, etc
214
+ const frgNamesJoined = inner.selectionSet.selections.map((sel: any) => PARTIALS_MAP[sel.name.value] || sel.name.value).join('.');
215
+ fragmentKey = `$${frgNamesJoined}:${data.id || 'none'}`;
216
+ }
181
217
 
182
- // if (key === 'messages') {
183
- // console.log('---->', data.id);
184
- // }
185
-
218
+ if (fragmentKey) {
186
219
  // Whether fragment is partial or not, always merge data;
187
220
  // Because partials might have data that's not in the main fragment
188
221
  const currentData = loadFragment(fragmentKey);
189
222
 
190
- // FRAGMENTS.delete(fragmentKey);
191
223
  FRAGMENTS.set(fragmentKey, mergeNestedObjects(currentData, extractFragment(data, innerSelections)));
192
224
  updatedKeys.push(fragmentKey);
193
225
 
194
226
  return fragmentKey;
195
227
  }
196
228
 
197
- return [inner.name.value, data?.[inner.name.value]];
229
+ // These are non-fragment fields
230
+ return [
231
+ inner.name.value,
232
+ data?.[inner.name.value]
233
+ ];
198
234
  // return {
199
235
  // name: inner.name.value,
200
236
  // value: data[key]?.[inner.name.value]
@@ -268,7 +304,6 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
268
304
  // Because partials might have data that's not in the main fragment
269
305
  const currentData = loadFragment(fragmentKey);
270
306
 
271
- // FRAGMENTS.delete(fragmentKey);
272
307
  FRAGMENTS.set(fragmentKey, mergeNestedObjects(currentData, extractFragment(data, selections)));
273
308
  updatedKeys.push(fragmentKey);
274
309
  }
@@ -294,6 +329,28 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
294
329
  }
295
330
  }
296
331
 
332
+ if (cacheObj.__flat?.data?.[0]) {
333
+ const spreadsFragmentKey = `${cacheObj.__flat.data[0]}.spreads`;
334
+
335
+ let spreads: Record<string, string> | undefined;
336
+ for (const cKey in cacheObj) {
337
+ if (
338
+ cKey !== '__flat' &&
339
+ Object.prototype.hasOwnProperty.call(cacheObj, cKey) &&
340
+ typeof cacheObj[cKey]?.data?.[0] === 'string'
341
+ ) {
342
+ if (!spreads) {
343
+ spreads = {};
344
+ }
345
+ spreads[cKey] = cacheObj[cKey].data[0];
346
+ }
347
+ }
348
+
349
+ if (spreads) {
350
+ FRAGMENTS.set(spreadsFragmentKey, spreads);
351
+ }
352
+ }
353
+
297
354
  return [cacheObj, updatedKeys];
298
355
  }
299
356
 
@@ -427,23 +484,77 @@ export function setDataToCache(
427
484
  * Append fragment to cache
428
485
  */
429
486
 
430
- export function appendFragmentToCache(cacheData: any) {
487
+ export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
431
488
  let newObj = cloneArrayLike(cacheData);
432
489
 
490
+ // if (testMode) {
491
+ // console.log('appendFragmentToCache:', newObj);
492
+ // }
493
+
433
494
  for (const key in newObj) {
434
- if (newObj[key] && typeof newObj[key] === 'object' && newObj[key].__cache && newObj[key].data) {
495
+ if (newObj[key] && typeof newObj[key] === 'object') {
435
496
  const isFlat = key === '__flat';
497
+ const isCache = newObj[key].__cache && newObj[key].data;
498
+
499
+ if (isCache) {
500
+
501
+ let cachedObj = {};
502
+ let cacheIsValid = true;
503
+
504
+ if (newObj[key].__list) {
505
+
506
+ cachedObj = newObj[key].data.map((di: any[]) => {
507
+
508
+ let item = {};
509
+ di.forEach((d) => {
510
+ if (Array.isArray(d)) {
511
+ item[d[0]] = d[1];
512
+ } else if (typeof d === 'string') {
513
+ const frgIndex = d.indexOf(':');
514
+ const frgName = d.substring(1, frgIndex);
515
+ const frgMap = PARTIALS_MAP[frgName];
516
+
517
+ let fragmentKey;
518
+ if (frgMap) {
519
+ fragmentKey = `$${frgMap}:${d.substring(frgIndex + 1)}`;
520
+ } else {
521
+ fragmentKey = d;
522
+ }
523
+
524
+ const cachedFragment = loadFragment(fragmentKey);
525
+ if (DO_LOG[0]) {
526
+ console.log('LOAD (1): ', d);
527
+ }
528
+ if (DO_LOG[1]) {
529
+ console.log('LOAD (1): ', cachedFragment);
530
+ }
531
+
532
+ if (cachedFragment) {
533
+ item = {
534
+ ...item,
535
+ ...cachedFragment,
536
+ };
537
+ } else {
538
+ // Cache was invalidated
539
+ cacheIsValid = false;
540
+ }
541
+ }
542
+ });
436
543
 
437
- let cachedObj = {};
438
- let cacheIsValid = true;
544
+ return item;
545
+ });
546
+ } else {
547
+ newObj[key].data.forEach((d: any) => {
548
+
549
+ // if (testMode) {
550
+ // console.log('cachedObj', cachedObj);
551
+ // console.log('key', key);
552
+ // console.log('d', d);
553
+ // console.log('d', typeof d);
554
+ // }
439
555
 
440
- if (newObj[key].__list) {
441
- cachedObj = newObj[key].data.map((di: any[]) => {
442
- let item = {};
443
- di.forEach((d) => {
444
556
  if (Array.isArray(d)) {
445
- // @ts-expect-error
446
- item[d[0]] = d[1];
557
+ cachedObj[d[0]] = d[1];
447
558
  } else if (typeof d === 'string') {
448
559
  const frgIndex = d.indexOf(':');
449
560
  const frgName = d.substring(1, frgIndex);
@@ -458,78 +569,57 @@ export function appendFragmentToCache(cacheData: any) {
458
569
 
459
570
  const cachedFragment = loadFragment(fragmentKey);
460
571
  if (DO_LOG[0]) {
461
- console.log('LOAD (1): ', d);
572
+ console.log('LOAD (2): ', d);
462
573
  }
463
574
  if (DO_LOG[1]) {
464
- console.log('LOAD (1): ', cachedFragment);
575
+ console.log('LOAD (2): ', cachedFragment);
465
576
  }
466
577
 
467
578
  if (cachedFragment) {
468
- item = {
469
- ...item,
470
- ...cachedFragment,
471
- };
579
+ if (isFlat) {
580
+ newObj = {
581
+ ...newObj,
582
+ ...cachedFragment,
583
+ };
584
+ delete newObj.__flat;
585
+ } else {
586
+ cachedObj = {
587
+ ...cachedObj,
588
+ ...cachedFragment,
589
+ };
590
+ }
472
591
  } else {
473
592
  // Cache was invalidated
474
593
  cacheIsValid = false;
475
594
  }
476
595
  }
477
596
  });
597
+ }
478
598
 
479
- return item;
480
- });
481
- } else {
482
- newObj[key].data.forEach((d: any) => {
483
- if (Array.isArray(d)) {
484
- // @ts-expect-error
485
- cachedObj[d[0]] = d[1];
486
- } else if (typeof d === 'string') {
487
- const frgIndex = d.indexOf(':');
488
- const frgName = d.substring(1, frgIndex);
489
- const frgMap = PARTIALS_MAP[frgName];
490
-
491
- let fragmentKey;
492
- if (frgMap) {
493
- fragmentKey = `$${frgMap}:${d.substring(frgIndex + 1)}`;
494
- } else {
495
- fragmentKey = d;
496
- }
599
+ if (!cacheIsValid) {
600
+ return null;
601
+ }
497
602
 
498
- const cachedFragment = loadFragment(fragmentKey);
499
- if (DO_LOG[0]) {
500
- console.log('LOAD (2): ', d);
501
- }
502
- if (DO_LOG[1]) {
503
- console.log('LOAD (2): ', cachedFragment);
504
- }
603
+ if (!isFlat) {
604
+ newObj[key] = cachedObj;
605
+ }
606
+ } else {
505
607
 
506
- if (cachedFragment) {
507
- if (isFlat) {
508
- newObj = {
509
- ...newObj,
510
- ...cachedFragment,
511
- };
512
- delete newObj.__flat;
513
- } else {
514
- cachedObj = {
515
- ...cachedObj,
516
- ...cachedFragment,
517
- };
518
- }
519
- } else {
520
- // Cache was invalidated
521
- cacheIsValid = false;
608
+ // Fragment spreads recursively loads the fragment data
609
+ // So we have to get the non-spread fields from query cache
610
+
611
+ const cacheInfo = cacheData[key];
612
+ if (newObj[key] && typeof newObj[key] === 'object' && cacheInfo?.__cache && cacheInfo.data) {
613
+ cacheInfo.data.forEach((di: any) => {
614
+ if (
615
+ Array.isArray(di) &&
616
+ di.length === 2 &&
617
+ !di?.[0]?.startsWith('$')
618
+ ) {
619
+ newObj[key][di[0]] = di[1];
522
620
  }
523
- }
524
- });
525
- }
526
-
527
- if (!cacheIsValid) {
528
- return null;
529
- }
530
-
531
- if (!isFlat) {
532
- newObj[key] = cachedObj;
621
+ });
622
+ }
533
623
  }
534
624
  }
535
625
  }
@@ -547,6 +637,7 @@ export function loadDataFromCache(
547
637
  variablesKey: string,
548
638
  cacheMap?: any,
549
639
  eagerFragmentKeyMap?: any,
640
+ testMode?: boolean,
550
641
  ) {
551
642
  // return query[variablesKey];
552
643
  const data = { ...requestData };
@@ -678,6 +769,7 @@ export function removeStaleCache() {
678
769
 
679
770
  for (const key of keysToEvict) {
680
771
  FRAGMENTS.delete(key);
772
+ FRAGMENTS.delete(`${key}.spreads`);
681
773
  }
682
774
  }
683
775
  }
@@ -761,7 +853,7 @@ export function updateFragment(
761
853
  if (cache && update) {
762
854
  let updateObj;
763
855
  if (typeof update === 'function') {
764
- updateObj = update(cache);
856
+ updateObj = update(mapArrayLikeObjects(cache));
765
857
  } else {
766
858
  updateObj = update;
767
859
  }
@@ -772,9 +864,6 @@ export function updateFragment(
772
864
 
773
865
  if (replaceId && replaceId !== updateFragmentKey) {
774
866
  FRAGMENTS.set(replaceId, updatedObj);
775
-
776
- // Must be deleted later, for the observer to work properly
777
- // setTimeout(() => FRAGMENTS.delete(id), 1000);
778
867
  }
779
868
 
780
869
  if (!doNotTriggerObserver && updateObservers) {
@@ -806,7 +895,7 @@ export function updateQuery(
806
895
  if (cache && update) {
807
896
  let updateObj;
808
897
  if (typeof update === 'function') {
809
- updateObj = update(cache);
898
+ updateObj = update(mapArrayLikeObjects(cache));
810
899
  } else {
811
900
  updateObj = update;
812
901
  }
@@ -848,6 +937,5 @@ export function triggerObserver(ids: (string | null)[], updateObservers?: Update
848
937
  */
849
938
 
850
939
  export function addFragmentToCache(id: string, obj: any) {
851
- // FRAGMENTS.delete(id);
852
940
  FRAGMENTS.set(id, obj);
853
941
  }
@@ -3,14 +3,8 @@ export const accountAuthFragment = `fragment accountAuthFragment on AccountAuth
3
3
  primaryOrganizationId
4
4
  activated
5
5
  hasPassword
6
- version
7
6
 
8
- settings {
9
- theme
10
- lightMode
11
- timeZone
12
- showSelfAvatar
13
- isBubbleOther
14
- showOtherAvatar
15
- }
7
+ alertUpdatesOnMount
8
+ webVersion
9
+ mobileVersion
16
10
  }`;
@@ -1,6 +1,7 @@
1
1
  export const accountFragment = `fragment accountFragment on Account {
2
2
  id
3
3
  deleted
4
+ color
4
5
 
5
6
  profile {
6
7
  id
@@ -10,4 +11,14 @@ export const accountFragment = `fragment accountFragment on Account {
10
11
  photoId
11
12
  photoUri
12
13
  }
14
+
15
+ settings {
16
+ theme
17
+ lightMode
18
+ timeZone
19
+ language
20
+ showSelfAvatar
21
+ isBubbleOther
22
+ showOtherAvatar
23
+ }
13
24
  }`;
@@ -0,0 +1,25 @@
1
+
2
+ export const logEntryFragment = `fragment logEntryFragment on LogEntry {
3
+ id
4
+ accountId
5
+ organizationId
6
+ cursor
7
+ date
8
+ createdAt
9
+ updatedAt
10
+ }`;
11
+
12
+ export const logEntryArableFragment = `fragment logEntryArableFragment on LogEntryArable {
13
+ __typename
14
+
15
+ id
16
+ type
17
+ activity
18
+ concentration
19
+ concentrationUnit
20
+ crop
21
+ quantity
22
+ unit
23
+ price
24
+ notes
25
+ }`;
@@ -3,7 +3,14 @@ export const organizationFragment = `fragment organizationFragment on Organizati
3
3
  id
4
4
  stripeCustomerId
5
5
  name
6
+ operation
7
+ dailyDigestTime
6
8
  domains
9
+
10
+ settings {
11
+ emoji
12
+ timeZone
13
+ }
7
14
  }`;
8
15
 
9
16
  export const organizationRelationshipFragment = `fragment organizationRelationshipFragment on OrganizationRelationship {
@@ -14,11 +21,12 @@ export const organizationRelationshipFragment = `fragment organizationRelationsh
14
21
  acl {
15
22
  id
16
23
  billing
17
- journal
24
+ digests
25
+ logs
18
26
  members
19
27
  finances
20
- products
21
28
  settings
22
29
  integrations
30
+ reminders
23
31
  }
24
32
  }`;
@@ -0,0 +1,21 @@
1
+ import { gql } from 'graphql-tag';
2
+ import { accountFragment } from '../fragments/accountFragments';
3
+
4
+ export const editAccountMtn = gql`
5
+ mutation editAccount (
6
+ $accountId: GenericID
7
+ $photoId: GenericID
8
+ $profile: AccountProfileInput
9
+ $settings: AccountSettingsInput
10
+ ) {
11
+ editAccount (
12
+ accountId: $accountId
13
+ photoId: $photoId
14
+ profile: $profile
15
+ settings: $settings
16
+ ) {
17
+ ...accountFragment
18
+ }
19
+ }
20
+ ${accountFragment}
21
+ `;
@@ -12,6 +12,7 @@ mutation startAIChat (
12
12
  ) {
13
13
  sessionKey
14
14
  node {
15
+ cursor
15
16
  ...aiChatFragment
16
17
  }
17
18
  }