@jsb188/graphql 1.1.4 → 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.
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,10 +27,9 @@ export const RULES = {
26
27
  chatsList: true,
27
28
  editChat: true,
28
29
  friend: true,
29
- friendsList: true,
30
- friendRequests: true,
31
30
  logEntries: true,
32
31
  myChats: true,
32
+ organizationRelationship: true,
33
33
  platformProducts: true,
34
34
  searchUsers: true,
35
35
  updateNote: true,
@@ -38,7 +38,6 @@ export const RULES = {
38
38
  // All fragments are cached,
39
39
  // but if you want to skip some, add it here
40
40
  accountAuthFragment: false,
41
- userAuthFragment: false,
42
41
  } as {
43
42
  __limit: number;
44
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
  /**
@@ -192,7 +220,6 @@ function mapSelections(data: any, innerSelections: any[], updatedKeys: string[]
192
220
  // Because partials might have data that's not in the main fragment
193
221
  const currentData = loadFragment(fragmentKey);
194
222
 
195
- // FRAGMENTS.delete(fragmentKey);
196
223
  FRAGMENTS.set(fragmentKey, mergeNestedObjects(currentData, extractFragment(data, innerSelections)));
197
224
  updatedKeys.push(fragmentKey);
198
225
 
@@ -277,7 +304,6 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
277
304
  // Because partials might have data that's not in the main fragment
278
305
  const currentData = loadFragment(fragmentKey);
279
306
 
280
- // FRAGMENTS.delete(fragmentKey);
281
307
  FRAGMENTS.set(fragmentKey, mergeNestedObjects(currentData, extractFragment(data, selections)));
282
308
  updatedKeys.push(fragmentKey);
283
309
  }
@@ -303,6 +329,28 @@ function makeCacheObject(data: any, selections: any[], variables?: any, cacheMap
303
329
  }
304
330
  }
305
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
+
306
354
  return [cacheObj, updatedKeys];
307
355
  }
308
356
 
@@ -439,19 +487,74 @@ export function setDataToCache(
439
487
  export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
440
488
  let newObj = cloneArrayLike(cacheData);
441
489
 
490
+ // if (testMode) {
491
+ // console.log('appendFragmentToCache:', newObj);
492
+ // }
493
+
442
494
  for (const key in newObj) {
443
- if (newObj[key] && typeof newObj[key] === 'object' && newObj[key].__cache && newObj[key].data) {
495
+ if (newObj[key] && typeof newObj[key] === 'object') {
444
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
+ });
445
543
 
446
- let cachedObj = {};
447
- 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
+ // }
448
555
 
449
- if (newObj[key].__list) {
450
- cachedObj = newObj[key].data.map((di: any[]) => {
451
- let item = {};
452
- di.forEach((d) => {
453
556
  if (Array.isArray(d)) {
454
- item[d[0]] = d[1];
557
+ cachedObj[d[0]] = d[1];
455
558
  } else if (typeof d === 'string') {
456
559
  const frgIndex = d.indexOf(':');
457
560
  const frgName = d.substring(1, frgIndex);
@@ -466,85 +569,57 @@ export function appendFragmentToCache(cacheData: any, testMode?: boolean) {
466
569
 
467
570
  const cachedFragment = loadFragment(fragmentKey);
468
571
  if (DO_LOG[0]) {
469
- console.log('LOAD (1): ', d);
572
+ console.log('LOAD (2): ', d);
470
573
  }
471
574
  if (DO_LOG[1]) {
472
- console.log('LOAD (1): ', cachedFragment);
575
+ console.log('LOAD (2): ', cachedFragment);
473
576
  }
474
577
 
475
578
  if (cachedFragment) {
476
- item = {
477
- ...item,
478
- ...cachedFragment,
479
- };
579
+ if (isFlat) {
580
+ newObj = {
581
+ ...newObj,
582
+ ...cachedFragment,
583
+ };
584
+ delete newObj.__flat;
585
+ } else {
586
+ cachedObj = {
587
+ ...cachedObj,
588
+ ...cachedFragment,
589
+ };
590
+ }
480
591
  } else {
481
592
  // Cache was invalidated
482
593
  cacheIsValid = false;
483
594
  }
484
595
  }
485
596
  });
597
+ }
486
598
 
487
- return item;
488
- });
489
- } else {
490
- newObj[key].data.forEach((d: any) => {
491
-
492
-
493
- // if (testMode) {
494
- // console.log('cachedObj', cachedObj);
495
- // console.log('d', d);
496
- // console.log('d', typeof d);
497
- // }
498
-
499
- if (Array.isArray(d)) {
500
- cachedObj[d[0]] = d[1];
501
- } else if (typeof d === 'string') {
502
- const frgIndex = d.indexOf(':');
503
- const frgName = d.substring(1, frgIndex);
504
- const frgMap = PARTIALS_MAP[frgName];
505
-
506
- let fragmentKey;
507
- if (frgMap) {
508
- fragmentKey = `$${frgMap}:${d.substring(frgIndex + 1)}`;
509
- } else {
510
- fragmentKey = d;
511
- }
599
+ if (!cacheIsValid) {
600
+ return null;
601
+ }
512
602
 
513
- const cachedFragment = loadFragment(fragmentKey);
514
- if (DO_LOG[0]) {
515
- console.log('LOAD (2): ', d);
516
- }
517
- if (DO_LOG[1]) {
518
- console.log('LOAD (2): ', cachedFragment);
519
- }
603
+ if (!isFlat) {
604
+ newObj[key] = cachedObj;
605
+ }
606
+ } else {
520
607
 
521
- if (cachedFragment) {
522
- if (isFlat) {
523
- newObj = {
524
- ...newObj,
525
- ...cachedFragment,
526
- };
527
- delete newObj.__flat;
528
- } else {
529
- cachedObj = {
530
- ...cachedObj,
531
- ...cachedFragment,
532
- };
533
- }
534
- } else {
535
- // Cache was invalidated
536
- 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];
537
620
  }
538
- }
539
- });
540
- }
541
-
542
- if (!cacheIsValid) {
543
- return null;
544
- }
545
-
546
- if (!isFlat) {
547
- newObj[key] = cachedObj;
621
+ });
622
+ }
548
623
  }
549
624
  }
550
625
  }
@@ -562,19 +637,12 @@ export function loadDataFromCache(
562
637
  variablesKey: string,
563
638
  cacheMap?: any,
564
639
  eagerFragmentKeyMap?: any,
640
+ testMode?: boolean,
565
641
  ) {
566
642
  // return query[variablesKey];
567
643
  const data = { ...requestData };
568
644
  const definitions = gqlQuery.definitions;
569
645
 
570
-
571
- // console.log('loadDataFromCache()');
572
- // console.log(data);
573
- // console.log(definitions);
574
-
575
-
576
-
577
-
578
646
  for (const def of definitions) {
579
647
  if (def.kind === 'OperationDefinition') {
580
648
  def?.selectionSet?.selections.forEach((selection: any) => {
@@ -701,6 +769,7 @@ export function removeStaleCache() {
701
769
 
702
770
  for (const key of keysToEvict) {
703
771
  FRAGMENTS.delete(key);
772
+ FRAGMENTS.delete(`${key}.spreads`);
704
773
  }
705
774
  }
706
775
  }
@@ -784,7 +853,7 @@ export function updateFragment(
784
853
  if (cache && update) {
785
854
  let updateObj;
786
855
  if (typeof update === 'function') {
787
- updateObj = update(cache);
856
+ updateObj = update(mapArrayLikeObjects(cache));
788
857
  } else {
789
858
  updateObj = update;
790
859
  }
@@ -795,9 +864,6 @@ export function updateFragment(
795
864
 
796
865
  if (replaceId && replaceId !== updateFragmentKey) {
797
866
  FRAGMENTS.set(replaceId, updatedObj);
798
-
799
- // Must be deleted later, for the observer to work properly
800
- // setTimeout(() => FRAGMENTS.delete(id), 1000);
801
867
  }
802
868
 
803
869
  if (!doNotTriggerObserver && updateObservers) {
@@ -829,7 +895,7 @@ export function updateQuery(
829
895
  if (cache && update) {
830
896
  let updateObj;
831
897
  if (typeof update === 'function') {
832
- updateObj = update(cache);
898
+ updateObj = update(mapArrayLikeObjects(cache));
833
899
  } else {
834
900
  updateObj = update;
835
901
  }
@@ -871,6 +937,5 @@ export function triggerObserver(ids: (string | null)[], updateObservers?: Update
871
937
  */
872
938
 
873
939
  export function addFragmentToCache(id: string, obj: any) {
874
- // FRAGMENTS.delete(id);
875
940
  FRAGMENTS.set(id, obj);
876
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
  }`;
@@ -3,7 +3,8 @@ export const logEntryFragment = `fragment logEntryFragment on LogEntry {
3
3
  id
4
4
  accountId
5
5
  organizationId
6
-
6
+ cursor
7
+ date
7
8
  createdAt
8
9
  updatedAt
9
10
  }`;
@@ -14,13 +15,11 @@ export const logEntryArableFragment = `fragment logEntryArableFragment on LogEnt
14
15
  id
15
16
  type
16
17
  activity
18
+ concentration
19
+ concentrationUnit
20
+ crop
17
21
  quantity
18
22
  unit
19
23
  price
20
24
  notes
21
-
22
- crop {
23
- id
24
- name
25
- }
26
25
  }`;
@@ -2,9 +2,15 @@
2
2
  export const organizationFragment = `fragment organizationFragment on Organization {
3
3
  id
4
4
  stripeCustomerId
5
- operation
6
5
  name
6
+ operation
7
+ dailyDigestTime
7
8
  domains
9
+
10
+ settings {
11
+ emoji
12
+ timeZone
13
+ }
8
14
  }`;
9
15
 
10
16
  export const organizationRelationshipFragment = `fragment organizationRelationshipFragment on OrganizationRelationship {
@@ -15,11 +21,12 @@ export const organizationRelationshipFragment = `fragment organizationRelationsh
15
21
  acl {
16
22
  id
17
23
  billing
18
- journal
24
+ digests
25
+ logs
19
26
  members
20
27
  finances
21
- products
22
28
  settings
23
29
  integrations
30
+ reminders
24
31
  }
25
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
  }
@@ -30,7 +30,7 @@ mutation authenticateWithPassword (
30
30
  $password: String!
31
31
  ) {
32
32
  authenticateWithPassword (
33
- identifier: $identifier,
33
+ identifier: $identifier
34
34
  password: $password
35
35
  ) {
36
36
  ${authenticateResultText}
@@ -52,6 +52,82 @@ mutation authenticateWithToken (
52
52
  ${authenticateFragments}
53
53
  `;
54
54
 
55
+ export const continueWithGoogleMtn = gql`
56
+ mutation continueWithGoogle (
57
+ $oauthToken: String!
58
+ ) {
59
+ continueWithGoogle (
60
+ oauthToken: $oauthToken
61
+ ) {
62
+ ${authenticateResultText}
63
+ }
64
+ }
65
+ ${authenticateFragments}
66
+ `;
67
+
68
+ export const checkUsernameOrEmailMtn = gql`
69
+ mutation checkUsernameOrEmail (
70
+ $usernameOrEmail: String!
71
+ $authCheck: Boolean
72
+ ) {
73
+ checkUsernameOrEmail (
74
+ usernameOrEmail: $usernameOrEmail
75
+ authCheck: $authCheck
76
+ ) {
77
+ value
78
+ type
79
+ hasPassword
80
+ taken
81
+ }
82
+ }`;
83
+
84
+ export const changeAccountPasswordMtn = gql`
85
+ mutation changeAccountPassword (
86
+ $password: String!
87
+ $currentPassword: String
88
+ $token: Token
89
+ ) {
90
+ changeAccountPassword (
91
+ password: $password
92
+ currentPassword: $currentPassword
93
+ token: $token
94
+ )
95
+ }`;
96
+
97
+ export const requestTokenizedEmailMtn = gql`
98
+ mutation requestTokenizedEmail (
99
+ $email: String!
100
+ $requestType: TokenType!
101
+ ) {
102
+ requestTokenizedEmail (
103
+ email: $email
104
+ requestType: $requestType
105
+ )
106
+ }`;
107
+
108
+ export const sendPhoneVerificationCodeMtn = gql`
109
+ mutation sendPhoneVerificationCode (
110
+ $phone: String!
111
+ ) {
112
+ sendPhoneVerificationCode (
113
+ phone: $phone
114
+ )
115
+ }`;
116
+
117
+ export const confirmPhoneVerificationCodeMtn = gql`
118
+ mutation confirmPhoneVerificationCode (
119
+ $phone: String!
120
+ $code: String!
121
+ ) {
122
+ confirmPhoneVerificationCode (
123
+ phone: $phone
124
+ code: $code
125
+ ) {
126
+ success
127
+ token
128
+ }
129
+ }`;
130
+
55
131
  export const sendEmailVerificationMtn = gql`
56
132
  mutation sendEmailVerification (
57
133
  $userId: UserID!
@@ -79,19 +155,6 @@ mutation signUpWithEmail (
79
155
  )
80
156
  }`;
81
157
 
82
- export const signInWithGoogleMtn = gql`
83
- mutation signInWithGoogle (
84
- $oauthToken: String!
85
- ) {
86
- signInWithGoogle (
87
- oauthToken: $oauthToken
88
- ) {
89
- ${authenticateResultText}
90
- }
91
- }
92
- ${authenticateFragments}
93
- `;
94
-
95
158
  export const signInWithAppleMtn = gql`
96
159
  mutation signInWithApple (
97
160
  $code: String!
@@ -0,0 +1,7 @@
1
+ export * from './accountMutations';
2
+ export * from './aiChatMutations';
3
+ export * from './authMutations';
4
+ export * from './billingMutations';
5
+ export * from './logMutations';
6
+ export * from './otherMutations';
7
+ export * from './userAuthMutations';
@@ -1,2 +1,43 @@
1
+ import { gql } from 'graphql-tag';
2
+ import { logEntryFragment, logEntryArableFragment } from '../fragments/logFragments';
1
3
 
2
- // Insert journal mutations here
4
+ export const editLogEntryMtn = gql`
5
+ mutation editLogEntry (
6
+ $logEntryId: GenericID!
7
+ $accountId: GenericID
8
+ $date: DateTime
9
+
10
+ $arableDetails: LogEntryArableInput
11
+ $livestockDetails: LogEntryLivestockInput
12
+ ) {
13
+ editLogEntry (
14
+ logEntryId: $logEntryId
15
+ accountId: $accountId
16
+ date: $date
17
+
18
+ arableDetails: $arableDetails
19
+ livestockDetails: $livestockDetails
20
+ ) {
21
+ ...logEntryFragment
22
+
23
+ details {
24
+ ...on LogEntryArable {
25
+ ...logEntryArableFragment
26
+ }
27
+ }
28
+ }
29
+ }
30
+
31
+ ${logEntryFragment}
32
+ ${logEntryArableFragment}
33
+ `;
34
+
35
+ export const deleteLogEntryMtn = gql`
36
+ mutation deleteLogEntry (
37
+ $logEntryId: GenericID!
38
+ ) {
39
+ deleteLogEntry (
40
+ logEntryId: $logEntryId
41
+ )
42
+ }
43
+ `;