houdini 0.17.9 → 0.17.10

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 (121) hide show
  1. package/README.md +33 -0
  2. package/build/cmd-cjs/index.js +2 -2
  3. package/build/cmd-esm/index.js +2 -2
  4. package/package.json +16 -1
  5. package/.turbo/turbo-compile.log +0 -5
  6. package/.turbo/turbo-typedefs.log +0 -5
  7. package/CHANGELOG.md +0 -377
  8. package/src/cmd/generate.ts +0 -54
  9. package/src/cmd/index.ts +0 -60
  10. package/src/cmd/init.ts +0 -637
  11. package/src/cmd/pullSchema.ts +0 -40
  12. package/src/codegen/generators/artifacts/artifacts.test.ts +0 -3246
  13. package/src/codegen/generators/artifacts/fieldKey.ts +0 -60
  14. package/src/codegen/generators/artifacts/index.ts +0 -330
  15. package/src/codegen/generators/artifacts/indexFile.ts +0 -24
  16. package/src/codegen/generators/artifacts/inputs.ts +0 -81
  17. package/src/codegen/generators/artifacts/operations.ts +0 -281
  18. package/src/codegen/generators/artifacts/pagination.test.ts +0 -664
  19. package/src/codegen/generators/artifacts/policy.test.ts +0 -298
  20. package/src/codegen/generators/artifacts/selection.ts +0 -208
  21. package/src/codegen/generators/artifacts/utils.test.ts +0 -118
  22. package/src/codegen/generators/artifacts/utils.ts +0 -108
  23. package/src/codegen/generators/definitions/enums.test.ts +0 -61
  24. package/src/codegen/generators/definitions/enums.ts +0 -68
  25. package/src/codegen/generators/definitions/index.ts +0 -11
  26. package/src/codegen/generators/definitions/schema.test.ts +0 -236
  27. package/src/codegen/generators/index.ts +0 -6
  28. package/src/codegen/generators/indexFile/index.ts +0 -63
  29. package/src/codegen/generators/indexFile/indexFile.test.ts +0 -72
  30. package/src/codegen/generators/persistedQueries/index.ts +0 -55
  31. package/src/codegen/generators/persistedQueries/persistedQuery.test.ts +0 -26
  32. package/src/codegen/generators/runtime/index.test.ts +0 -74
  33. package/src/codegen/generators/runtime/index.ts +0 -64
  34. package/src/codegen/generators/runtime/runtime.test.ts +0 -25
  35. package/src/codegen/generators/typescript/addReferencedInputTypes.ts +0 -77
  36. package/src/codegen/generators/typescript/index.ts +0 -412
  37. package/src/codegen/generators/typescript/inlineType.ts +0 -409
  38. package/src/codegen/generators/typescript/typeReference.ts +0 -44
  39. package/src/codegen/generators/typescript/types.ts +0 -81
  40. package/src/codegen/generators/typescript/typescript.test.ts +0 -1434
  41. package/src/codegen/index.ts +0 -406
  42. package/src/codegen/transforms/addID.test.ts +0 -93
  43. package/src/codegen/transforms/addID.ts +0 -86
  44. package/src/codegen/transforms/composeQueries.test.ts +0 -50
  45. package/src/codegen/transforms/composeQueries.ts +0 -154
  46. package/src/codegen/transforms/fragmentVariables.test.ts +0 -636
  47. package/src/codegen/transforms/fragmentVariables.ts +0 -417
  48. package/src/codegen/transforms/index.ts +0 -7
  49. package/src/codegen/transforms/list.ts +0 -484
  50. package/src/codegen/transforms/lists.test.ts +0 -530
  51. package/src/codegen/transforms/paginate.test.ts +0 -1528
  52. package/src/codegen/transforms/paginate.ts +0 -770
  53. package/src/codegen/transforms/schema.test.ts +0 -136
  54. package/src/codegen/transforms/schema.ts +0 -109
  55. package/src/codegen/transforms/typename.test.ts +0 -125
  56. package/src/codegen/transforms/typename.ts +0 -55
  57. package/src/codegen/utils/commonjs.ts +0 -26
  58. package/src/codegen/utils/flattenSelections.ts +0 -179
  59. package/src/codegen/utils/graphql.test.ts +0 -35
  60. package/src/codegen/utils/graphql.ts +0 -79
  61. package/src/codegen/utils/index.ts +0 -5
  62. package/src/codegen/utils/moduleExport.ts +0 -27
  63. package/src/codegen/utils/murmur.ts +0 -79
  64. package/src/codegen/validators/index.ts +0 -4
  65. package/src/codegen/validators/noIDAlias.test.ts +0 -71
  66. package/src/codegen/validators/noIDAlias.ts +0 -39
  67. package/src/codegen/validators/plugins.ts +0 -25
  68. package/src/codegen/validators/typeCheck.test.ts +0 -960
  69. package/src/codegen/validators/typeCheck.ts +0 -1086
  70. package/src/codegen/validators/uniqueNames.test.ts +0 -59
  71. package/src/codegen/validators/uniqueNames.ts +0 -39
  72. package/src/lib/cleanupFiles.ts +0 -20
  73. package/src/lib/config.test.ts +0 -13
  74. package/src/lib/config.ts +0 -954
  75. package/src/lib/constants.ts +0 -11
  76. package/src/lib/error.ts +0 -24
  77. package/src/lib/fs.ts +0 -285
  78. package/src/lib/graphql.test.ts +0 -211
  79. package/src/lib/graphql.ts +0 -200
  80. package/src/lib/imports.ts +0 -82
  81. package/src/lib/index.ts +0 -17
  82. package/src/lib/introspection.ts +0 -39
  83. package/src/lib/parse.test.ts +0 -75
  84. package/src/lib/parse.ts +0 -23
  85. package/src/lib/path.ts +0 -49
  86. package/src/lib/pipeline.ts +0 -17
  87. package/src/lib/types.ts +0 -34
  88. package/src/lib/walk.ts +0 -104
  89. package/src/runtime/cache/cache.ts +0 -1026
  90. package/src/runtime/cache/gc.ts +0 -56
  91. package/src/runtime/cache/index.ts +0 -3
  92. package/src/runtime/cache/lists.ts +0 -516
  93. package/src/runtime/cache/storage.ts +0 -574
  94. package/src/runtime/cache/stuff.ts +0 -77
  95. package/src/runtime/cache/subscription.ts +0 -329
  96. package/src/runtime/cache/tests/availability.test.ts +0 -408
  97. package/src/runtime/cache/tests/gc.test.ts +0 -319
  98. package/src/runtime/cache/tests/keys.test.ts +0 -36
  99. package/src/runtime/cache/tests/list.test.ts +0 -3854
  100. package/src/runtime/cache/tests/readwrite.test.ts +0 -1201
  101. package/src/runtime/cache/tests/scalars.test.ts +0 -218
  102. package/src/runtime/cache/tests/storage.test.ts +0 -426
  103. package/src/runtime/cache/tests/subscriptions.test.ts +0 -1757
  104. package/src/runtime/index.ts +0 -29
  105. package/src/runtime/lib/config.ts +0 -211
  106. package/src/runtime/lib/constants.ts +0 -17
  107. package/src/runtime/lib/deepEquals.ts +0 -32
  108. package/src/runtime/lib/errors.ts +0 -8
  109. package/src/runtime/lib/index.ts +0 -8
  110. package/src/runtime/lib/log.ts +0 -69
  111. package/src/runtime/lib/network.ts +0 -303
  112. package/src/runtime/lib/networkUtils.ts +0 -151
  113. package/src/runtime/lib/scalars.test.ts +0 -877
  114. package/src/runtime/lib/scalars.ts +0 -195
  115. package/src/runtime/lib/types.ts +0 -195
  116. package/src/test/index.ts +0 -294
  117. package/src/vite/ast.ts +0 -107
  118. package/src/vite/houdini.ts +0 -113
  119. package/src/vite/imports.ts +0 -129
  120. package/src/vite/index.ts +0 -55
  121. package/src/vite/schema.ts +0 -80
