@jsb188/graphql 1.1.11 → 1.1.13

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 (174) hide show
  1. package/LICENSE +21 -0
  2. package/audience.d.ts +205 -0
  3. package/cache/config.ts +65 -28
  4. package/cache/index.helpers.ts +96 -0
  5. package/cache/index.ts +364 -808
  6. package/cache/mounted-queries.ts +33 -0
  7. package/cache/optimistic-ui-runtime.ts +46 -0
  8. package/cache/optimistic-ui.ts +401 -0
  9. package/cache/persist.ts +107 -22
  10. package/cache/realtime-fence.ts +47 -0
  11. package/cache/realtime-live.ts +17 -0
  12. package/cache/store.ts +22 -64
  13. package/cache/value.helpers.ts +17 -0
  14. package/{drafts/store.ts → client/draft-persistence-indexed-db.ts} +19 -3
  15. package/{drafts/index.ts → client/draft-persistence.ts} +13 -39
  16. package/client/index.ts +4 -2
  17. package/client/request.helpers.ts +14 -0
  18. package/client/request.ts +122 -232
  19. package/client/storage-asset.ts +58 -0
  20. package/gql/fragments/accountAuthFragments.ts +0 -4
  21. package/gql/fragments/agentMemoryFragments.ts +5 -1
  22. package/gql/fragments/agentNoteFragments.ts +156 -0
  23. package/gql/fragments/aiChatFragments.ts +35 -11
  24. package/gql/fragments/aiChatLinkFragments.ts +8 -0
  25. package/gql/fragments/audienceFragments.ts +76 -0
  26. package/gql/fragments/dataTableFragments.ts +48 -80
  27. package/gql/fragments/emailFragments.ts +49 -21
  28. package/gql/fragments/emailOutreachFragments.ts +281 -0
  29. package/gql/fragments/fieldDocFragments.ts +222 -40
  30. package/gql/fragments/inboundContactFragments.ts +2 -0
  31. package/gql/fragments/libraryFragments.ts +120 -0
  32. package/gql/fragments/logFragments.ts +93 -2
  33. package/gql/fragments/organizationFragments.ts +12 -0
  34. package/gql/fragments/qrCodeFragments.ts +29 -0
  35. package/gql/fragments/scheduleFragments.ts +574 -145
  36. package/gql/fragments/sheetFragments.ts +86 -424
  37. package/gql/fragments/storageFragments.ts +25 -1
  38. package/gql/fragments/workWaitFragments.ts +13 -0
  39. package/gql/fragments/workflowFragments.ts +32 -4
  40. package/gql/mutations/agentMemoryMutations.ts +32 -0
  41. package/gql/mutations/agentNoteMutations.ts +64 -0
  42. package/gql/mutations/aiChatLinkMutations.ts +13 -0
  43. package/gql/mutations/aiChatMutations.ts +34 -4
  44. package/gql/mutations/audienceMutations.ts +58 -0
  45. package/gql/mutations/authMutations.ts +24 -28
  46. package/gql/mutations/billingMutations.ts +24 -0
  47. package/gql/mutations/contactMutations.ts +23 -0
  48. package/gql/mutations/dataTableMutations.ts +18 -75
  49. package/gql/mutations/emailMutations.ts +22 -0
  50. package/gql/mutations/emailOutreachMutations.ts +139 -0
  51. package/gql/mutations/fieldDocMutations.ts +162 -13
  52. package/gql/mutations/index.ts +10 -5
  53. package/gql/mutations/organizationMutations.ts +6 -0
  54. package/gql/mutations/qrCodeMutations.ts +20 -0
  55. package/gql/mutations/scheduleMutations.ts +389 -417
  56. package/gql/mutations/sheetMutations.ts +62 -118
  57. package/gql/mutations/storageMutations.ts +1 -1
  58. package/gql/mutations/workflowMutations.ts +94 -0
  59. package/gql/queries/agentMemoryQueries.ts +12 -2
  60. package/gql/queries/agentNoteQueries.ts +86 -0
  61. package/gql/queries/aiChatQueries.ts +63 -12
  62. package/gql/queries/audienceQueries.ts +104 -0
  63. package/gql/queries/authQueries.ts +20 -4
  64. package/gql/queries/billingQueries.ts +114 -9
  65. package/gql/queries/dataTableQueries.ts +43 -75
  66. package/gql/queries/emailOutreachQueries.ts +282 -0
  67. package/gql/queries/emailQueries.ts +48 -64
  68. package/gql/queries/emailReplyReportQueries.ts +127 -0
  69. package/gql/queries/fieldDocQueries.ts +128 -98
  70. package/gql/queries/inboundContactQueries.ts +15 -4
  71. package/gql/queries/index.ts +5 -3
  72. package/gql/queries/libraryQueries.ts +112 -0
  73. package/gql/queries/logQueries.ts +86 -12
  74. package/gql/queries/organizationQueries.ts +44 -12
  75. package/gql/queries/qrCodeQueries.ts +27 -0
  76. package/gql/queries/scheduleQueries.ts +401 -278
  77. package/gql/queries/sheetQueries.ts +94 -165
  78. package/gql/queries/storageQueries.ts +11 -4
  79. package/gql/queries/workflowQueries.ts +61 -12
  80. package/gql/reactivity.ts +98 -0
  81. package/gql/recordReads.ts +171 -0
  82. package/hooks/index.helpers.ts +52 -0
  83. package/hooks/index.ts +920 -715
  84. package/hooks/use-agentMemory-mtn.ts +29 -31
  85. package/hooks/use-agentMemory-qry.ts +15 -3
  86. package/hooks/use-agentNote-mtn.ts +89 -0
  87. package/hooks/use-agentNote-qry.ts +162 -0
  88. package/hooks/use-agentNote-recovery.helpers.ts +58 -0
  89. package/hooks/use-agentNote-recovery.ts +57 -0
  90. package/hooks/use-aiChat-mtn.ts +104 -115
  91. package/hooks/use-aiChat-qry.helpers.ts +124 -0
  92. package/hooks/use-aiChat-qry.ts +254 -387
  93. package/hooks/use-aiChat-stream-mtn.ts +14 -0
  94. package/hooks/use-audience-mtn.ts +170 -0
  95. package/hooks/use-audience-qry.helpers.ts +39 -0
  96. package/hooks/use-audience-qry.ts +245 -0
  97. package/hooks/use-auth-mtn.ts +30 -1
  98. package/hooks/use-billing-mtn.ts +31 -0
  99. package/hooks/use-billing-qry.ts +76 -0
  100. package/hooks/use-dataTable-mtn.ts +23 -115
  101. package/hooks/use-dataTable-qry.ts +69 -914
  102. package/hooks/use-email-mtn.ts +26 -0
  103. package/hooks/use-email-qry.helpers.ts +37 -0
  104. package/hooks/use-email-qry.ts +44 -174
  105. package/hooks/use-emailOutreach-mtn.ts +211 -0
  106. package/hooks/use-emailOutreach-qry.helpers.ts +25 -0
  107. package/hooks/use-emailOutreach-qry.ts +367 -0
  108. package/hooks/use-emailOutreachReplyReport-qry.ts +46 -0
  109. package/hooks/use-fieldDoc-mtn.ts +244 -32
  110. package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
  111. package/hooks/use-fieldDoc-qry.ts +134 -102
  112. package/hooks/use-inboundContact-qry.ts +7 -9
  113. package/hooks/use-library-qry.ts +233 -0
  114. package/hooks/use-log-mtn.ts +25 -12
  115. package/hooks/use-log-qry.ts +99 -46
  116. package/hooks/use-organization-qry.ts +16 -60
  117. package/hooks/use-qrCode-mtn.ts +40 -0
  118. package/hooks/use-qrCode-qry.ts +78 -0
  119. package/hooks/use-schedule-mtn.ts +231 -348
  120. package/hooks/use-schedule-qry.ts +208 -139
  121. package/hooks/use-schedule-reactive.ts +218 -0
  122. package/hooks/use-sheet-mtn.ts +91 -199
  123. package/hooks/use-sheet-qry.ts +88 -237
  124. package/hooks/use-storage-qry.ts +84 -61
  125. package/hooks/use-workflow-mtn.ts +84 -23
  126. package/hooks/use-workflow-qry.ts +60 -61
  127. package/libs/ai-chat-link-events.ts +78 -0
  128. package/libs/ai-chat-outgoing.helpers.ts +153 -0
  129. package/libs/ai-chat-outgoing.ts +11 -0
  130. package/libs/ai-chat-outgoing.types.ts +25 -0
  131. package/libs/ai-chat-run-realtime.ts +20 -0
  132. package/libs/ai-chat-submission-realtime.ts +150 -0
  133. package/libs/data-changes.helpers.ts +161 -0
  134. package/libs/data-changes.ts +382 -0
  135. package/libs/fragment-realtime-sequence.ts +55 -0
  136. package/libs/open-sheet.ts +138 -0
  137. package/libs/record-read.ts +327 -0
  138. package/libs/sheet-command-batch.ts +45 -0
  139. package/libs/sheet-realtime-packet.ts +53 -0
  140. package/libs/sheet-view-realtime-router.ts +99 -0
  141. package/loaders/emailPreference.ts +69 -0
  142. package/loaders/rest.ts +12 -0
  143. package/package.json +77 -25
  144. package/schedule.d.ts +1183 -0
  145. package/sse.helpers.ts +19 -0
  146. package/sse.ts +26 -115
  147. package/types.d.ts +48 -25
  148. package/cache/index.test.ts +0 -881
  149. package/client/data-table-realtime-subscriptions.test.ts +0 -740
  150. package/client/data-table-realtime-subscriptions.ts +0 -955
  151. package/client/data-table-realtime.test.ts +0 -792
  152. package/client/data-table-realtime.ts +0 -1218
  153. package/client/fragment-realtime-sequence.ts +0 -52
  154. package/client/open-sheet.ts +0 -137
  155. package/client/request-managed-session.test.ts +0 -574
  156. package/client/request-no-store.test.ts +0 -157
  157. package/client/sse-ticket.test.ts +0 -26
  158. package/deno.lock +0 -170
  159. package/deno.test.json +0 -10
  160. package/gql/fragments/agentDocFragments.ts +0 -46
  161. package/gql/fragments/billingFragments.ts +0 -14
  162. package/gql/fragments/campaignFragments.ts +0 -277
  163. package/gql/mutations/agentDocMutations.ts +0 -177
  164. package/gql/mutations/campaignMutations.ts +0 -215
  165. package/gql/queries/agentDocQueries.ts +0 -120
  166. package/gql/queries/appQueries.ts +0 -14
  167. package/gql/queries/campaignQueries.ts +0 -127
  168. package/hooks/use-agentDoc-mtn.ts +0 -285
  169. package/hooks/use-agentDoc-qry.ts +0 -208
  170. package/hooks/use-app-qry.ts +0 -22
  171. package/hooks/use-campaign-mtn.ts +0 -377
  172. package/hooks/use-campaign-qry.ts +0 -257
  173. package/loaders/campaign.ts +0 -137
  174. package/tsconfig.json +0 -13
