houdini 0.17.13 → 0.18.0

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