@@ -1,3854 +0,0 @@
1
- import { test, expect, vi } from 'vitest'
2
-
3
- import { testConfigFile } from '../../../test'
4
- import { SubscriptionSelection, RefetchUpdateMode } from '../../lib/types'
5
- import { Cache } from '../cache'
6
-
7
- const config = testConfigFile()
8
-
9
- test('prepend linked lists update', function () {
10
- // instantiate the cache
11
- const cache = new Cache(config)
12
-
13
- const selection = {
14
- viewer: {
15
- type: 'User',
16
- keyRaw: 'viewer',
17
- fields: {
18
- id: {
19
- type: 'ID',
20
- keyRaw: 'id',
21
- },
22
- firstName: {
23
- type: 'String',
24
- keyRaw: 'firstName',
25
- },
26
- friends: {
27
- type: 'User',
28
- keyRaw: 'friends',
29
- update: 'prepend',
30
- fields: {
31
- id: {
32
- type: 'ID',
33
- keyRaw: 'id',
34
- },
35
- firstName: {
36
- type: 'String',
37
- keyRaw: 'firstName',
38
- },
39
- },
40
- },
41
- },
42
- },
43
- } as SubscriptionSelection
44
-
45
- // add some data to the cache
46
- cache.write({
47
- selection,
48
- data: {
49
- viewer: {
50
- id: '1',
51
- firstName: 'bob',
52
- friends: [
53
- {
54
- id: '2',
55
- firstName: 'jane',
56
- },
57
- {
58
- id: '3',
59
- firstName: 'mary',
60
- },
61
- ],
62
- },
63
- },
64
- applyUpdates: true,
65
- })
66
-
67
- // make sure we can get the linked lists back
68
- expect(
69
- cache.read({
70
- selection: {
71
- friends: {
72
- type: 'User',
73
- keyRaw: 'friends',
74
- update: RefetchUpdateMode.prepend,
75
- fields: {
76
- id: {
77
- type: 'ID',
78
- keyRaw: 'id',
79
- },
80
- firstName: {
81
- type: 'String',
82
- keyRaw: 'firstName',
83
- },
84
- },
85
- },
86
- },
87
- parent: 'User:1',
88
- }).data
89
- ).toEqual({
90
- friends: [
91
- {
92
- id: '2',
93
- firstName: 'jane',
94
- },
95
- {
96
- id: '3',
97
- firstName: 'mary',
98
- },
99
- ],
100
- })
101
- // add some data to the cache
102
- cache.write({
103
- selection,
104
- data: {
105
- viewer: {
106
- id: '1',
107
- firstName: 'bob',
108
- friends: [
109
- {
110
- id: '4',
111
- firstName: 'jane',
112
- },
113
- {
114
- id: '5',
115
- firstName: 'mary',
116
- },
117
- ],
118
- },
119
- },
120
- applyUpdates: true,
121
- })
122
-
123
- // make sure we can get the linked lists back
124
- expect(
125
- cache.read({
126
- selection: {
127
- friends: {
128
- type: 'User',
129
- keyRaw: 'friends',
130
- update: RefetchUpdateMode.prepend,
131
- fields: {
132
- id: {
133
- type: 'ID',
134
- keyRaw: 'id',
135
- },
136
- firstName: {
137
- type: 'String',
138
- keyRaw: 'firstName',
139
- },
140
- },
141
- },
142
- },
143
- parent: 'User:1',
144
- }).data
145
- ).toEqual({
146
- friends: [
147
- {
148
- id: '4',
149
- firstName: 'jane',
150
- },
151
- {
152
- id: '5',
153
- firstName: 'mary',
154
- },
155
- {
156
- id: '2',
157
- firstName: 'jane',
158
- },
159
- {
160
- id: '3',
161
- firstName: 'mary',
162
- },
163
- ],
164
- })
165
- })
166
-
167
- test('append in list', function () {
168
- // instantiate a cache
169
- const cache = new Cache(config)
170
-
171
- const selection = {
172
- viewer: {
173
- type: 'User',
174
- keyRaw: 'viewer',
175
- fields: {
176
- id: {
177
- type: 'ID',
178
- keyRaw: 'id',
179
- },
180
- friends: {
181
- type: 'User',
182
- keyRaw: 'friends',
183
- list: {
184
- name: 'All_Users',
185
- connection: false,
186
- type: 'User',
187
- },
188
- fields: {
189
- id: {
190
- type: 'ID',
191
- keyRaw: 'id',
192
- },
193
- firstName: {
194
- type: 'String',
195
- keyRaw: 'firstName',
196
- },
197
- },
198
- },
199
- },
200
- },
201
- }
202
-
203
- // start off associated with one object
204
- cache.write({
205
- selection,
206
- data: {
207
- viewer: {
208
- id: '1',
209
- friends: [
210
- {
211
- id: '2',
212
- firstName: 'jane',
213
- },
214
- ],
215
- },
216
- },
217
- })
218
-
219
- // a function to spy on that will play the role of set
220
- const set = vi.fn()
221
-
222
- // subscribe to the fields
223
- cache.subscribe({
224
- rootType: 'Query',
225
- set,
226
- selection,
227
- })
228
-
229
- // insert an element into the list (no parent ID)
230
- cache.list('All_Users').append(
231
- { id: { type: 'ID', keyRaw: 'id' }, firstName: { type: 'String', keyRaw: 'firstName' } },
232
- {
233
- id: '3',
234
- firstName: 'mary',
235
- }
236
- )
237
-
238
- // make sure we got the new value
239
- expect(set).toHaveBeenCalledWith({
240
- viewer: {
241
- id: '1',
242
- friends: [
243
- {
244
- firstName: 'jane',
245
- id: '2',
246
- },
247
- {
248
- firstName: 'mary',
249
- id: '3',
250
- },
251
- ],
252
- },
253
- })
254
- })
255
-
256
- test('prepend in list', function () {
257
- // instantiate a cache
258
- const cache = new Cache(config)
259
-
260
- const selection = {
261
- viewer: {
262
- type: 'User',
263
- keyRaw: 'viewer',
264
- fields: {
265
- id: {
266
- type: 'ID',
267
- keyRaw: 'id',
268
- },
269
- friends: {
270
- type: 'User',
271
- keyRaw: 'friends',
272
- list: {
273
- name: 'All_Users',
274
- connection: false,
275
- type: 'User',
276
- },
277
- fields: {
278
- id: {
279
- type: 'ID',
280
- keyRaw: 'id',
281
- },
282
- firstName: {
283
- type: 'String',
284
- keyRaw: 'firstName',
285
- },
286
- },
287
- },
288
- },
289
- },
290
- }
291
-
292
- // start off associated with one object
293
- cache.write({
294
- selection,
295
- data: {
296
- viewer: {
297
- id: '1',
298
- friends: [
299
- {
300
- id: '2',
301
- firstName: 'jane',
302
- },
303
- ],
304
- },
305
- },
306
- })
307
-
308
- // a function to spy on that will play the role of set
309
- const set = vi.fn()
310
-
311
- // subscribe to the fields
312
- cache.subscribe({
313
- rootType: 'Query',
314
- set,
315
- selection,
316
- })
317
-
318
- // insert an element into the list (no parent ID)
319
- cache.list('All_Users').prepend(
320
- { id: { type: 'ID', keyRaw: 'id' }, firstName: { type: 'String', keyRaw: 'firstName' } },
321
- {
322
- id: '3',
323
- firstName: 'mary',
324
- }
325
- )
326
-
327
- // make sure we got the new value
328
- expect(set).toHaveBeenCalledWith({
329
- viewer: {
330
- id: '1',
331
- friends: [
332
- {
333
- firstName: 'mary',
334
- id: '3',
335
- },
336
- {
337
- firstName: 'jane',
338
- id: '2',
339
- },
340
- ],
341
- },
342
- })
343
- })
344
-
345
- test('remove from connection', function () {
346
- // instantiate a cache
347
- const cache = new Cache(config)
348
-
349
- const selection: SubscriptionSelection = {
350
- viewer: {
351
- type: 'User',
352
- keyRaw: 'viewer',
353
- fields: {
354
- id: {
355
- type: 'ID',
356
- keyRaw: 'id',
357
- },
358
- friends: {
359
- type: 'User',
360
- keyRaw: 'friends',
361
- list: {
362
- name: 'All_Users',
363
- connection: true,
364
- type: 'User',
365
- },
366
- fields: {
367
- edges: {
368
- type: 'UserEdge',
369
- keyRaw: 'edges',
370
- fields: {
371
- node: {
372
- type: 'Node',
373
- keyRaw: 'node',
374
- abstract: true,
375
- fields: {
376
- __typename: {
377
- type: 'String',
378
- keyRaw: '__typename',
379
- },
380
- id: {
381
- type: 'ID',
382
- keyRaw: 'id',
383
- },
384
- firstName: {
385
- type: 'String',
386
- keyRaw: 'firstName',
387
- },
388
- },
389
- },
390
- },
391
- },
392
- },
393
- },
394
- },
395
- },
396
- }
397
-
398
- // start off associated with one object
399
- cache.write({
400
- selection,
401
- data: {
402
- viewer: {
403
- id: '1',
404
- friends: {
405
- edges: [
406
- {
407
- node: {
408
- __typename: 'User',
409
- id: '2',
410
- firstName: 'jane',
411
- },
412
- },
413
- {
414
- node: {
415
- __typename: 'User',
416
- id: '3',
417
- firstName: 'jane',
418
- },
419
- },
420
- ],
421
- },
422
- },
423
- },
424
- })
425
-
426
- // a function to spy on that will play the role of set
427
- const set = vi.fn()
428
-
429
- // subscribe to the fields
430
- cache.subscribe({
431
- rootType: 'Query',
432
- set,
433
- selection,
434
- })
435
-
436
- // remove user 2 from the list
437
- cache.list('All_Users').remove({
438
- id: '2',
439
- })
440
-
441
- // the first time set was called, a new entry was added.
442
- // the second time it's called, we get a new value for mary-prime
443
- expect(set).toHaveBeenCalledWith({
444
- viewer: {
445
- id: '1',
446
- friends: {
447
- edges: [
448
- {
449
- node: {
450
- __typename: 'User',
451
- id: '3',
452
- firstName: 'jane',
453
- },
454
- },
455
- ],
456
- },
457
- },
458
- })
459
-
460
- // make sure we aren't subscribing to user 2 any more
461
- expect(cache._internal_unstable.subscriptions.get('User:2', 'firstName')).toHaveLength(0)
462
- // but we're still subscribing to user 3
463
- expect(cache._internal_unstable.subscriptions.get('User:3', 'firstName')).toHaveLength(1)
464
- })
465
-
466
- test('element removed from list can be added back', function () {
467
- // instantiate a cache
468
- const cache = new Cache(config)
469
-
470
- const selection: SubscriptionSelection = {
471
- viewer: {
472
- type: 'User',
473
- keyRaw: 'viewer',
474
- fields: {
475
- id: {
476
- type: 'ID',
477
- keyRaw: 'id',
478
- },
479
- friends: {
480
- type: 'User',
481
- keyRaw: 'friends',
482
- list: {
483
- name: 'All_Users',
484
- connection: true,
485
- type: 'User',
486
- },
487
- fields: {
488
- edges: {
489
- type: 'UserEdge',
490
- keyRaw: 'edges',
491
- fields: {
492
- node: {
493
- type: 'Node',
494
- keyRaw: 'node',
495
- abstract: true,
496
- fields: {
497
- __typename: {
498
- type: 'String',
499
- keyRaw: '__typename',
500
- },
501
- id: {
502
- type: 'ID',
503
- keyRaw: 'id',
504
- },
505
- firstName: {
506
- type: 'String',
507
- keyRaw: 'firstName',
508
- },
509
- },
510
- },
511
- },
512
- },
513
- },
514
- },
515
- },
516
- },
517
- }
518
-
519
- // start off associated with one object
520
- cache.write({
521
- selection,
522
- data: {
523
- viewer: {
524
- id: '1',
525
- friends: {
526
- edges: [
527
- {
528
- node: {
529
- __typename: 'User',
530
- id: '2',
531
- firstName: 'jane2',
532
- },
533
- },
534
- {
535
- node: {
536
- __typename: 'User',
537
- id: '3',
538
- firstName: 'jane',
539
- },
540
- },
541
- ],
542
- },
543
- },
544
- },
545
- })
546
-
547
- // a function to spy on that will play the role of set
548
- const set = vi.fn()
549
-
550
- // subscribe to the fields
551
- cache.subscribe({
552
- rootType: 'Query',
553
- set,
554
- selection,
555
- })
556
-
557
- // remove user 2 from the list
558
- cache.list('All_Users').remove({
559
- id: '2',
560
- })
561
-
562
- cache.list('All_Users').append(
563
- {
564
- id: {
565
- keyRaw: 'id',
566
- type: 'String',
567
- },
568
- firstName: {
569
- keyRaw: 'firstName',
570
- type: 'String',
571
- },
572
- },
573
- {
574
- __typename: 'User',
575
- id: '2',
576
- firstName: 'jane2',
577
- },
578
- {}
579
- )
580
-
581
- expect(set).toHaveBeenNthCalledWith(2, {
582
- viewer: {
583
- id: '1',
584
- friends: {
585
- edges: [
586
- {
587
- node: {
588
- __typename: 'User',
589
- id: '3',
590
- firstName: 'jane',
591
- },
592
- },
593
- {
594
- node: {
595
- __typename: 'User',
596
- id: '2',
597
- firstName: 'jane2',
598
- },
599
- },
600
- ],
601
- },
602
- },
603
- })
604
- })
605
-
606
- test('append in connection', function () {
607
- // instantiate a cache
608
- const cache = new Cache(config)
609
-
610
- const selection: SubscriptionSelection = {
611
- viewer: {
612
- type: 'User',
613
- keyRaw: 'viewer',
614
- fields: {
615
- id: {
616
- type: 'ID',
617
- keyRaw: 'id',
618
- },
619
- friends: {
620
- type: 'User',
621
- keyRaw: 'friends',
622
- list: {
623
- name: 'All_Users',
624
- connection: true,
625
- type: 'User',
626
- },
627
- fields: {
628
- edges: {
629
- type: 'UserEdge',
630
- keyRaw: 'edges',
631
- fields: {
632
- node: {
633
- type: 'Node',
634
- keyRaw: 'node',
635
- abstract: true,
636
- fields: {
637
- __typename: {
638
- type: 'String',
639
- keyRaw: '__typename',
640
- },
641
- id: {
642
- type: 'ID',
643
- keyRaw: 'id',
644
- },
645
- firstName: {
646
- type: 'String',
647
- keyRaw: 'firstName',
648
- },
649
- },
650
- },
651
- },
652
- },
653
- },
654
- },
655
- },
656
- },
657
- }
658
-
659
- // start off associated with one object
660
- cache.write({
661
- selection,
662
- data: {
663
- viewer: {
664
- id: '1',
665
- friends: {
666
- edges: [
667
- {
668
- node: {
669
- __typename: 'User',
670
- id: '2',
671
- firstName: 'jane',
672
- },
673
- },
674
- ],
675
- },
676
- },
677
- },
678
- })
679
-
680
- // a function to spy on that will play the role of set
681
- const set = vi.fn()
682
-
683
- // subscribe to the fields
684
- cache.subscribe({
685
- rootType: 'Query',
686
- set,
687
- selection,
688
- })
689
-
690
- // insert an element into the list (no parent ID)
691
- cache.list('All_Users').append(
692
- { id: { type: 'ID', keyRaw: 'id' }, firstName: { type: 'String', keyRaw: 'firstName' } },
693
- {
694
- id: '3',
695
- firstName: 'mary',
696
- }
697
- )
698
-
699
- // make sure we got the new value
700
- expect(set).toHaveBeenCalledWith({
701
- viewer: {
702
- id: '1',
703
- friends: {
704
- edges: [
705
- {
706
- node: {
707
- __typename: 'User',
708
- id: '2',
709
- firstName: 'jane',
710
- },
711
- },
712
- {
713
- node: {
714
- __typename: 'User',
715
- id: '3',
716
- firstName: 'mary',
717
- },
718
- },
719
- ],
720
- },
721
- },
722
- })
723
- })
724
-
725
- test('inserting data with an update overwrites a record inserted with list.append', function () {
726
- // instantiate a cache
727
- const cache = new Cache(config)
728
-
729
- const selection: SubscriptionSelection = {
730
- viewer: {
731
- type: 'User',
732
- keyRaw: 'viewer',
733
- fields: {
734
- id: {
735
- type: 'ID',
736
- keyRaw: 'id',
737
- },
738
- friends: {
739
- type: 'User',
740
- keyRaw: 'friends',
741
- list: {
742
- name: 'All_Users',
743
- connection: true,
744
- type: 'User',
745
- },
746
- fields: {
747
- edges: {
748
- type: 'UserEdge',
749
- keyRaw: 'edges',
750
- fields: {
751
- node: {
752
- type: 'Node',
753
- keyRaw: 'node',
754
- abstract: true,
755
- fields: {
756
- __typename: {
757
- type: 'String',
758
- keyRaw: '__typename',
759
- },
760
- id: {
761
- type: 'ID',
762
- keyRaw: 'id',
763
- },
764
- firstName: {
765
- type: 'String',
766
- keyRaw: 'firstName',
767
- },
768
- },
769
- },
770
- },
771
- },
772
- },
773
- },
774
- },
775
- },
776
- }
777
-
778
- // start off associated with one object
779
- cache.write({
780
- selection,
781
- data: {
782
- viewer: {
783
- id: '1',
784
- friends: {
785
- edges: [
786
- {
787
- node: {
788
- __typename: 'User',
789
- id: '2',
790
- firstName: 'jane',
791
- },
792
- },
793
- ],
794
- },
795
- },
796
- },
797
- })
798
-
799
- // a function to spy on that will play the role of set
800
- const set = vi.fn()
801
-
802
- // subscribe to the fields
803
- cache.subscribe({
804
- rootType: 'Query',
805
- set,
806
- selection,
807
- })
808
-
809
- // insert an element into the list (no parent ID)
810
- cache.list('All_Users').append(
811
- { id: { type: 'ID', keyRaw: 'id' }, firstName: { type: 'String', keyRaw: 'firstName' } },
812
- {
813
- id: '3',
814
- firstName: 'mary',
815
- }
816
- )
817
-
818
- // insert a record with a query update
819
- cache.write({
820
- applyUpdates: true,
821
- data: {
822
- viewer: {
823
- id: '1',
824
- firstName: 'John',
825
- friends: {
826
- edges: [
827
- {
828
- cursor: '1234',
829
- node: {
830
- __typename: 'User',
831
- id: '3',
832
- firstName: 'mary',
833
- },
834
- },
835
- ],
836
- },
837
- },
838
- },
839
- selection: {
840
- viewer: {
841
- type: 'User',
842
- keyRaw: 'viewer',
843
- fields: {
844
- id: {
845
- type: 'ID',
846
- keyRaw: 'id',
847
- },
848
- firstName: {
849
- type: 'String',
850
- keyRaw: 'firstName',
851
- },
852
- friends: {
853
- type: 'User',
854
- keyRaw: 'friends',
855
- fields: {
856
- edges: {
857
- type: 'UserEdge',
858
- keyRaw: 'edges',
859
- update: 'append',
860
- fields: {
861
- cursor: {
862
- type: 'String',
863
- keyRaw: 'cursor',
864
- },
865
- node: {
866
- type: 'User',
867
- keyRaw: 'node',
868
- fields: {
869
- __typename: {
870
- type: 'String',
871
- keyRaw: '__typename',
872
- },
873
- id: {
874
- type: 'ID',
875
- keyRaw: 'id',
876
- },
877
- firstName: {
878
- type: 'String',
879
- keyRaw: 'firstName',
880
- },
881
- },
882
- },
883
- },
884
- },
885
- },
886
- },
887
- },
888
- },
889
- } as SubscriptionSelection,
890
- })
891
-
892
- // make sure the duplicate has been removed
893
- expect(set).toHaveBeenNthCalledWith(2, {
894
- viewer: {
895
- id: '1',
896
- friends: {
897
- edges: [
898
- {
899
- node: {
900
- __typename: 'User',
901
- id: '2',
902
- firstName: 'jane',
903
- },
904
- },
905
- {
906
- node: {
907
- __typename: 'User',
908
- id: '3',
909
- firstName: 'mary',
910
- },
911
- },
912
- ],
913
- },
914
- },
915
- })
916
- })
917
-
918
- test('list filter - must_not positive', function () {
919
- // instantiate a cache
920
- const cache = new Cache(config)
921
-
922
- const selection: SubscriptionSelection = {
923
- viewer: {
924
- type: 'User',
925
- keyRaw: 'viewer',
926
- fields: {
927
- id: {
928
- type: 'ID',
929
- keyRaw: 'id',
930
- },
931
- friends: {
932
- type: 'User',
933
- keyRaw: 'friends',
934
- list: {
935
- name: 'All_Users',
936
- connection: false,
937
- type: 'User',
938
- },
939
- filters: {
940
- foo: {
941
- kind: 'String',
942
- value: 'bar',
943
- },
944
- },
945
- fields: {
946
- id: {
947
- type: 'ID',
948
- keyRaw: 'id',
949
- },
950
- firstName: {
951
- type: 'String',
952
- keyRaw: 'firstName',
953
- },
954
- },
955
- },
956
- },
957
- },
958
- }
959
-
960
- // start off associated with one object
961
- cache.write({
962
- selection,
963
- data: {
964
- viewer: {
965
- id: '1',
966
- friends: [
967
- {
968
- id: '2',
969
- firstName: 'jane',
970
- },
971
- ],
972
- },
973
- },
974
- })
975
-
976
- // a function to spy on that will play the role of set
977
- const set = vi.fn()
978
-
979
- // subscribe to the fields
980
- cache.subscribe({
981
- rootType: 'Query',
982
- set,
983
- selection,
984
- })
985
-
986
- // insert an element into the list (no parent ID)
987
- cache
988
- .list('All_Users')
989
- .when({ must_not: { foo: 'not-bar' } })
990
- .prepend(
991
- {
992
- id: { type: 'ID', keyRaw: 'id' },
993
- firstName: { type: 'String', keyRaw: 'firstName' },
994
- },
995
- {
996
- id: '3',
997
- firstName: 'mary',
998
- }
999
- )
1000
-
1001
- // make sure we got the new value
1002
- expect(set).toHaveBeenCalledWith({
1003
- viewer: {
1004
- id: '1',
1005
- friends: [
1006
- {
1007
- firstName: 'mary',
1008
- id: '3',
1009
- },
1010
- {
1011
- firstName: 'jane',
1012
- id: '2',
1013
- },
1014
- ],
1015
- },
1016
- })
1017
- })
1018
-
1019
- test('list filter - must_not negative', function () {
1020
- // instantiate a cache
1021
- const cache = new Cache(config)
1022
-
1023
- const selection: SubscriptionSelection = {
1024
- viewer: {
1025
- type: 'User',
1026
- keyRaw: 'viewer',
1027
- fields: {
1028
- id: {
1029
- type: 'ID',
1030
- keyRaw: 'id',
1031
- },
1032
- friends: {
1033
- type: 'User',
1034
- keyRaw: 'friends',
1035
- list: {
1036
- name: 'All_Users',
1037
- connection: false,
1038
- type: 'User',
1039
- },
1040
- filters: {
1041
- foo: {
1042
- kind: 'String',
1043
- value: 'bar',
1044
- },
1045
- },
1046
- fields: {
1047
- id: {
1048
- type: 'ID',
1049
- keyRaw: 'id',
1050
- },
1051
- firstName: {
1052
- type: 'String',
1053
- keyRaw: 'firstName',
1054
- },
1055
- },
1056
- },
1057
- },
1058
- },
1059
- }
1060
-
1061
- // start off associated with one object
1062
- cache.write({
1063
- selection,
1064
- data: {
1065
- viewer: {
1066
- id: '1',
1067
- friends: [
1068
- {
1069
- id: '2',
1070
- firstName: 'jane',
1071
- },
1072
- ],
1073
- },
1074
- },
1075
- })
1076
-
1077
- // a function to spy on that will play the role of set
1078
- const set = vi.fn()
1079
-
1080
- // subscribe to the fields
1081
- cache.subscribe({
1082
- rootType: 'Query',
1083
- set,
1084
- selection,
1085
- })
1086
-
1087
- // insert an element into the list (no parent ID)
1088
- cache
1089
- .list('All_Users')
1090
- .when({ must_not: { foo: 'bar' } })
1091
- .prepend(
1092
- {
1093
- id: { type: 'ID', keyRaw: 'id' },
1094
- firstName: { type: 'String', keyRaw: 'firstName' },
1095
- },
1096
- {
1097
- id: '3',
1098
- firstName: 'mary',
1099
- }
1100
- )
1101
-
1102
- // make sure we got the new value
1103
- expect(set).not.toHaveBeenCalled()
1104
- })
1105
-
1106
- test('list filter - must positive', function () {
1107
- // instantiate a cache
1108
- const cache = new Cache(config)
1109
-
1110
- const selection: SubscriptionSelection = {
1111
- viewer: {
1112
- type: 'User',
1113
- keyRaw: 'viewer',
1114
- fields: {
1115
- id: {
1116
- type: 'ID',
1117
- keyRaw: 'id',
1118
- },
1119
- friends: {
1120
- type: 'User',
1121
- keyRaw: 'friends',
1122
- list: {
1123
- name: 'All_Users',
1124
- connection: false,
1125
- type: 'User',
1126
- },
1127
- filters: {
1128
- foo: {
1129
- kind: 'String',
1130
- value: 'bar',
1131
- },
1132
- },
1133
- fields: {
1134
- id: {
1135
- type: 'ID',
1136
- keyRaw: 'id',
1137
- },
1138
- firstName: {
1139
- type: 'String',
1140
- keyRaw: 'firstName',
1141
- },
1142
- },
1143
- },
1144
- },
1145
- },
1146
- }
1147
-
1148
- // start off associated with one object
1149
- cache.write({
1150
- selection,
1151
- data: {
1152
- viewer: {
1153
- id: '1',
1154
- friends: [
1155
- {
1156
- id: '2',
1157
- firstName: 'jane',
1158
- },
1159
- ],
1160
- },
1161
- },
1162
- })
1163
-
1164
- // a function to spy on that will play the role of set
1165
- const set = vi.fn()
1166
-
1167
- // subscribe to the fields
1168
- cache.subscribe({
1169
- rootType: 'Query',
1170
- set,
1171
- selection,
1172
- })
1173
-
1174
- // insert an element into the list (no parent ID)
1175
- cache
1176
- .list('All_Users')
1177
- .when({ must: { foo: 'bar' } })
1178
- .prepend(
1179
- {
1180
- id: { type: 'ID', keyRaw: 'id' },
1181
- firstName: { type: 'String', keyRaw: 'firstName' },
1182
- },
1183
- {
1184
- id: '3',
1185
- firstName: 'mary',
1186
- }
1187
- )
1188
-
1189
- // make sure we got the new value
1190
- expect(set).toHaveBeenCalledWith({
1191
- viewer: {
1192
- id: '1',
1193
- friends: [
1194
- {
1195
- firstName: 'mary',
1196
- id: '3',
1197
- },
1198
- {
1199
- firstName: 'jane',
1200
- id: '2',
1201
- },
1202
- ],
1203
- },
1204
- })
1205
- })
1206
-
1207
- test('list filter - must negative', function () {
1208
- // instantiate a cache
1209
- const cache = new Cache(config)
1210
-
1211
- const selection: SubscriptionSelection = {
1212
- viewer: {
1213
- type: 'User',
1214
- keyRaw: 'viewer',
1215
- fields: {
1216
- id: {
1217
- type: 'ID',
1218
- keyRaw: 'id',
1219
- },
1220
- friends: {
1221
- type: 'User',
1222
- keyRaw: 'friends',
1223
- list: {
1224
- name: 'All_Users',
1225
- connection: false,
1226
- type: 'User',
1227
- },
1228
- filters: {
1229
- foo: {
1230
- kind: 'String',
1231
- value: 'bar',
1232
- },
1233
- },
1234
- fields: {
1235
- id: {
1236
- type: 'ID',
1237
- keyRaw: 'id',
1238
- },
1239
- firstName: {
1240
- type: 'String',
1241
- keyRaw: 'firstName',
1242
- },
1243
- },
1244
- },
1245
- },
1246
- },
1247
- }
1248
-
1249
- // start off associated with one object
1250
- cache.write({
1251
- selection,
1252
- data: {
1253
- viewer: {
1254
- id: '1',
1255
- friends: [
1256
- {
1257
- id: '2',
1258
- firstName: 'jane',
1259
- },
1260
- ],
1261
- },
1262
- },
1263
- })
1264
-
1265
- // a function to spy on that will play the role of set
1266
- const set = vi.fn()
1267
-
1268
- // subscribe to the fields
1269
- cache.subscribe({
1270
- rootType: 'Query',
1271
- set,
1272
- selection,
1273
- })
1274
-
1275
- // insert an element into the list (no parent ID)
1276
- cache
1277
- .list('All_Users')
1278
- .when({ must: { foo: 'not-bar' } })
1279
- .prepend(
1280
- {
1281
- id: { type: 'ID', keyRaw: 'id' },
1282
- firstName: { type: 'String', keyRaw: 'firstName' },
1283
- },
1284
- {
1285
- id: '3',
1286
- firstName: 'mary',
1287
- }
1288
- )
1289
-
1290
- // make sure we got the new value
1291
- expect(set).not.toHaveBeenCalled()
1292
- })
1293
-
1294
- test('remove from list', function () {
1295
- // instantiate a cache
1296
- const cache = new Cache(config)
1297
-
1298
- const selection = {
1299
- viewer: {
1300
- type: 'User',
1301
- keyRaw: 'viewer',
1302
- fields: {
1303
- id: {
1304
- type: 'ID',
1305
- keyRaw: 'id',
1306
- },
1307
- friends: {
1308
- type: 'User',
1309
- keyRaw: 'friends',
1310
- list: {
1311
- name: 'All_Users',
1312
- connection: false,
1313
- type: 'User',
1314
- },
1315
- fields: {
1316
- id: {
1317
- type: 'ID',
1318
- keyRaw: 'id',
1319
- },
1320
- firstName: {
1321
- type: 'String',
1322
- keyRaw: 'firstName',
1323
- },
1324
- },
1325
- },
1326
- },
1327
- },
1328
- }
1329
-
1330
- // start off associated with one object
1331
- cache.write({
1332
- selection,
1333
- data: {
1334
- viewer: {
1335
- id: '1',
1336
- friends: [
1337
- {
1338
- id: '2',
1339
- firstName: 'jane',
1340
- },
1341
- ],
1342
- },
1343
- },
1344
- })
1345
-
1346
- // a function to spy on that will play the role of set
1347
- const set = vi.fn()
1348
-
1349
- // subscribe to the fields
1350
- cache.subscribe({
1351
- rootType: 'Query',
1352
- set,
1353
- selection,
1354
- })
1355
-
1356
- // remove user 2 from the list
1357
- cache.list('All_Users').remove({
1358
- id: '2',
1359
- })
1360
-
1361
- // the first time set was called, a new entry was added.
1362
- // the second time it's called, we get a new value for mary-prime
1363
- expect(set).toHaveBeenCalledWith({
1364
- viewer: {
1365
- id: '1',
1366
- friends: [],
1367
- },
1368
- })
1369
-
1370
- // make sure we aren't subscribing to user 2 any more
1371
- expect(cache._internal_unstable.subscriptions.get('User:2', 'firstName')).toHaveLength(0)
1372
- })
1373
-
1374
- test('delete node', function () {
1375
- // instantiate a cache
1376
- const cache = new Cache(config)
1377
-
1378
- const selection = {
1379
- viewer: {
1380
- type: 'User',
1381
- keyRaw: 'viewer',
1382
- fields: {
1383
- id: {
1384
- type: 'ID',
1385
- keyRaw: 'id',
1386
- },
1387
- friends: {
1388
- type: 'User',
1389
- keyRaw: 'friends',
1390
- list: {
1391
- name: 'All_Users',
1392
- connection: false,
1393
- type: 'User',
1394
- },
1395
- fields: {
1396
- id: {
1397
- type: 'ID',
1398
- keyRaw: 'id',
1399
- },
1400
- firstName: {
1401
- type: 'String',
1402
- keyRaw: 'firstName',
1403
- },
1404
- },
1405
- },
1406
- },
1407
- },
1408
- }
1409
-
1410
- // start off associated with one object
1411
- cache.write({
1412
- selection,
1413
- data: {
1414
- viewer: {
1415
- id: '1',
1416
- friends: [
1417
- {
1418
- id: '2',
1419
- firstName: 'jane',
1420
- },
1421
- ],
1422
- },
1423
- },
1424
- })
1425
-
1426
- // a function to spy on that will play the role of set
1427
- const set = vi.fn()
1428
-
1429
- // subscribe to the fields
1430
- cache.subscribe({
1431
- rootType: 'Query',
1432
- set,
1433
- selection,
1434
- })
1435
-
1436
- // remove user 2 from the list
1437
- cache.delete(
1438
- cache._internal_unstable.id('User', {
1439
- id: '2',
1440
- })!
1441
- )
1442
-
1443
- // we should have been updated with an empty list
1444
- expect(set).toHaveBeenCalledWith({
1445
- viewer: {
1446
- id: '1',
1447
- friends: [],
1448
- },
1449
- })
1450
-
1451
- // make sure its empty now
1452
- expect(cache._internal_unstable.storage.topLayer.operations['User:2'].deleted).toBeTruthy()
1453
- })
1454
-
1455
- test('delete node from connection', function () {
1456
- // instantiate a cache
1457
- const cache = new Cache(config)
1458
-
1459
- const selection: SubscriptionSelection = {
1460
- viewer: {
1461
- type: 'User',
1462
- keyRaw: 'viewer',
1463
- fields: {
1464
- id: {
1465
- type: 'ID',
1466
- keyRaw: 'id',
1467
- },
1468
- friends: {
1469
- type: 'User',
1470
- keyRaw: 'friends',
1471
- list: {
1472
- name: 'All_Users',
1473
- connection: true,
1474
- type: 'User',
1475
- },
1476
- fields: {
1477
- edges: {
1478
- type: 'UserEdge',
1479
- keyRaw: 'edges',
1480
- fields: {
1481
- node: {
1482
- type: 'Node',
1483
- keyRaw: 'node',
1484
- abstract: true,
1485
- fields: {
1486
- __typename: {
1487
- type: 'String',
1488
- keyRaw: '__typename',
1489
- },
1490
- id: {
1491
- type: 'ID',
1492
- keyRaw: 'id',
1493
- },
1494
- firstName: {
1495
- type: 'String',
1496
- keyRaw: 'firstName',
1497
- },
1498
- },
1499
- },
1500
- },
1501
- },
1502
- },
1503
- },
1504
- },
1505
- },
1506
- }
1507
-
1508
- // start off associated with one object
1509
- cache.write({
1510
- selection,
1511
- data: {
1512
- viewer: {
1513
- id: '1',
1514
- friends: {
1515
- edges: [
1516
- {
1517
- node: {
1518
- __typename: 'User',
1519
- id: '2',
1520
- firstName: 'jane',
1521
- },
1522
- },
1523
- ],
1524
- },
1525
- },
1526
- },
1527
- })
1528
-
1529
- // a function to spy on that will play the role of set
1530
- const set = vi.fn()
1531
-
1532
- // subscribe to the fields
1533
- cache.subscribe({
1534
- rootType: 'Query',
1535
- set,
1536
- selection,
1537
- })
1538
-
1539
- // remove user 2 from the list
1540
- cache.delete(
1541
- cache._internal_unstable.id('User', {
1542
- id: '2',
1543
- })!
1544
- )
1545
-
1546
- // we should have been updated with an empty list
1547
- expect(set).toHaveBeenCalledWith({
1548
- viewer: {
1549
- id: '1',
1550
- friends: {
1551
- edges: [],
1552
- },
1553
- },
1554
- })
1555
-
1556
- // make sure its empty now
1557
- expect(cache._internal_unstable.storage.topLayer.operations['User:2'].deleted).toBeTruthy()
1558
- })
1559
-
1560
- test('append operation', function () {
1561
- // instantiate a cache
1562
- const cache = new Cache(config)
1563
-
1564
- // create a list we will add to
1565
- cache.write({
1566
- selection: {
1567
- viewer: {
1568
- type: 'User',
1569
- keyRaw: 'viewer',
1570
- fields: {
1571
- id: {
1572
- type: 'ID',
1573
- keyRaw: 'id',
1574
- },
1575
- },
1576
- },
1577
- },
1578
- data: {
1579
- viewer: {
1580
- id: '1',
1581
- },
1582
- },
1583
- })
1584
-
1585
- // subscribe to the data to register the list
1586
- cache.subscribe(
1587
- {
1588
- rootType: 'User',
1589
- selection: {
1590
- friends: {
1591
- type: 'User',
1592
- keyRaw: 'friends',
1593
- list: {
1594
- name: 'All_Users',
1595
- connection: false,
1596
- type: 'User',
1597
- },
1598
- fields: {
1599
- id: {
1600
- type: 'ID',
1601
- keyRaw: 'id',
1602
- },
1603
- firstName: {
1604
- type: 'String',
1605
- keyRaw: 'firstName',
1606
- },
1607
- },
1608
- },
1609
- },
1610
- parentID: cache._internal_unstable.id('User', '1')!,
1611
- set: vi.fn(),
1612
- },
1613
- {}
1614
- )
1615
-
1616
- // write some data to a different location with a new user
1617
- // that should be added to the list
1618
- cache.write({
1619
- selection: {
1620
- newUser: {
1621
- type: 'User',
1622
- keyRaw: 'newUser',
1623
- operations: [
1624
- {
1625
- action: 'insert',
1626
- list: 'All_Users',
1627
- },
1628
- ],
1629
- fields: {
1630
- id: {
1631
- type: 'ID',
1632
- keyRaw: 'id',
1633
- },
1634
- },
1635
- },
1636
- },
1637
- data: {
1638
- newUser: {
1639
- id: '3',
1640
- },
1641
- },
1642
- })
1643
-
1644
- // make sure we just added to the list
1645
- expect([...cache.list('All_Users', '1')]).toHaveLength(1)
1646
- })
1647
-
1648
- test('append from list', function () {
1649
- // instantiate a cache
1650
- const cache = new Cache(config)
1651
-
1652
- // create a list we will add to
1653
- cache.write({
1654
- selection: {
1655
- viewer: {
1656
- type: 'User',
1657
- keyRaw: 'viewer',
1658
- fields: {
1659
- id: {
1660
- type: 'ID',
1661
- keyRaw: 'id',
1662
- },
1663
- },
1664
- },
1665
- },
1666
- data: {
1667
- viewer: {
1668
- id: '1',
1669
- },
1670
- },
1671
- })
1672
-
1673
- // subscribe to the data to register the list
1674
- cache.subscribe(
1675
- {
1676
- rootType: 'User',
1677
- selection: {
1678
- friends: {
1679
- type: 'User',
1680
- keyRaw: 'friends',
1681
- list: {
1682
- name: 'All_Users',
1683
- connection: false,
1684
- type: 'User',
1685
- },
1686
- fields: {
1687
- id: {
1688
- type: 'ID',
1689
- keyRaw: 'id',
1690
- },
1691
- firstName: {
1692
- type: 'String',
1693
- keyRaw: 'firstName',
1694
- },
1695
- },
1696
- },
1697
- },
1698
- parentID: cache._internal_unstable.id('User', '1')!,
1699
- set: vi.fn(),
1700
- },
1701
- {}
1702
- )
1703
-
1704
- // write some data to a different location with a new user
1705
- // that should be added to the list
1706
- cache.write({
1707
- selection: {
1708
- newUser: {
1709
- type: 'User',
1710
- keyRaw: 'newUser',
1711
- operations: [
1712
- {
1713
- action: 'insert',
1714
- list: 'All_Users',
1715
- },
1716
- ],
1717
- fields: {
1718
- id: {
1719
- type: 'ID',
1720
- keyRaw: 'id',
1721
- },
1722
- },
1723
- },
1724
- },
1725
- data: {
1726
- newUser: [{ id: '3' }, { id: '4' }],
1727
- },
1728
- })
1729
-
1730
- // make sure we just added to the list
1731
- expect([...cache.list('All_Users', '1')]).toHaveLength(2)
1732
- })
1733
-
1734
- test('toggle list', function () {
1735
- // instantiate a cache
1736
- const cache = new Cache(config)
1737
-
1738
- // create a list we will add to
1739
- cache.write({
1740
- selection: {
1741
- viewer: {
1742
- type: 'User',
1743
- keyRaw: 'viewer',
1744
- fields: {
1745
- id: {
1746
- type: 'ID',
1747
- keyRaw: 'id',
1748
- },
1749
- friends: {
1750
- type: 'User',
1751
- keyRaw: 'friends',
1752
- list: {
1753
- name: 'All_Users',
1754
- connection: false,
1755
- type: 'User',
1756
- },
1757
- fields: {
1758
- id: {
1759
- type: 'ID',
1760
- keyRaw: 'id',
1761
- },
1762
- firstName: {
1763
- type: 'String',
1764
- keyRaw: 'firstName',
1765
- },
1766
- },
1767
- },
1768
- },
1769
- },
1770
- },
1771
- data: {
1772
- viewer: {
1773
- id: '1',
1774
- friends: [{ id: '5' }],
1775
- },
1776
- },
1777
- })
1778
-
1779
- // subscribe to the data to register the list
1780
- cache.subscribe(
1781
- {
1782
- rootType: 'User',
1783
- selection: {
1784
- friends: {
1785
- type: 'User',
1786
- keyRaw: 'friends',
1787
- list: {
1788
- name: 'All_Users',
1789
- connection: false,
1790
- type: 'User',
1791
- },
1792
- fields: {
1793
- id: {
1794
- type: 'ID',
1795
- keyRaw: 'id',
1796
- },
1797
- firstName: {
1798
- type: 'String',
1799
- keyRaw: 'firstName',
1800
- },
1801
- },
1802
- },
1803
- },
1804
- parentID: cache._internal_unstable.id('User', '1')!,
1805
- set: vi.fn(),
1806
- },
1807
- {}
1808
- )
1809
-
1810
- const toggleSelection: SubscriptionSelection = {
1811
- newUser: {
1812
- type: 'User',
1813
- keyRaw: 'newUser',
1814
- operations: [
1815
- {
1816
- action: 'toggle',
1817
- list: 'All_Users',
1818
- },
1819
- ],
1820
- fields: {
1821
- id: {
1822
- type: 'ID',
1823
- keyRaw: 'id',
1824
- },
1825
- },
1826
- },
1827
- }
1828
-
1829
- // write some data to a different location with a new user
1830
- // that should be added to the list
1831
- cache.write({ selection: toggleSelection, data: { newUser: { id: '3' } } })
1832
- expect([...cache.list('All_Users', '1')]).toEqual(['User:5', 'User:3'])
1833
-
1834
- // toggle the user again to remove the user
1835
- cache.write({ selection: toggleSelection, data: { newUser: { id: '3' } } })
1836
- expect([...cache.list('All_Users', '1')]).toEqual(['User:5'])
1837
-
1838
- // toggle the user again to add the user back
1839
- cache.write({ selection: toggleSelection, data: { newUser: { id: '3' } } })
1840
- expect([...cache.list('All_Users', '1')]).toEqual(['User:5', 'User:3'])
1841
- })
1842
-
1843
- test('append when operation', function () {
1844
- // instantiate a cache
1845
- const cache = new Cache(config)
1846
-
1847
- // create a list we will add to
1848
- cache.write({
1849
- selection: {
1850
- viewer: {
1851
- type: 'User',
1852
- keyRaw: 'viewer',
1853
- fields: {
1854
- id: {
1855
- type: 'ID',
1856
- keyRaw: 'id',
1857
- },
1858
- },
1859
- },
1860
- },
1861
- data: {
1862
- viewer: {
1863
- id: '1',
1864
- },
1865
- },
1866
- })
1867
-
1868
- // subscribe to the data to register the list
1869
- cache.subscribe(
1870
- {
1871
- rootType: 'User',
1872
- selection: {
1873
- friends: {
1874
- type: 'User',
1875
- keyRaw: 'friends',
1876
- list: {
1877
- name: 'All_Users',
1878
- connection: false,
1879
- type: 'User',
1880
- },
1881
- filters: {
1882
- value: {
1883
- kind: 'String',
1884
- value: 'foo',
1885
- },
1886
- },
1887
- fields: {
1888
- id: {
1889
- type: 'ID',
1890
- keyRaw: 'id',
1891
- },
1892
- firstName: {
1893
- type: 'String',
1894
- keyRaw: 'firstName',
1895
- },
1896
- },
1897
- },
1898
- },
1899
- parentID: cache._internal_unstable.id('User', '1')!,
1900
- set: vi.fn(),
1901
- },
1902
- {}
1903
- )
1904
-
1905
- // write some data to a different location with a new user
1906
- // that should be added to the list
1907
- cache.write({
1908
- selection: {
1909
- newUser: {
1910
- type: 'User',
1911
- keyRaw: 'newUser',
1912
- operations: [
1913
- {
1914
- action: 'insert',
1915
- list: 'All_Users',
1916
- when: {
1917
- must: {
1918
- value: 'not-foo',
1919
- },
1920
- },
1921
- },
1922
- ],
1923
- fields: {
1924
- id: {
1925
- type: 'ID',
1926
- keyRaw: 'id',
1927
- },
1928
- },
1929
- },
1930
- },
1931
- data: {
1932
- newUser: {
1933
- id: '3',
1934
- },
1935
- },
1936
- })
1937
-
1938
- // make sure we just added to the list
1939
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
1940
- })
1941
-
1942
- test('prepend when operation', function () {
1943
- // instantiate a cache
1944
- const cache = new Cache(config)
1945
-
1946
- // create a list we will add to
1947
- cache.write({
1948
- selection: {
1949
- viewer: {
1950
- type: 'User',
1951
- keyRaw: 'viewer',
1952
- fields: {
1953
- id: {
1954
- type: 'ID',
1955
- keyRaw: 'id',
1956
- },
1957
- },
1958
- },
1959
- },
1960
- data: {
1961
- viewer: {
1962
- id: '1',
1963
- },
1964
- },
1965
- })
1966
-
1967
- // subscribe to the data to register the list
1968
- cache.subscribe(
1969
- {
1970
- rootType: 'User',
1971
- selection: {
1972
- friends: {
1973
- type: 'User',
1974
- keyRaw: 'friends',
1975
- list: {
1976
- name: 'All_Users',
1977
- connection: false,
1978
- type: 'User',
1979
- },
1980
- filters: {
1981
- value: {
1982
- kind: 'String',
1983
- value: 'foo',
1984
- },
1985
- },
1986
- fields: {
1987
- id: {
1988
- type: 'ID',
1989
- keyRaw: 'id',
1990
- },
1991
- firstName: {
1992
- type: 'String',
1993
- keyRaw: 'firstName',
1994
- },
1995
- },
1996
- },
1997
- },
1998
- parentID: cache._internal_unstable.id('User', '1')!,
1999
- set: vi.fn(),
2000
- },
2001
- {}
2002
- )
2003
-
2004
- // write some data to a different location with a new user
2005
- // that should be added to the list
2006
- cache.write({
2007
- selection: {
2008
- newUser: {
2009
- type: 'User',
2010
- keyRaw: 'newUser',
2011
- operations: [
2012
- {
2013
- action: 'insert',
2014
- list: 'All_Users',
2015
- position: 'first',
2016
- when: {
2017
- must: {
2018
- value: 'not-foo',
2019
- },
2020
- },
2021
- },
2022
- ],
2023
- fields: {
2024
- id: {
2025
- type: 'ID',
2026
- keyRaw: 'id',
2027
- },
2028
- },
2029
- },
2030
- },
2031
- data: {
2032
- newUser: {
2033
- id: '3',
2034
- },
2035
- },
2036
- })
2037
-
2038
- // make sure we just added to the list
2039
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
2040
- })
2041
-
2042
- test('prepend operation', function () {
2043
- // instantiate a cache
2044
- const cache = new Cache(config)
2045
-
2046
- // create a list we will add to
2047
- cache.write({
2048
- selection: {
2049
- viewer: {
2050
- type: 'User',
2051
- keyRaw: 'viewer',
2052
- fields: {
2053
- id: {
2054
- type: 'ID',
2055
- keyRaw: 'id',
2056
- },
2057
- friends: {
2058
- type: 'User',
2059
- keyRaw: 'friends',
2060
- fields: {
2061
- id: {
2062
- type: 'String',
2063
- keyRaw: 'id',
2064
- },
2065
- firstName: {
2066
- type: 'String',
2067
- keyRaw: 'firstName',
2068
- },
2069
- },
2070
- },
2071
- },
2072
- },
2073
- },
2074
- data: {
2075
- viewer: {
2076
- id: '1',
2077
- friends: [
2078
- {
2079
- id: '2',
2080
- firstName: 'mary',
2081
- },
2082
- ],
2083
- },
2084
- },
2085
- })
2086
-
2087
- // subscribe to the data to register the list
2088
- cache.subscribe(
2089
- {
2090
- rootType: 'User',
2091
- selection: {
2092
- friends: {
2093
- type: 'User',
2094
- keyRaw: 'friends',
2095
- list: {
2096
- name: 'All_Users',
2097
- connection: false,
2098
- type: 'User',
2099
- },
2100
- fields: {
2101
- id: {
2102
- type: 'ID',
2103
- keyRaw: 'id',
2104
- },
2105
- firstName: {
2106
- type: 'String',
2107
- keyRaw: 'firstName',
2108
- },
2109
- },
2110
- },
2111
- },
2112
- parentID: cache._internal_unstable.id('User', '1')!,
2113
- set: vi.fn(),
2114
- },
2115
- {}
2116
- )
2117
-
2118
- // write some data to a different location with a new user
2119
- // that should be added to the list
2120
- cache.write({
2121
- selection: {
2122
- newUser: {
2123
- type: 'User',
2124
- keyRaw: 'newUser',
2125
- operations: [
2126
- {
2127
- action: 'insert',
2128
- list: 'All_Users',
2129
- position: 'first',
2130
- },
2131
- ],
2132
- fields: {
2133
- id: {
2134
- type: 'ID',
2135
- keyRaw: 'id',
2136
- },
2137
- },
2138
- },
2139
- },
2140
- data: {
2141
- newUser: {
2142
- id: '3',
2143
- },
2144
- },
2145
- })
2146
-
2147
- // make sure we just added to the list
2148
- expect([...cache.list('All_Users', '1')]).toEqual(['User:3', 'User:2'])
2149
- })
2150
-
2151
- test('remove operation', function () {
2152
- // instantiate a cache
2153
- const cache = new Cache(config)
2154
-
2155
- // create a list we will add to
2156
- cache.write({
2157
- selection: {
2158
- viewer: {
2159
- type: 'User',
2160
- keyRaw: 'viewer',
2161
- fields: {
2162
- id: {
2163
- type: 'ID',
2164
- keyRaw: 'id',
2165
- },
2166
- friends: {
2167
- type: 'User',
2168
- keyRaw: 'friends',
2169
- fields: {
2170
- id: {
2171
- type: 'ID',
2172
- keyRaw: 'id',
2173
- },
2174
- firstName: {
2175
- type: 'String',
2176
- keyRaw: 'firstName',
2177
- },
2178
- },
2179
- },
2180
- },
2181
- },
2182
- },
2183
- data: {
2184
- viewer: {
2185
- id: '1',
2186
- friends: [{ id: '2', firstName: 'jane' }],
2187
- },
2188
- },
2189
- })
2190
-
2191
- // subscribe to the data to register the list
2192
- cache.subscribe(
2193
- {
2194
- rootType: 'User',
2195
- selection: {
2196
- friends: {
2197
- type: 'User',
2198
- keyRaw: 'friends',
2199
- list: {
2200
- name: 'All_Users',
2201
- connection: false,
2202
- type: 'User',
2203
- },
2204
- fields: {
2205
- id: {
2206
- type: 'ID',
2207
- keyRaw: 'id',
2208
- },
2209
- firstName: {
2210
- type: 'String',
2211
- keyRaw: 'firstName',
2212
- },
2213
- },
2214
- },
2215
- },
2216
- parentID: cache._internal_unstable.id('User', '1')!,
2217
- set: vi.fn(),
2218
- },
2219
- {}
2220
- )
2221
-
2222
- // write some data to a different location with a new user
2223
- // that should be removed from the operation
2224
- cache.write({
2225
- selection: {
2226
- newUser: {
2227
- type: 'User',
2228
- keyRaw: 'newUser',
2229
- operations: [
2230
- {
2231
- action: 'remove',
2232
- list: 'All_Users',
2233
- },
2234
- ],
2235
- fields: {
2236
- id: {
2237
- type: 'ID',
2238
- keyRaw: 'id',
2239
- },
2240
- },
2241
- },
2242
- },
2243
- data: {
2244
- newUser: {
2245
- id: '2',
2246
- },
2247
- },
2248
- })
2249
-
2250
- // make sure we removed the element from the list
2251
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
2252
- })
2253
-
2254
- test('remove operation from list', function () {
2255
- // instantiate a cache
2256
- const cache = new Cache(config)
2257
-
2258
- // create a list we will add to
2259
- cache.write({
2260
- selection: {
2261
- viewer: {
2262
- type: 'User',
2263
- keyRaw: 'viewer',
2264
- fields: {
2265
- id: {
2266
- type: 'ID',
2267
- keyRaw: 'id',
2268
- },
2269
- friends: {
2270
- type: 'User',
2271
- keyRaw: 'friends',
2272
- fields: {
2273
- id: {
2274
- type: 'ID',
2275
- keyRaw: 'id',
2276
- },
2277
- firstName: {
2278
- type: 'String',
2279
- keyRaw: 'firstName',
2280
- },
2281
- },
2282
- },
2283
- },
2284
- },
2285
- },
2286
- data: {
2287
- viewer: {
2288
- id: '1',
2289
- friends: [
2290
- { id: '2', firstName: 'jane' },
2291
- { id: '3', firstName: 'Alfred' },
2292
- ],
2293
- },
2294
- },
2295
- })
2296
-
2297
- // subscribe to the data to register the list
2298
- cache.subscribe(
2299
- {
2300
- rootType: 'User',
2301
- selection: {
2302
- friends: {
2303
- type: 'User',
2304
- keyRaw: 'friends',
2305
- list: {
2306
- name: 'All_Users',
2307
- connection: false,
2308
- type: 'User',
2309
- },
2310
- fields: {
2311
- id: {
2312
- type: 'ID',
2313
- keyRaw: 'id',
2314
- },
2315
- firstName: {
2316
- type: 'String',
2317
- keyRaw: 'firstName',
2318
- },
2319
- },
2320
- },
2321
- },
2322
- parentID: cache._internal_unstable.id('User', '1')!,
2323
- set: vi.fn(),
2324
- },
2325
- {}
2326
- )
2327
-
2328
- // write some data to a different location with a new user
2329
- // that should be removed from the operation
2330
- cache.write({
2331
- selection: {
2332
- newUser: {
2333
- type: 'User',
2334
- keyRaw: 'newUser',
2335
- operations: [
2336
- {
2337
- action: 'remove',
2338
- list: 'All_Users',
2339
- },
2340
- ],
2341
- fields: {
2342
- id: {
2343
- type: 'ID',
2344
- keyRaw: 'id',
2345
- },
2346
- },
2347
- },
2348
- },
2349
- data: {
2350
- newUser: [{ id: '2' }, { id: '3' }],
2351
- },
2352
- })
2353
-
2354
- // make sure we removed the element from the list
2355
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
2356
- })
2357
-
2358
- test('delete operation', function () {
2359
- // instantiate a cache
2360
- const cache = new Cache(config)
2361
-
2362
- // create a list we will add to
2363
- cache.write({
2364
- selection: {
2365
- viewer: {
2366
- type: 'User',
2367
- keyRaw: 'viewer',
2368
- fields: {
2369
- id: {
2370
- type: 'ID',
2371
- keyRaw: 'id',
2372
- },
2373
- friends: {
2374
- type: 'User',
2375
- keyRaw: 'friends',
2376
- fields: {
2377
- id: {
2378
- type: 'ID',
2379
- keyRaw: 'id',
2380
- },
2381
- firstName: {
2382
- type: 'String',
2383
- keyRaw: 'firstName',
2384
- },
2385
- },
2386
- },
2387
- },
2388
- },
2389
- },
2390
- data: {
2391
- viewer: {
2392
- id: '1',
2393
- friends: [{ id: '2', firstName: 'jane' }],
2394
- },
2395
- },
2396
- })
2397
-
2398
- // subscribe to the data to register the list
2399
- cache.subscribe(
2400
- {
2401
- rootType: 'User',
2402
- selection: {
2403
- friends: {
2404
- type: 'User',
2405
- keyRaw: 'friends',
2406
- list: {
2407
- name: 'All_Users',
2408
- connection: false,
2409
- type: 'User',
2410
- },
2411
- fields: {
2412
- id: {
2413
- type: 'ID',
2414
- keyRaw: 'id',
2415
- },
2416
- firstName: {
2417
- type: 'String',
2418
- keyRaw: 'firstName',
2419
- },
2420
- },
2421
- },
2422
- },
2423
- parentID: cache._internal_unstable.id('User', '1')!,
2424
- set: vi.fn(),
2425
- },
2426
- {}
2427
- )
2428
-
2429
- // write some data to a different location with a new user
2430
- // that should be added to the list
2431
- cache.write({
2432
- selection: {
2433
- deleteUser: {
2434
- type: 'User',
2435
- keyRaw: 'deleteUser',
2436
- fields: {
2437
- id: {
2438
- type: 'ID',
2439
- keyRaw: 'id',
2440
- operations: [
2441
- {
2442
- action: 'delete',
2443
- type: 'User',
2444
- },
2445
- ],
2446
- },
2447
- },
2448
- },
2449
- },
2450
- data: {
2451
- deleteUser: {
2452
- id: '2',
2453
- },
2454
- },
2455
- })
2456
-
2457
- // make sure we removed the element from the list
2458
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
2459
-
2460
- expect(cache._internal_unstable.storage.topLayer.operations['User:2'].deleted).toBeTruthy()
2461
- })
2462
-
2463
- test('delete operation from list', function () {
2464
- // instantiate a cache
2465
- const cache = new Cache(config)
2466
-
2467
- // create a list we will add to
2468
- cache.write({
2469
- selection: {
2470
- viewer: {
2471
- type: 'User',
2472
- keyRaw: 'viewer',
2473
- fields: {
2474
- id: {
2475
- type: 'ID',
2476
- keyRaw: 'id',
2477
- },
2478
- friends: {
2479
- type: 'User',
2480
- keyRaw: 'friends',
2481
- fields: {
2482
- id: {
2483
- type: 'ID',
2484
- keyRaw: 'id',
2485
- },
2486
- firstName: {
2487
- type: 'String',
2488
- keyRaw: 'firstName',
2489
- },
2490
- },
2491
- },
2492
- },
2493
- },
2494
- },
2495
- data: {
2496
- viewer: {
2497
- id: '1',
2498
- friends: [
2499
- { id: '2', firstName: 'jane' },
2500
- { id: '3', firstName: 'Alfred' },
2501
- ],
2502
- },
2503
- },
2504
- })
2505
-
2506
- // subscribe to the data to register the list
2507
- cache.subscribe(
2508
- {
2509
- rootType: 'User',
2510
- selection: {
2511
- friends: {
2512
- type: 'User',
2513
- keyRaw: 'friends',
2514
- list: {
2515
- name: 'All_Users',
2516
- connection: false,
2517
- type: 'User',
2518
- },
2519
- fields: {
2520
- id: {
2521
- type: 'ID',
2522
- keyRaw: 'id',
2523
- },
2524
- firstName: {
2525
- type: 'String',
2526
- keyRaw: 'firstName',
2527
- },
2528
- },
2529
- },
2530
- },
2531
- parentID: cache._internal_unstable.id('User', '1')!,
2532
- set: vi.fn(),
2533
- },
2534
- {}
2535
- )
2536
-
2537
- // write some data to a different location with a new user
2538
- // that should be added to the list
2539
- cache.write({
2540
- selection: {
2541
- deleteUser: {
2542
- type: 'User',
2543
- keyRaw: 'deleteUser',
2544
- fields: {
2545
- id: {
2546
- type: 'ID',
2547
- keyRaw: 'id',
2548
- operations: [
2549
- {
2550
- action: 'delete',
2551
- type: 'User',
2552
- },
2553
- ],
2554
- },
2555
- },
2556
- },
2557
- },
2558
- data: {
2559
- deleteUser: {
2560
- id: ['2', '3'],
2561
- },
2562
- },
2563
- })
2564
-
2565
- // make sure we removed the element from the list
2566
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
2567
-
2568
- expect(cache._internal_unstable.storage.topLayer.operations['User:2'].deleted).toBeTruthy()
2569
- expect(cache._internal_unstable.storage.topLayer.operations['User:3'].deleted).toBeTruthy()
2570
- })
2571
-
2572
- test('delete operation from connection', function () {
2573
- // instantiate a cache
2574
- const cache = new Cache(config)
2575
-
2576
- // create a list we will add to
2577
- cache.write({
2578
- selection: {
2579
- viewer: {
2580
- type: 'User',
2581
- keyRaw: 'viewer',
2582
- fields: {
2583
- id: {
2584
- type: 'ID',
2585
- keyRaw: 'id',
2586
- },
2587
- friends: {
2588
- type: 'User',
2589
- keyRaw: 'friends',
2590
- list: {
2591
- name: 'All_Users',
2592
- connection: true,
2593
- type: 'User',
2594
- },
2595
- fields: {
2596
- edges: {
2597
- type: 'UserEdge',
2598
- keyRaw: 'edges',
2599
- fields: {
2600
- node: {
2601
- type: 'Node',
2602
- keyRaw: 'node',
2603
- abstract: true,
2604
- fields: {
2605
- __typename: {
2606
- type: 'String',
2607
- keyRaw: '__typename',
2608
- },
2609
- id: {
2610
- type: 'ID',
2611
- keyRaw: 'id',
2612
- },
2613
- firstName: {
2614
- type: 'String',
2615
- keyRaw: 'firstName',
2616
- },
2617
- },
2618
- },
2619
- },
2620
- },
2621
- },
2622
- },
2623
- },
2624
- },
2625
- },
2626
- data: {
2627
- viewer: {
2628
- id: '1',
2629
- friends: {
2630
- edges: [{ node: { id: '2', firstName: 'jane', __typename: 'User' } }],
2631
- },
2632
- },
2633
- },
2634
- })
2635
-
2636
- // subscribe to the data to register the list
2637
- cache.subscribe(
2638
- {
2639
- rootType: 'User',
2640
- selection: {
2641
- friends: {
2642
- type: 'User',
2643
- keyRaw: 'friends',
2644
- list: {
2645
- name: 'All_Users',
2646
- connection: true,
2647
- type: 'User',
2648
- },
2649
- fields: {
2650
- edges: {
2651
- type: 'UserEdge',
2652
- keyRaw: 'edges',
2653
- fields: {
2654
- node: {
2655
- type: 'Node',
2656
- keyRaw: 'node',
2657
- abstract: true,
2658
- fields: {
2659
- __typename: {
2660
- type: 'String',
2661
- keyRaw: '__typename',
2662
- },
2663
- id: {
2664
- type: 'ID',
2665
- keyRaw: 'id',
2666
- },
2667
- firstName: {
2668
- type: 'String',
2669
- keyRaw: 'firstName',
2670
- },
2671
- },
2672
- },
2673
- },
2674
- },
2675
- },
2676
- },
2677
- },
2678
- parentID: cache._internal_unstable.id('User', '1')!,
2679
- set: vi.fn(),
2680
- },
2681
- {}
2682
- )
2683
-
2684
- // write some data to a different location with a new user
2685
- // that should be added to the list
2686
- cache.write({
2687
- selection: {
2688
- deleteUser: {
2689
- type: 'User',
2690
- keyRaw: 'deleteUser',
2691
- fields: {
2692
- id: {
2693
- type: 'ID',
2694
- keyRaw: 'id',
2695
- operations: [
2696
- {
2697
- action: 'delete',
2698
- type: 'User',
2699
- },
2700
- ],
2701
- },
2702
- },
2703
- },
2704
- },
2705
- data: {
2706
- deleteUser: {
2707
- id: '2',
2708
- },
2709
- },
2710
- })
2711
-
2712
- // make sure we removed the element from the list
2713
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
2714
- expect(cache._internal_unstable.storage.topLayer.operations['User:2'].deleted).toBeTruthy()
2715
- })
2716
-
2717
- test('disabled linked lists update', function () {
2718
- // instantiate the cache
2719
- const cache = new Cache(config)
2720
-
2721
- const selection = {
2722
- viewer: {
2723
- type: 'User',
2724
- keyRaw: 'viewer',
2725
- fields: {
2726
- id: {
2727
- type: 'ID',
2728
- keyRaw: 'id',
2729
- },
2730
- firstName: {
2731
- type: 'String',
2732
- keyRaw: 'firstName',
2733
- },
2734
- friends: {
2735
- type: 'User',
2736
- keyRaw: 'friends',
2737
- update: 'append',
2738
- fields: {
2739
- id: {
2740
- type: 'ID',
2741
- keyRaw: 'id',
2742
- },
2743
- firstName: {
2744
- type: 'String',
2745
- keyRaw: 'firstName',
2746
- },
2747
- },
2748
- },
2749
- },
2750
- },
2751
- } as SubscriptionSelection
2752
-
2753
- // add some data to the cache
2754
- cache.write({
2755
- selection,
2756
- data: {
2757
- viewer: {
2758
- id: '1',
2759
- firstName: 'bob',
2760
- friends: [
2761
- {
2762
- id: '2',
2763
- firstName: 'jane',
2764
- },
2765
- {
2766
- id: '3',
2767
- firstName: 'mary',
2768
- },
2769
- ],
2770
- },
2771
- },
2772
- })
2773
-
2774
- // make sure we can get the linked lists back
2775
- expect(
2776
- cache.read({
2777
- selection: { friends: selection.viewer.fields!.friends },
2778
- parent: 'User:1',
2779
- }).data
2780
- ).toEqual({
2781
- friends: [
2782
- {
2783
- id: '2',
2784
- firstName: 'jane',
2785
- },
2786
- {
2787
- id: '3',
2788
- firstName: 'mary',
2789
- },
2790
- ],
2791
- })
2792
-
2793
- // add some data to the cache
2794
- cache.write({
2795
- selection,
2796
- data: {
2797
- viewer: {
2798
- id: '1',
2799
- firstName: 'bob',
2800
- friends: [
2801
- {
2802
- id: '3',
2803
- firstName: 'jane',
2804
- },
2805
- {
2806
- id: '4',
2807
- firstName: 'mary',
2808
- },
2809
- ],
2810
- },
2811
- },
2812
- })
2813
-
2814
- // make sure we can get the linked lists back
2815
- expect(
2816
- cache.read({
2817
- selection: { friends: selection.viewer.fields!.friends },
2818
- parent: 'User:1',
2819
- }).data
2820
- ).toEqual({
2821
- friends: [
2822
- {
2823
- id: '3',
2824
- firstName: 'jane',
2825
- },
2826
- {
2827
- id: '4',
2828
- firstName: 'mary',
2829
- },
2830
- ],
2831
- })
2832
- })
2833
-
2834
- test('append linked lists update', function () {
2835
- // instantiate the cache
2836
- const cache = new Cache(config)
2837
-
2838
- const selection = {
2839
- viewer: {
2840
- type: 'User',
2841
- keyRaw: 'viewer',
2842
- fields: {
2843
- id: {
2844
- type: 'ID',
2845
- keyRaw: 'id',
2846
- },
2847
- firstName: {
2848
- type: 'String',
2849
- keyRaw: 'firstName',
2850
- },
2851
- friends: {
2852
- type: 'User',
2853
- keyRaw: 'friends',
2854
- update: 'append',
2855
- fields: {
2856
- id: {
2857
- type: 'ID',
2858
- keyRaw: 'id',
2859
- },
2860
- firstName: {
2861
- type: 'String',
2862
- keyRaw: 'firstName',
2863
- },
2864
- },
2865
- },
2866
- },
2867
- },
2868
- } as SubscriptionSelection
2869
-
2870
- // add some data to the cache
2871
- cache.write({
2872
- selection,
2873
- data: {
2874
- viewer: {
2875
- id: '1',
2876
- firstName: 'bob',
2877
- friends: [
2878
- {
2879
- id: '2',
2880
- firstName: 'jane',
2881
- },
2882
- {
2883
- id: '3',
2884
- firstName: 'mary',
2885
- },
2886
- ],
2887
- },
2888
- },
2889
- })
2890
-
2891
- // make sure we can get the linked lists back
2892
- expect(
2893
- cache.read({
2894
- selection: { friends: selection.viewer.fields!.friends },
2895
- parent: 'User:1',
2896
- }).data
2897
- ).toEqual({
2898
- friends: [
2899
- {
2900
- id: '2',
2901
- firstName: 'jane',
2902
- },
2903
- {
2904
- id: '3',
2905
- firstName: 'mary',
2906
- },
2907
- ],
2908
- })
2909
-
2910
- // add some data to the cache
2911
- cache.write({
2912
- selection,
2913
- data: {
2914
- viewer: {
2915
- id: '1',
2916
- firstName: 'bob',
2917
- friends: [
2918
- {
2919
- id: '4',
2920
- firstName: 'jane',
2921
- },
2922
- {
2923
- id: '5',
2924
- firstName: 'mary',
2925
- },
2926
- ],
2927
- },
2928
- },
2929
- applyUpdates: true,
2930
- })
2931
-
2932
- // make sure we can get the linked lists back
2933
- expect(
2934
- cache.read({
2935
- selection: { friends: selection.viewer.fields!.friends },
2936
- parent: 'User:1',
2937
- }).data
2938
- ).toEqual({
2939
- friends: [
2940
- {
2941
- id: '2',
2942
- firstName: 'jane',
2943
- },
2944
- {
2945
- id: '3',
2946
- firstName: 'mary',
2947
- },
2948
- {
2949
- id: '4',
2950
- firstName: 'jane',
2951
- },
2952
- {
2953
- id: '5',
2954
- firstName: 'mary',
2955
- },
2956
- ],
2957
- })
2958
- })
2959
-
2960
- test('writing a scalar marked with a disabled update overwrites', function () {
2961
- // instantiate the cache
2962
- const cache = new Cache(config)
2963
-
2964
- const selection = {
2965
- viewer: {
2966
- type: 'User',
2967
- keyRaw: 'viewer',
2968
- fields: {
2969
- id: {
2970
- type: 'ID',
2971
- keyRaw: 'id',
2972
- },
2973
- firstName: {
2974
- type: 'String',
2975
- keyRaw: 'firstName',
2976
- },
2977
- friends: {
2978
- type: 'Int',
2979
- keyRaw: 'friends',
2980
- update: 'append',
2981
- },
2982
- },
2983
- },
2984
- } as SubscriptionSelection
2985
-
2986
- // add some data to the cache
2987
- cache.write({
2988
- selection,
2989
- data: {
2990
- viewer: {
2991
- id: '1',
2992
- firstName: 'bob',
2993
- friends: [1],
2994
- },
2995
- },
2996
- })
2997
-
2998
- // make sure we can get the linked lists back
2999
- expect(cache.read({ selection }).data).toEqual({
3000
- viewer: {
3001
- id: '1',
3002
- firstName: 'bob',
3003
- friends: [1],
3004
- },
3005
- })
3006
-
3007
- // add some data to the cache
3008
- cache.write({
3009
- selection,
3010
- data: {
3011
- viewer: {
3012
- id: '1',
3013
- firstName: 'bob',
3014
- friends: [2],
3015
- },
3016
- },
3017
- })
3018
-
3019
- // make sure we can get the updated lists back
3020
- expect(cache.read({ selection }).data).toEqual({
3021
- viewer: {
3022
- id: '1',
3023
- firstName: 'bob',
3024
- friends: [2],
3025
- },
3026
- })
3027
- })
3028
-
3029
- test('writing a scalar marked with a prepend', function () {
3030
- // instantiate the cache
3031
- const cache = new Cache(config)
3032
-
3033
- const selection = {
3034
- viewer: {
3035
- type: 'User',
3036
- keyRaw: 'viewer',
3037
- fields: {
3038
- id: {
3039
- type: 'ID',
3040
- keyRaw: 'id',
3041
- },
3042
- firstName: {
3043
- type: 'String',
3044
- keyRaw: 'firstName',
3045
- },
3046
- friends: {
3047
- type: 'Int',
3048
- keyRaw: 'friends',
3049
- update: 'prepend',
3050
- },
3051
- },
3052
- },
3053
- } as SubscriptionSelection
3054
-
3055
- // add some data to the cache
3056
- cache.write({
3057
- selection,
3058
- data: {
3059
- viewer: {
3060
- id: '1',
3061
- firstName: 'bob',
3062
- friends: [1],
3063
- },
3064
- },
3065
- })
3066
-
3067
- // make sure we can get the linked lists back
3068
- expect(cache.read({ selection }).data).toEqual({
3069
- viewer: {
3070
- id: '1',
3071
- firstName: 'bob',
3072
- friends: [1],
3073
- },
3074
- })
3075
-
3076
- // add some data to the cache
3077
- cache.write({
3078
- selection,
3079
- data: {
3080
- viewer: {
3081
- id: '1',
3082
- firstName: 'bob',
3083
- friends: [2],
3084
- },
3085
- },
3086
- applyUpdates: true,
3087
- })
3088
-
3089
- // make sure we can get the updated lists back
3090
- expect(cache.read({ selection }).data).toEqual({
3091
- viewer: {
3092
- id: '1',
3093
- firstName: 'bob',
3094
- friends: [2, 1],
3095
- },
3096
- })
3097
- })
3098
-
3099
- test('writing a scalar marked with an append', function () {
3100
- // instantiate the cache
3101
- const cache = new Cache(config)
3102
-
3103
- const selection = {
3104
- viewer: {
3105
- type: 'User',
3106
- keyRaw: 'viewer',
3107
- fields: {
3108
- id: {
3109
- type: 'ID',
3110
- keyRaw: 'id',
3111
- },
3112
- firstName: {
3113
- type: 'String',
3114
- keyRaw: 'firstName',
3115
- },
3116
- friends: {
3117
- type: 'Int',
3118
- keyRaw: 'friends',
3119
- update: 'append',
3120
- },
3121
- },
3122
- },
3123
- } as SubscriptionSelection
3124
-
3125
- // add some data to the cache
3126
- cache.write({
3127
- selection,
3128
- data: {
3129
- viewer: {
3130
- id: '1',
3131
- firstName: 'bob',
3132
- friends: [1],
3133
- },
3134
- },
3135
- })
3136
-
3137
- // make sure we can get the linked lists back
3138
- expect(cache.read({ selection }).data).toEqual({
3139
- viewer: {
3140
- id: '1',
3141
- firstName: 'bob',
3142
- friends: [1],
3143
- },
3144
- })
3145
-
3146
- // add some data to the cache
3147
- cache.write({
3148
- selection,
3149
- data: {
3150
- viewer: {
3151
- id: '1',
3152
- firstName: 'bob',
3153
- friends: [2],
3154
- },
3155
- },
3156
- applyUpdates: true,
3157
- })
3158
-
3159
- // make sure we can get the updated lists back
3160
- expect(cache.read({ selection }).data).toEqual({
3161
- viewer: {
3162
- id: '1',
3163
- firstName: 'bob',
3164
- friends: [1, 2],
3165
- },
3166
- })
3167
- })
3168
-
3169
- test('list operations fail silently', function () {
3170
- // instantiate a cache
3171
- const cache = new Cache(config)
3172
-
3173
- // write some data to a different location with a new user
3174
- // that should be added to the list
3175
- expect(() =>
3176
- cache.write({
3177
- selection: {
3178
- newUser: {
3179
- type: 'User',
3180
- keyRaw: 'newUser',
3181
- operations: [
3182
- {
3183
- action: 'insert',
3184
- list: 'All_Users',
3185
- },
3186
- ],
3187
- fields: {
3188
- id: {
3189
- type: 'ID',
3190
- keyRaw: 'id',
3191
- },
3192
- },
3193
- },
3194
- },
3195
- data: {
3196
- newUser: {
3197
- id: '3',
3198
- },
3199
- },
3200
- })
3201
- ).not.toThrow()
3202
- })
3203
-
3204
- test('when conditions look for all matching lists', function () {
3205
- // instantiate a cache
3206
- const cache = new Cache(config)
3207
-
3208
- const selection: SubscriptionSelection = {
3209
- viewer: {
3210
- type: 'User',
3211
- keyRaw: 'viewer',
3212
- fields: {
3213
- id: {
3214
- type: 'ID',
3215
- keyRaw: 'id',
3216
- },
3217
- friends: {
3218
- type: 'User',
3219
- // the key takes an argument so that we can have multiple
3220
- // lists tracked in the cache
3221
- keyRaw: 'friends(filter: true, foo: $var)',
3222
- list: {
3223
- name: 'All_Users',
3224
- connection: false,
3225
- type: 'User',
3226
- },
3227
- filters: {
3228
- foo: {
3229
- kind: 'Variable',
3230
- value: 'var',
3231
- },
3232
- filter: {
3233
- kind: 'Boolean',
3234
- value: true,
3235
- },
3236
- },
3237
-
3238
- fields: {
3239
- id: {
3240
- type: 'ID',
3241
- keyRaw: 'id',
3242
- },
3243
- firstName: {
3244
- type: 'String',
3245
- keyRaw: 'firstName',
3246
- },
3247
- },
3248
- },
3249
- },
3250
- },
3251
- }
3252
-
3253
- // start off associated with one object
3254
- cache.write({
3255
- selection,
3256
- variables: {
3257
- var: 'hello',
3258
- },
3259
- data: {
3260
- viewer: {
3261
- id: '1',
3262
- friends: [
3263
- {
3264
- id: '2',
3265
- firstName: 'yves',
3266
- },
3267
- ],
3268
- },
3269
- },
3270
- })
3271
-
3272
- // write the same value with a different key
3273
- cache.write({
3274
- selection,
3275
- variables: {
3276
- var: 'world',
3277
- },
3278
- data: {
3279
- viewer: {
3280
- id: '1',
3281
- friends: [
3282
- {
3283
- id: '2',
3284
- firstName: 'yves',
3285
- },
3286
- ],
3287
- },
3288
- },
3289
- })
3290
-
3291
- // a function to spy on that will play the role of set
3292
- const set = vi.fn()
3293
-
3294
- // subscribe to the fields twice
3295
- cache.subscribe(
3296
- {
3297
- rootType: 'Query',
3298
- set,
3299
- selection,
3300
- },
3301
- {
3302
- var: 'world',
3303
- }
3304
- )
3305
- cache.subscribe(
3306
- {
3307
- rootType: 'Query',
3308
- set,
3309
- selection,
3310
- },
3311
- {
3312
- var: 'hello',
3313
- }
3314
- )
3315
-
3316
- // insert an element into the list (no parent ID)
3317
- cache
3318
- .list('All_Users')
3319
- .when({ must: { filter: true } })
3320
- .append(
3321
- {
3322
- id: { type: 'ID', keyRaw: 'id' },
3323
- firstName: { type: 'String', keyRaw: 'firstName' },
3324
- },
3325
- {
3326
- id: '3',
3327
- firstName: 'mathew',
3328
- },
3329
- {
3330
- var: 'hello',
3331
- }
3332
- )
3333
-
3334
- expect(cache.read({ selection, variables: { var: 'world' } }).data).toEqual({
3335
- viewer: {
3336
- friends: [
3337
- {
3338
- firstName: 'yves',
3339
- id: '2',
3340
- },
3341
- {
3342
- firstName: 'mathew',
3343
- id: '3',
3344
- },
3345
- ],
3346
- id: '1',
3347
- },
3348
- })
3349
- })
3350
-
3351
- test('parentID must be passed if there are multiple instances of a list handler', function () {
3352
- // instantiate a cache
3353
- const cache = new Cache(config)
3354
-
3355
- const friendsSelection: SubscriptionSelection = {
3356
- friends: {
3357
- type: 'User',
3358
- keyRaw: 'friends',
3359
- list: {
3360
- name: 'All_Users',
3361
- connection: false,
3362
- type: 'User',
3363
- },
3364
- fields: {
3365
- id: {
3366
- type: 'ID',
3367
- keyRaw: 'id',
3368
- },
3369
- firstName: {
3370
- type: 'String',
3371
- keyRaw: 'firstName',
3372
- },
3373
- },
3374
- },
3375
- }
3376
-
3377
- // create a list we will add to
3378
- cache.write({
3379
- selection: {
3380
- viewer: {
3381
- type: 'User',
3382
- keyRaw: 'viewer',
3383
- fields: {
3384
- id: {
3385
- type: 'ID',
3386
- keyRaw: 'id',
3387
- },
3388
- ...friendsSelection,
3389
- },
3390
- },
3391
- },
3392
- data: {
3393
- viewer: {
3394
- id: '1',
3395
- friends: [
3396
- {
3397
- id: '2',
3398
- firstName: 'Jean',
3399
- },
3400
- ],
3401
- },
3402
- },
3403
- })
3404
-
3405
- // subscribe to the data to register the list
3406
- cache.subscribe(
3407
- {
3408
- rootType: 'User',
3409
- selection: friendsSelection,
3410
- parentID: cache._internal_unstable.id('User', '1')!,
3411
- set: vi.fn(),
3412
- },
3413
- {}
3414
- )
3415
-
3416
- // subscribe to the connection with a different parentID
3417
- cache.subscribe(
3418
- {
3419
- rootType: 'User',
3420
- selection: friendsSelection,
3421
- parentID: cache._internal_unstable.id('User', '2')!,
3422
- set: vi.fn(),
3423
- },
3424
- {}
3425
- )
3426
-
3427
- // append a value to the store
3428
- const writeSelectionNoParentID: SubscriptionSelection = {
3429
- user: {
3430
- type: 'User',
3431
- keyRaw: 'user',
3432
- operations: [
3433
- {
3434
- action: 'insert',
3435
- list: 'All_Users',
3436
- },
3437
- ],
3438
- fields: {
3439
- id: {
3440
- type: 'ID',
3441
- keyRaw: 'id',
3442
- },
3443
- firstName: {
3444
- type: 'String',
3445
- keyRaw: 'firstName',
3446
- },
3447
- },
3448
- },
3449
- }
3450
- const writeSelectionWithParentID: SubscriptionSelection = {
3451
- user: {
3452
- type: 'User',
3453
- keyRaw: 'user',
3454
- operations: [
3455
- {
3456
- action: 'insert',
3457
- list: 'All_Users',
3458
- parentID: {
3459
- kind: 'String',
3460
- value: '1',
3461
- },
3462
- },
3463
- ],
3464
- fields: {
3465
- id: {
3466
- type: 'ID',
3467
- keyRaw: 'id',
3468
- },
3469
- firstName: {
3470
- type: 'String',
3471
- keyRaw: 'firstName',
3472
- },
3473
- },
3474
- },
3475
- }
3476
-
3477
- // write the value without a parent ID
3478
- cache.write({
3479
- selection: writeSelectionNoParentID,
3480
- data: { user: { id: '2', firstName: 'test' } },
3481
- })
3482
- // make sure we didn't modify the lists
3483
- expect([...cache.list('All_Users', '1')]).toHaveLength(1)
3484
- expect([...cache.list('All_Users', '2')]).toHaveLength(0)
3485
-
3486
- // write the value with a parent ID
3487
- cache.write({
3488
- selection: writeSelectionWithParentID,
3489
- data: { user: { id: '2', firstName: 'test' } },
3490
- })
3491
- // make sure we modified the correct list
3492
- expect([...cache.list('All_Users', '1')]).toHaveLength(2)
3493
- expect([...cache.list('All_Users', '2')]).toHaveLength(0)
3494
- })
3495
-
3496
- test('append in abstract list', function () {
3497
- // instantiate a cache
3498
- const cache = new Cache(config)
3499
-
3500
- const selection = {
3501
- viewer: {
3502
- type: 'Node',
3503
- keyRaw: 'viewer',
3504
- fields: {
3505
- id: {
3506
- type: 'ID',
3507
- keyRaw: 'id',
3508
- },
3509
- __typename: {
3510
- type: 'String',
3511
- keyRaw: '__typename',
3512
- },
3513
- friends: {
3514
- type: 'Node',
3515
- keyRaw: 'friends',
3516
- list: {
3517
- name: 'All_Nodes',
3518
- connection: false,
3519
- type: 'Node',
3520
- },
3521
- fields: {
3522
- id: {
3523
- type: 'ID',
3524
- keyRaw: 'id',
3525
- },
3526
- firstName: {
3527
- type: 'String',
3528
- keyRaw: 'firstName',
3529
- },
3530
- __typename: {
3531
- type: 'String',
3532
- keyRaw: '__typename',
3533
- },
3534
- },
3535
- },
3536
- },
3537
- },
3538
- }
3539
-
3540
- // start off associated with one object
3541
- cache.write({
3542
- selection,
3543
- data: {
3544
- viewer: {
3545
- id: '1',
3546
- __typename: 'User',
3547
- friends: [
3548
- {
3549
- id: '2',
3550
- firstName: 'jane',
3551
- __typename: 'User',
3552
- },
3553
- ],
3554
- },
3555
- },
3556
- })
3557
-
3558
- // a function to spy on that will play the role of set
3559
- const set = vi.fn()
3560
-
3561
- // subscribe to the fields
3562
- cache.subscribe({
3563
- rootType: 'Query',
3564
- set,
3565
- selection,
3566
- })
3567
-
3568
- // insert an element into the list (no parent ID)
3569
- cache.list('All_Nodes').append(
3570
- { id: { type: 'ID', keyRaw: 'id' }, firstName: { type: 'String', keyRaw: 'firstName' } },
3571
- {
3572
- id: '3',
3573
- firstName: 'mary',
3574
- __typename: 'User',
3575
- }
3576
- )
3577
-
3578
- // make sure we got the new value
3579
- expect(set).toHaveBeenCalledWith({
3580
- viewer: {
3581
- id: '1',
3582
- __typename: 'User',
3583
- friends: [
3584
- {
3585
- firstName: 'jane',
3586
- id: '2',
3587
- __typename: 'User',
3588
- },
3589
- {
3590
- firstName: 'mary',
3591
- id: '3',
3592
- __typename: 'User',
3593
- },
3594
- ],
3595
- },
3596
- })
3597
- })
3598
-
3599
- test('list operations on interface fields without a well defined parent update the correct values in cache', function () {
3600
- // they have to use __typename to compute the parentID because the list type is Node but the cached value is User:OOOOO// instantiate a cache
3601
- const cache = new Cache(config)
3602
-
3603
- const selection = {
3604
- viewer: {
3605
- type: 'Node',
3606
- keyRaw: 'viewer',
3607
- fields: {
3608
- id: {
3609
- type: 'ID',
3610
- keyRaw: 'id',
3611
- },
3612
- __typename: {
3613
- type: 'String',
3614
- keyRaw: '__typename',
3615
- },
3616
- friends: {
3617
- type: 'Node',
3618
- keyRaw: 'friends',
3619
- abstract: true,
3620
- fields: {
3621
- id: {
3622
- type: 'ID',
3623
- keyRaw: 'id',
3624
- },
3625
- __typename: {
3626
- type: 'String',
3627
- keyRaw: '__typename',
3628
- },
3629
- notFriends: {
3630
- type: 'Node',
3631
- keyRaw: 'notFriends',
3632
- abstract: true,
3633
- list: {
3634
- name: 'Not_Friends',
3635
- connection: false,
3636
- type: 'Node',
3637
- },
3638
- fields: {
3639
- id: {
3640
- type: 'ID',
3641
- keyRaw: 'id',
3642
- },
3643
- firstName: {
3644
- type: 'String',
3645
- keyRaw: 'firstName',
3646
- },
3647
- __typename: {
3648
- type: 'String',
3649
- keyRaw: '__typename',
3650
- },
3651
- },
3652
- },
3653
- },
3654
- },
3655
- },
3656
- },
3657
- }
3658
-
3659
- // start off associated with one object
3660
- cache.write({
3661
- selection,
3662
- data: {
3663
- viewer: {
3664
- id: '1',
3665
- __typename: 'User',
3666
- friends: [
3667
- {
3668
- id: '2',
3669
- __typename: 'User',
3670
- notFriends: [
3671
- {
3672
- id: '3',
3673
- firstName: 'jane',
3674
- __typename: 'User',
3675
- },
3676
- ],
3677
- },
3678
- {
3679
- id: '3',
3680
- __typename: 'User',
3681
- notFriends: [
3682
- {
3683
- id: '4',
3684
- firstName: 'jane',
3685
- __typename: 'User',
3686
- },
3687
- ],
3688
- },
3689
- ],
3690
- },
3691
- },
3692
- })
3693
-
3694
- // a function to call
3695
- const set = vi.fn()
3696
-
3697
- // subscribe to the fields (create the list handler)
3698
- cache.subscribe({
3699
- rootType: 'Query',
3700
- set,
3701
- selection,
3702
- })
3703
-
3704
- // insert into the not friends list for user 3
3705
- cache.list('Not_Friends', '3').append(
3706
- {
3707
- id: {
3708
- type: 'ID',
3709
- keyRaw: 'id',
3710
- },
3711
- firstName: {
3712
- type: 'String',
3713
- keyRaw: 'firstName',
3714
- },
3715
- __typename: {
3716
- type: 'String',
3717
- keyRaw: '__typename',
3718
- },
3719
- },
3720
- {
3721
- id: '5',
3722
- firstName: 'Billy',
3723
- __typename: 'User',
3724
- }
3725
- )
3726
-
3727
- expect(set).toHaveBeenCalledWith({
3728
- viewer: {
3729
- id: '1',
3730
- __typename: 'User',
3731
- friends: [
3732
- {
3733
- id: '2',
3734
- __typename: 'User',
3735
- notFriends: [
3736
- {
3737
- id: '3',
3738
- firstName: 'jane',
3739
- __typename: 'User',
3740
- },
3741
- ],
3742
- },
3743
- {
3744
- id: '3',
3745
- __typename: 'User',
3746
- notFriends: [
3747
- {
3748
- id: '4',
3749
- firstName: 'jane',
3750
- __typename: 'User',
3751
- },
3752
- {
3753
- id: '5',
3754
- firstName: 'Billy',
3755
- __typename: 'User',
3756
- },
3757
- ],
3758
- },
3759
- ],
3760
- },
3761
- })
3762
- })
3763
-
3764
- test("parentID ignores single lists that don't match", function () {
3765
- // instantiate a cache
3766
- const cache = new Cache(config)
3767
-
3768
- // create a list we will add to
3769
- cache.write({
3770
- selection: {
3771
- viewer: {
3772
- type: 'User',
3773
- keyRaw: 'viewer',
3774
- fields: {
3775
- id: {
3776
- type: 'ID',
3777
- keyRaw: 'id',
3778
- },
3779
- },
3780
- },
3781
- },
3782
- data: {
3783
- viewer: {
3784
- id: '1',
3785
- },
3786
- },
3787
- })
3788
-
3789
- // subscribe to the data to register the list
3790
- cache.subscribe(
3791
- {
3792
- rootType: 'User',
3793
- selection: {
3794
- friends: {
3795
- type: 'User',
3796
- keyRaw: 'friends',
3797
- list: {
3798
- name: 'All_Users',
3799
- connection: false,
3800
- type: 'User',
3801
- },
3802
- fields: {
3803
- id: {
3804
- type: 'ID',
3805
- keyRaw: 'id',
3806
- },
3807
- firstName: {
3808
- type: 'String',
3809
- keyRaw: 'firstName',
3810
- },
3811
- },
3812
- },
3813
- },
3814
- parentID: cache._internal_unstable.id('User', '1')!,
3815
- set: vi.fn(),
3816
- },
3817
- {}
3818
- )
3819
-
3820
- // write some data to a different location with a new user
3821
- // that should be added to the list
3822
- cache.write({
3823
- selection: {
3824
- newUser: {
3825
- type: 'User',
3826
- keyRaw: 'newUser',
3827
- operations: [
3828
- {
3829
- action: 'insert',
3830
- list: 'All_Users',
3831
- parentID: {
3832
- kind: 'String',
3833
- value: '2',
3834
- },
3835
- },
3836
- ],
3837
- fields: {
3838
- id: {
3839
- type: 'ID',
3840
- keyRaw: 'id',
3841
- },
3842
- },
3843
- },
3844
- },
3845
- data: {
3846
- newUser: {
3847
- id: '3',
3848
- },
3849
- },
3850
- })
3851
-
3852
- // make sure we just added to the list
3853
- expect([...cache.list('All_Users', '1')]).toHaveLength(0)
3854
- })