@@ -1,17 +1,9 @@
1
1
  import { gql } from 'graphql-tag';
2
- import {
3
- dataTableCellFragment,
4
- dataTableRowFragment,
5
- } from '../fragments/dataTableFragments.ts';
6
- import {
7
- sheetFragment,
8
- sheetMetadataFragment,
9
- sheetReferencedDataTableSchemaFragment,
10
- sheetTabFragment,
11
- sheetTabSummaryFragment,
12
- } from '../fragments/sheetFragments.ts';
2
+ import { sheetFragment, sheetMetadataFragment, sheetTabFragment, sheetTabSummaryFragment } from '../fragments/sheetFragments.ts';
3
+ import { live } from '../reactivity.ts';
13
4
 
14
- export const sheetsQry = gql`
5
+ export const sheetsQry = live(
6
+ gql`
15
7
  query sheets (
16
8
  $organizationId: GenericID!
17
9
  $sort: GridItemSort
@@ -31,9 +23,12 @@ query sheets (
31
23
  }
32
24
 
33
25
  ${sheetFragment}
34
- `;
26
+ `,
27
+ { refresh: 'notice', lists: ['sheet'] },
28
+ );
35
29
 
36
- export const publicSheetMetadataQry = gql`
30
+ export const publicSheetMetadataQry = live(
31
+ gql`
37
32
  query publicSheetMetadata (
38
33
  $sheetId: GenericID!
39
34
  ) {
@@ -45,14 +40,17 @@ query publicSheetMetadata (
45
40
  }
46
41
 
47
42
  ${sheetMetadataFragment}
48
- `;
43
+ `,
44
+ { refresh: 'auto' },
45
+ );
49
46
 
50
47
  // NOTE: sheetAccess is viewer-specific and works signed out: the public
51
48
  // sheet route uses it to learn the owner org and view/edit facts, and the
52
49
  // in-app Sheet page uses it for the viewer's effective edit fact (grants
53
50
  // are per-viewer overrides, so the base policy alone cannot decide it).
54
51
 
55
- export const sheetAccessQry = gql`
52
+ export const sheetAccessQry = live(
53
+ gql`
56
54
  query sheetAccess (
57
55
  $sheetId: GenericID!
58
56
  ) {
@@ -66,36 +64,16 @@ query sheetAccess (
66
64
  signInRequired
67
65
  }
68
66
  }
69
- `;
70
-
71
- export const sheetRegionSourcePreviewRowsQry = gql`
72
- query sheetRegionSourcePreviewRows (
73
- $organizationId: GenericID!
74
- $region: SheetRegionInput!
75
- $limit: Int!
76
- ) {
77
- sheetRegionSourcePreviewRows (
78
- organizationId: $organizationId
79
- region: $region
80
- limit: $limit
81
- ) {
82
- ...dataTableRowFragment
83
-
84
- cells {
85
- ...dataTableCellFragment
86
- }
87
- }
88
- }
89
-
90
- ${dataTableRowFragment}
91
- ${dataTableCellFragment}
92
- `;
67
+ `,
68
+ { refresh: 'auto', derived: ['sheetAccess'] },
69
+ );
93
70
 
94
71
  // NOTE: sheetAccessGrants is owner-org only: the access settings screen
95
72
  // loads the grants list with it, on top of the base policy carried by the
96
73
  // Sheet fragment's "access" selection.
97
74
 
98
- export const sheetAccessGrantsQry = gql`
75
+ export const sheetAccessGrantsQry = live(
76
+ gql`
99
77
  query sheetAccessGrants (
100
78
  $organizationId: GenericID!
101
79
  $sheetId: GenericID!
@@ -109,30 +87,29 @@ query sheetAccessGrants (
109
87
  permission
110
88
  }
111
89
  }
112
- `;
113
-
114
- // NOTE: openSheet is the page-bootstrap contract. It derives the owner org
115
- // from sheetId, folds viewer access into the response, and lets the server
116
- // choose a compact snapshot or a short contiguous cell-only delta.
117
-
118
- export const openSheetQry = gql`
90
+ `,
91
+ { refresh: 'auto' },
92
+ );
93
+
94
+ // NOTE: openSheet is the page-bootstrap contract (contract 4). It derives the
95
+ // owner org from sheetId, folds viewer access into the response, and serves a
96
+ // DELTA after the client's applied sinceSeq or a SNAPSHOT plus its tail (with
97
+ // the topology rows the image absorbed after sinceSeq, so pending work
98
+ // re-maps through them).
99
+
100
+ export const openSheetQry = live(
101
+ gql`
119
102
  query openSheet (
120
103
  $sheetId: GenericID!
121
104
  $selectedTabId: GenericID
122
105
  $sinceSeq: BigInt
123
- $baseSnapshotSeq: BigInt
124
- $baseSnapshotEncodingVersion: Int
125
- $baseSnapshotChecksum: String
126
- $viewport: SheetGridViewportInput
106
+ $requireSnapshot: Boolean
127
107
  ) {
128
108
  openSheet (
129
109
  sheetId: $sheetId
130
110
  selectedTabId: $selectedTabId
131
111
  sinceSeq: $sinceSeq
132
- baseSnapshotSeq: $baseSnapshotSeq
133
- baseSnapshotEncodingVersion: $baseSnapshotEncodingVersion
134
- baseSnapshotChecksum: $baseSnapshotChecksum
135
- viewport: $viewport
112
+ requireSnapshot: $requireSnapshot
136
113
  ) {
137
114
  access {
138
115
  sheetId
@@ -143,7 +120,7 @@ query openSheet (
143
120
  }
144
121
 
145
122
  contractVersion
146
- realtimeToken
123
+ realtimeToken
147
124
  status
148
125
  unavailableReason
149
126
  sheet {
@@ -195,39 +172,75 @@ query openSheet (
195
172
  createdAt
196
173
  }
197
174
 
198
- referencedDataTables {
199
- ...sheetReferencedDataTableSchemaFragment
175
+ topologyChanges {
176
+ sheetTabId
177
+ seq
178
+ kind
179
+ payload
180
+ commandId
200
181
  }
182
+
183
+ topologyComplete
201
184
  }
202
185
  }
203
186
 
204
187
  ${sheetFragment}
205
188
  ${sheetTabFragment}
206
189
  ${sheetTabSummaryFragment}
207
- ${sheetReferencedDataTableSchemaFragment}
208
- `;
190
+ `,
191
+ { refresh: 'auto' },
192
+ );
209
193
 
210
- // NOTE: openSheetTab is the post-manifest bootstrap contract. It intentionally
211
- // excludes workbook JSON, all-tab summaries, and cross-tab reference metadata.
194
+ // NOTE: renewSheetRealtimeCapability re-runs openSheet's authorization over a
195
+ // fence-only read and returns fresh signed capabilities, never workbook data.
196
+ // It is fetched no-store only: bearer tokens must stay out of the cache.
212
197
 
213
- export const openSheetTabQry = gql`
214
- query openSheetTab (
198
+ export const renewSheetRealtimeCapabilityQry = live(
199
+ gql`
200
+ query renewSheetRealtimeCapability (
201
+ $sheetId: GenericID!
202
+ ) {
203
+ renewSheetRealtimeCapability (
204
+ sheetId: $sheetId
205
+ ) {
206
+ access {
207
+ sheetId
208
+ organizationId
209
+ canView
210
+ canEdit
211
+ signInRequired
212
+ }
213
+
214
+ status
215
+ unavailableReason
216
+ realtimeToken
217
+ }
218
+ }
219
+ `,
220
+ { refresh: 'auto' },
221
+ );
222
+
223
+ // NOTE: syncSheetTab is the tab-only snapshot/delta negotiation (contract 3)
224
+ // used once the workbook manifest is known: a gap, a selection or a
225
+ // watermark catch-up asks for the rows after the tab's applied sinceSeq.
226
+
227
+ export const syncSheetTabQry = live(
228
+ gql`
229
+ query syncSheetTab (
215
230
  $sheetId: GenericID!
216
231
  $sheetTabId: GenericID!
217
232
  $sinceSeq: BigInt
218
- $baseSnapshotSeq: BigInt
219
- $baseSnapshotEncodingVersion: Int
220
- $baseSnapshotChecksum: String
221
- $viewport: SheetGridViewportInput
233
+ $requireSnapshot: Boolean
234
+ $minimumCellsRevision: BigInt
235
+ $minimumDesignRevision: BigInt
222
236
  ) {
223
- openSheetTab (
237
+ syncSheetTab (
224
238
  sheetId: $sheetId
225
239
  sheetTabId: $sheetTabId
226
240
  sinceSeq: $sinceSeq
227
- baseSnapshotSeq: $baseSnapshotSeq
228
- baseSnapshotEncodingVersion: $baseSnapshotEncodingVersion
229
- baseSnapshotChecksum: $baseSnapshotChecksum
230
- viewport: $viewport
241
+ requireSnapshot: $requireSnapshot
242
+ minimumCellsRevision: $minimumCellsRevision
243
+ minimumDesignRevision: $minimumDesignRevision
231
244
  ) {
232
245
  access {
233
246
  sheetId
@@ -238,7 +251,6 @@ query openSheetTab (
238
251
  }
239
252
 
240
253
  contractVersion
241
- realtimeToken
242
254
  status
243
255
  unavailableReason
244
256
  selectedTab {
@@ -282,102 +294,19 @@ query openSheetTab (
282
294
  createdAt
283
295
  }
284
296
 
285
- referencedDataTables {
286
- ...sheetReferencedDataTableSchemaFragment
287
- }
288
- }
289
- }
290
-
291
- ${sheetTabFragment}
292
- ${sheetReferencedDataTableSchemaFragment}
293
- `;
294
-
295
- // NOTE: sheetTabChanges is the realtime gap-recovery tail read: a client that
296
- // detects a seq gap in its SheetView fragment deltas fetches every journaled change
297
- // after its last-seen seq and replays the JSON payloads locally instead of
298
- // refetching the whole viewport. tailAvailable: false means the tail cannot
299
- // be served (too long/heavy, truncated, or pre-deployment history) and the
300
- // client must request a fresh authoritative tab snapshot.
301
-
302
- export const sheetTabChangesQry = gql`
303
- query sheetTabChanges (
304
- $organizationId: GenericID!
305
- $sheetId: GenericID!
306
- $sheetTabId: GenericID!
307
- $afterSeq: BigInt!
308
- ) {
309
- sheetTabChanges (
310
- organizationId: $organizationId
311
- sheetId: $sheetId
312
- sheetTabId: $sheetTabId
313
- afterSeq: $afterSeq
314
- ) {
315
- sheetTabId
316
- currentSeq
317
- tailAvailable
318
- changes {
297
+ topologyChanges {
319
298
  sheetTabId
320
299
  seq
321
300
  kind
322
301
  payload
323
302
  commandId
324
- causationId
325
- sessionId
326
- commandType
327
- createdAt
328
303
  }
329
- }
330
- }
331
- `;
332
304
 
333
- // NOTE: operational history exposes only the compact semantic intent needed
334
- // for instant replay and is scoped by the server to the authenticated actor
335
- // plus this exact browser-tab session.
336
-
337
- export const sheetOperationalHistoryQry = gql`
338
- query sheetOperationalHistory (
339
- $organizationId: GenericID!
340
- $sheetId: GenericID!
341
- $sheetTabId: GenericID!
342
- $sessionId: String!
343
- ) {
344
- sheetOperationalHistory (
345
- organizationId: $organizationId
346
- sheetId: $sheetId
347
- sheetTabId: $sheetTabId
348
- sessionId: $sessionId
349
- ) {
350
- sheetId
351
- sheetTabId
352
- sessionId
353
- truncated
354
- incompleteBeforeBoundary
355
- undo {
356
- commandId
357
- commandType
358
- committedAt
359
- resultRevision
360
- resultDesignRevision
361
- axisMove {
362
- axis
363
- sourceStartIndex
364
- sourceEndIndex
365
- targetStartIndex
366
- }
367
- }
368
- redo {
369
- commandId
370
- commandType
371
- committedAt
372
- resultRevision
373
- resultDesignRevision
374
- axisMove {
375
- axis
376
- sourceStartIndex
377
- sourceEndIndex
378
- targetStartIndex
379
- }
380
- }
305
+ topologyComplete
381
306
  }
382
307
  }
383
- `;
308
+
309
+ ${sheetTabFragment}
310
+ `,
311
+ { refresh: 'auto' },
312
+ );
@@ -1,7 +1,9 @@
1
1
  import { gql } from 'graphql-tag';
2
2
  import { generatedFileFragment } from '../fragments/storageFragments.ts';
3
+ import { live } from '../reactivity.ts';
3
4
 
4
- export const generatedFilesQry = gql`
5
+ export const generatedFilesQry = live(
6
+ gql`
5
7
  query generatedFiles (
6
8
  $organizationId: GenericID!
7
9
  $cursor: Cursor
@@ -19,9 +21,12 @@ query generatedFiles (
19
21
  }
20
22
 
21
23
  ${generatedFileFragment}
22
- `;
24
+ `,
25
+ { refresh: 'auto', lists: ['generatedFile'] },
26
+ );
23
27
 
24
- export const generatedFileQry = gql`
28
+ export const generatedFileQry = live(
29
+ gql`
25
30
  query generatedFile (
26
31
  $organizationId: GenericID!
27
32
  $code: String!
@@ -35,4 +40,6 @@ query generatedFile (
35
40
  }
36
41
 
37
42
  ${generatedFileFragment}
38
- `;
43
+ `,
44
+ { refresh: 'auto' },
45
+ );
@@ -1,10 +1,23 @@
1
1
  import { gql } from 'graphql-tag';
2
- import {
3
- workflowFragment,
4
- workflowRunFragment,
5
- } from '../fragments/workflowFragments.ts';
2
+ import { workflowFragment, workflowRunFragment } from '../fragments/workflowFragments.ts';
3
+ import { workWaitItemFragment } from '../fragments/workWaitFragments.ts';
4
+ import { live } from '../reactivity.ts';
6
5
 
7
- export const workflowMetadataQry = gql`
6
+ export const workflowQry = live(
7
+ gql`
8
+ query workflow ($workflowId: GenericID!) {
9
+ workflow (workflowId: $workflowId) {
10
+ ...workflowFragment
11
+ }
12
+ }
13
+ ${workflowFragment}
14
+ ${workWaitItemFragment}
15
+ `,
16
+ { refresh: 'auto' },
17
+ );
18
+
19
+ export const workflowMetadataQry = live(
20
+ gql`
8
21
  query workflowMetadata (
9
22
  $workflowId: GenericID!
10
23
  ) {
@@ -15,9 +28,12 @@ query workflowMetadata (
15
28
  title
16
29
  }
17
30
  }
18
- `;
31
+ `,
32
+ { refresh: 'auto' },
33
+ );
19
34
 
20
- export const workflowsQry = gql`
35
+ export const workflowsQry = live(
36
+ gql`
21
37
  query workflows (
22
38
  $organizationId: GenericID!
23
39
  $filter: WorkflowsFilter!
@@ -44,9 +60,37 @@ query workflows (
44
60
 
45
61
  ${workflowFragment}
46
62
  ${workflowRunFragment}
47
- `;
63
+ ${workWaitItemFragment}
64
+ `,
65
+ { refresh: 'notice', lists: ['workflow'] },
66
+ );
67
+
68
+ // The workflow of one form, for the notice on its page. Not a table, so a workflow added elsewhere refetches it quietly
69
+ // instead of raising the "new data" notice; the alias keeps it apart from workflowsQry's declaration
70
+ export const fieldDocWorkflowQry = live(
71
+ gql`
72
+ query fieldDocWorkflow (
73
+ $organizationId: GenericID!
74
+ $fieldDocId: GenericID!
75
+ ) {
76
+ fieldDocWorkflow: workflows (
77
+ organizationId: $organizationId
78
+ filter: { kind: WORKFLOW, fieldDocId: $fieldDocId }
79
+ after: true
80
+ limit: 1
81
+ ) {
82
+ ...workflowFragment
83
+ }
84
+ }
85
+
86
+ ${workflowFragment}
87
+ ${workWaitItemFragment}
88
+ `,
89
+ { refresh: 'auto', lists: ['workflow'] },
90
+ );
48
91
 
49
- export const workflowRunsQry = gql`
92
+ export const workflowRunsQry = live(
93
+ gql`
50
94
  query workflowRuns (
51
95
  $organizationId: GenericID!
52
96
  $filter: WorkflowRunsFilter!
@@ -74,9 +118,12 @@ query workflowRuns (
74
118
  }
75
119
 
76
120
  ${workflowRunFragment}
77
- `;
121
+ `,
122
+ { refresh: 'notice', lists: ['workflowRun'], scope: { workflowId: 'filter.workflowId' } },
123
+ );
78
124
 
79
- export const workflowRunQry = gql`
125
+ export const workflowRunQry = live(
126
+ gql`
80
127
  query workflowRun (
81
128
  $organizationId: GenericID!
82
129
  $workflowRunId: GenericID!
@@ -96,4 +143,6 @@ query workflowRun (
96
143
  }
97
144
 
98
145
  ${workflowRunFragment}
99
- `;
146
+ `,
147
+ { refresh: 'auto' },
148
+ );
@@ -0,0 +1,98 @@
1
+ import type { DataTypeKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
2
+ import type { DocumentNode, OperationDefinitionNode } from 'graphql';
3
+
4
+ /*
5
+ * What a query document states about itself: how it refreshes and which data
6
+ * types it depends on (design 3). Attached to the document with live().
7
+ */
8
+ export interface QueryReactivity {
9
+ refresh: 'auto' | 'notice';
10
+ lists?: readonly DataTypeKey[];
11
+ derived?: readonly DataTypeKey[];
12
+ // scope key → variable path, when the names differ; null ignores that scope key
13
+ scope?: Readonly<Record<string, string | null>>;
14
+ // Changes caused by this tab do not refresh the query (editor sessions with unsaved local state)
15
+ skipOwnChanges?: true;
16
+ }
17
+
18
+ /*
19
+ * A declaration as registered: the declaration plus every name its query is cached or mounted under.
20
+ */
21
+ export interface RegisteredQueryReactivity extends QueryReactivity {
22
+ // Operation name first, then every root field name or alias that differs from it
23
+ names: string[];
24
+ }
25
+
26
+ // Filled as query modules load; read by the realtime dispatcher
27
+ const REGISTRY = {
28
+ byName: new Map<string, RegisteredQueryReactivity>(),
29
+ byType: new Map<DataTypeKey, RegisteredQueryReactivity[]>(),
30
+ };
31
+
32
+ /*
33
+ * Return the names one query document is known by: its operation name, then the
34
+ * response name (alias or field name) of every root field, without duplicates.
35
+ * Interpolated fragment definitions may come before or after the operation.
36
+ */
37
+ function getQueryDocumentNames(document: DocumentNode): string[] {
38
+ const operation = document.definitions.find(
39
+ (definition): definition is OperationDefinitionNode => definition.kind === 'OperationDefinition',
40
+ );
41
+ if (!operation?.name) {
42
+ throw new Error('live() needs a document with a named operation');
43
+ }
44
+
45
+ const roots = operation.selectionSet.selections.flatMap((selection) =>
46
+ selection.kind === 'Field' ? [selection.alias?.value || selection.name.value] : [],
47
+ );
48
+ return [...new Set([operation.name.value, ...roots])];
49
+ }
50
+
51
+ /*
52
+ * Attach a reactivity declaration to a query document and return the document unchanged.
53
+ * The declaration is registered under the operation name and every root field name.
54
+ */
55
+ export function live<T extends DocumentNode>(document: T, reactivity: QueryReactivity): T {
56
+ const names = getQueryDocumentNames(document);
57
+ const existing = REGISTRY.byName.get(names[0]);
58
+ if (existing) {
59
+ // Two documents may share one operation name (aiChatMessages, emailOutreach); they declare the same thing
60
+ for (const name of names) {
61
+ if (!existing.names.includes(name)) {
62
+ existing.names.push(name);
63
+ REGISTRY.byName.set(name, existing);
64
+ }
65
+ }
66
+ return document;
67
+ }
68
+
69
+ const registered: RegisteredQueryReactivity = { ...reactivity, names };
70
+ for (const name of names) {
71
+ REGISTRY.byName.set(name, registered);
72
+ }
73
+
74
+ // A type named in both lists and derived is registered once
75
+ for (const type of new Set([...(reactivity.lists || []), ...(reactivity.derived || [])])) {
76
+ const declarations = REGISTRY.byType.get(type);
77
+ if (declarations) {
78
+ declarations.push(registered);
79
+ } else {
80
+ REGISTRY.byType.set(type, [registered]);
81
+ }
82
+ }
83
+ return document;
84
+ }
85
+
86
+ /*
87
+ * Return every declared query that lists or is derived from one data type.
88
+ */
89
+ export function getQueryReactivitiesForType(type: DataTypeKey): readonly RegisteredQueryReactivity[] {
90
+ return REGISTRY.byType.get(type) || [];
91
+ }
92
+
93
+ /*
94
+ * Return the declaration of one query by operation or root field name.
95
+ */
96
+ export function getQueryReactivity(name: string): RegisteredQueryReactivity | undefined {
97
+ return REGISTRY.byName.get(name);
98
+ }