@khanacademy/wonder-blocks-data 13.0.11 → 14.0.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 (66) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/package.json +5 -5
  3. package/src/components/__tests__/data.test.tsx +0 -832
  4. package/src/components/__tests__/gql-router.test.tsx +0 -63
  5. package/src/components/__tests__/intercept-requests.test.tsx +0 -57
  6. package/src/components/__tests__/track-data.test.tsx +0 -56
  7. package/src/components/data.ts +0 -73
  8. package/src/components/gql-router.tsx +0 -63
  9. package/src/components/intercept-context.ts +0 -19
  10. package/src/components/intercept-requests.tsx +0 -67
  11. package/src/components/track-data.tsx +0 -28
  12. package/src/hooks/__tests__/__snapshots__/use-shared-cache.test.ts.snap +0 -17
  13. package/src/hooks/__tests__/use-cached-effect.test.tsx +0 -789
  14. package/src/hooks/__tests__/use-gql-router-context.test.tsx +0 -132
  15. package/src/hooks/__tests__/use-gql.test.tsx +0 -204
  16. package/src/hooks/__tests__/use-hydratable-effect.test.ts +0 -708
  17. package/src/hooks/__tests__/use-request-interception.test.tsx +0 -254
  18. package/src/hooks/__tests__/use-server-effect.test.ts +0 -293
  19. package/src/hooks/__tests__/use-shared-cache.test.ts +0 -263
  20. package/src/hooks/use-cached-effect.ts +0 -297
  21. package/src/hooks/use-gql-router-context.ts +0 -49
  22. package/src/hooks/use-gql.ts +0 -58
  23. package/src/hooks/use-hydratable-effect.ts +0 -201
  24. package/src/hooks/use-request-interception.ts +0 -53
  25. package/src/hooks/use-server-effect.ts +0 -75
  26. package/src/hooks/use-shared-cache.ts +0 -107
  27. package/src/index.ts +0 -46
  28. package/src/util/__tests__/__snapshots__/scoped-in-memory-cache.test.ts.snap +0 -19
  29. package/src/util/__tests__/__snapshots__/serializable-in-memory-cache.test.ts.snap +0 -19
  30. package/src/util/__tests__/get-gql-data-from-response.test.ts +0 -186
  31. package/src/util/__tests__/get-gql-request-id.test.ts +0 -132
  32. package/src/util/__tests__/graphql-document-node-parser.test.ts +0 -535
  33. package/src/util/__tests__/hydration-cache-api.test.ts +0 -34
  34. package/src/util/__tests__/merge-gql-context.test.ts +0 -73
  35. package/src/util/__tests__/purge-caches.test.ts +0 -28
  36. package/src/util/__tests__/request-api.test.ts +0 -176
  37. package/src/util/__tests__/request-fulfillment.test.ts +0 -146
  38. package/src/util/__tests__/request-tracking.test.tsx +0 -321
  39. package/src/util/__tests__/result-from-cache-response.test.ts +0 -79
  40. package/src/util/__tests__/scoped-in-memory-cache.test.ts +0 -316
  41. package/src/util/__tests__/serializable-in-memory-cache.test.ts +0 -397
  42. package/src/util/__tests__/ssr-cache.test.ts +0 -636
  43. package/src/util/__tests__/to-gql-operation.test.ts +0 -41
  44. package/src/util/data-error.ts +0 -63
  45. package/src/util/get-gql-data-from-response.ts +0 -65
  46. package/src/util/get-gql-request-id.ts +0 -106
  47. package/src/util/gql-error.ts +0 -43
  48. package/src/util/gql-router-context.ts +0 -9
  49. package/src/util/gql-types.ts +0 -64
  50. package/src/util/graphql-document-node-parser.ts +0 -132
  51. package/src/util/graphql-types.ts +0 -28
  52. package/src/util/hydration-cache-api.ts +0 -30
  53. package/src/util/merge-gql-context.ts +0 -35
  54. package/src/util/purge-caches.ts +0 -14
  55. package/src/util/request-api.ts +0 -65
  56. package/src/util/request-fulfillment.ts +0 -121
  57. package/src/util/request-tracking.ts +0 -211
  58. package/src/util/result-from-cache-response.ts +0 -30
  59. package/src/util/scoped-in-memory-cache.ts +0 -121
  60. package/src/util/serializable-in-memory-cache.ts +0 -44
  61. package/src/util/ssr-cache.ts +0 -193
  62. package/src/util/status.ts +0 -35
  63. package/src/util/to-gql-operation.ts +0 -43
  64. package/src/util/types.ts +0 -145
  65. package/tsconfig-build.json +0 -12
  66. package/tsconfig-build.tsbuildinfo +0 -1
@@ -1,535 +0,0 @@
1
- import {jest as wsJest} from "@khanacademy/wonder-stuff-testing";
2
- import {graphQLDocumentNodeParser} from "../graphql-document-node-parser";
3
-
4
- describe("#graphQLDocumentNodeParser", () => {
5
- describe("in production - shorter error messages", () => {
6
- beforeEach(() => {
7
- process.env.NODE_ENV = "production";
8
- });
9
-
10
- wsJest.afterEachRestoreEnv("NODE_ENV");
11
-
12
- it("should throw if the document lacks the kind property", () => {
13
- // Arrange
14
- const documentNode: any = {};
15
-
16
- // Act
17
- const underTest = () => graphQLDocumentNodeParser(documentNode);
18
-
19
- // Assert
20
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
21
- `"Bad DocumentNode"`,
22
- );
23
- });
24
-
25
- it("should throw if the document contains only fragments", () => {
26
- // Arrange
27
- const documentNode = {
28
- kind: "Document",
29
- definitions: [
30
- {
31
- kind: "FragmentDefinition",
32
- name: {
33
- kind: "Name",
34
- value: "fragment",
35
- },
36
- typeCondition: {
37
- kind: "NamedType",
38
- name: {
39
- kind: "Name",
40
- value: "Query",
41
- },
42
- },
43
- selectionSet: {
44
- kind: "SelectionSet",
45
- selections: [
46
- {
47
- kind: "Field",
48
- name: {
49
- kind: "Name",
50
- value: "test",
51
- },
52
- },
53
- ],
54
- },
55
- },
56
- ],
57
- } as const;
58
-
59
- // Act
60
- const underTest = () => graphQLDocumentNodeParser(documentNode);
61
-
62
- // Assert
63
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
64
- `"Fragment only"`,
65
- );
66
- });
67
-
68
- it("should throw if the document contains subscriptions", () => {
69
- // Arrange
70
- const documentNode = {
71
- kind: "Document",
72
- definitions: [
73
- {
74
- kind: "OperationDefinition",
75
- operation: "subscription",
76
- variableDefinitions: [],
77
- name: {
78
- kind: "Name",
79
- value: "subscription",
80
- },
81
- },
82
- ],
83
- } as const;
84
-
85
- // Act
86
- const underTest = () => graphQLDocumentNodeParser(documentNode);
87
-
88
- // Assert
89
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
90
- `"No subscriptions"`,
91
- );
92
- });
93
-
94
- it("should throw if the document contains more than one query", () => {
95
- // Arrange
96
- const documentNode = {
97
- kind: "Document",
98
- definitions: [
99
- {
100
- kind: "OperationDefinition",
101
- operation: "query",
102
- variableDefinitions: [],
103
- name: {
104
- kind: "Name",
105
- value: "query",
106
- },
107
- },
108
- {
109
- kind: "OperationDefinition",
110
- operation: "query",
111
- variableDefinitions: [],
112
- name: {
113
- kind: "Name",
114
- value: "query",
115
- },
116
- },
117
- ],
118
- } as const;
119
-
120
- // Act
121
- const underTest = () => graphQLDocumentNodeParser(documentNode);
122
-
123
- // Assert
124
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
125
- `"Too many ops"`,
126
- );
127
- });
128
-
129
- it("should throw if the document contains more than one mutation", () => {
130
- // Arrange
131
- const documentNode = {
132
- kind: "Document",
133
- definitions: [
134
- {
135
- kind: "OperationDefinition",
136
- operation: "mutation",
137
- variableDefinitions: [],
138
- name: {
139
- kind: "Name",
140
- value: "mutation",
141
- },
142
- },
143
- {
144
- kind: "OperationDefinition",
145
- operation: "mutation",
146
- variableDefinitions: [],
147
- name: {
148
- kind: "Name",
149
- value: "mutation",
150
- },
151
- },
152
- ],
153
- } as const;
154
-
155
- // Act
156
- const underTest = () => graphQLDocumentNodeParser(documentNode);
157
-
158
- // Assert
159
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
160
- `"Too many ops"`,
161
- );
162
- });
163
-
164
- it("should throw if the document is a combination of query and mutation", () => {
165
- // Arrange
166
- const documentNode = {
167
- kind: "Document",
168
- definitions: [
169
- {
170
- kind: "OperationDefinition",
171
- operation: "query",
172
- variableDefinitions: [],
173
- name: {
174
- kind: "Name",
175
- value: "query",
176
- },
177
- },
178
- {
179
- kind: "OperationDefinition",
180
- operation: "mutation",
181
- variableDefinitions: [],
182
- name: {
183
- kind: "Name",
184
- value: "mutation",
185
- },
186
- },
187
- ],
188
- } as const;
189
-
190
- // Act
191
- const underTest = () => graphQLDocumentNodeParser(documentNode);
192
-
193
- // Assert
194
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
195
- `"Too many ops"`,
196
- );
197
- });
198
- });
199
-
200
- describe("not in production - more informative error messages", () => {
201
- it("should throw if the document lacks the kind property", () => {
202
- // Arrange
203
- const documentNode = {} as any;
204
-
205
- // Act
206
- const underTest = () => graphQLDocumentNodeParser(documentNode);
207
-
208
- // Assert
209
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
210
- `"Argument of {} passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another method to convert your operation into a document"`,
211
- );
212
- });
213
-
214
- it("should throw if the document contains only fragments", () => {
215
- // Arrange
216
- const documentNode = {
217
- kind: "Document",
218
- definitions: [
219
- {
220
- kind: "FragmentDefinition",
221
- name: {
222
- kind: "Name",
223
- value: "fragment",
224
- },
225
- typeCondition: {
226
- kind: "NamedType",
227
- name: {
228
- kind: "Name",
229
- value: "Query",
230
- },
231
- },
232
- selectionSet: {
233
- kind: "SelectionSet",
234
- selections: [
235
- {
236
- kind: "Field",
237
- name: {
238
- kind: "Name",
239
- value: "test",
240
- },
241
- },
242
- ],
243
- },
244
- },
245
- ],
246
- } as const;
247
-
248
- // Act
249
- const underTest = () => graphQLDocumentNodeParser(documentNode);
250
-
251
- // Assert
252
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
253
- `"Passing only a fragment to 'graphql' is not supported. You must include a query or mutation as well"`,
254
- );
255
- });
256
-
257
- it("should throw if the document contains subscriptions", () => {
258
- // Arrange
259
- const documentNode = {
260
- kind: "Document",
261
- definitions: [
262
- {
263
- kind: "OperationDefinition",
264
- operation: "subscription",
265
- variableDefinitions: [],
266
- name: {
267
- kind: "Name",
268
- value: "subscription",
269
- },
270
- },
271
- ],
272
- } as const;
273
-
274
- // Act
275
- const underTest = () => graphQLDocumentNodeParser(documentNode);
276
-
277
- // Assert
278
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
279
- `"We do not support subscriptions. {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"subscription","variableDefinitions":[],"name":{"kind":"Name","value":"subscription"}}]} had 1 subscriptions"`,
280
- );
281
- });
282
-
283
- it("should throw if the document contains more than one query", () => {
284
- // Arrange
285
- const documentNode = {
286
- kind: "Document",
287
- definitions: [
288
- {
289
- kind: "OperationDefinition",
290
- operation: "query",
291
- variableDefinitions: [],
292
- name: {
293
- kind: "Name",
294
- value: "query",
295
- },
296
- },
297
- {
298
- kind: "OperationDefinition",
299
- operation: "query",
300
- variableDefinitions: [],
301
- name: {
302
- kind: "Name",
303
- value: "query",
304
- },
305
- },
306
- ],
307
- } as const;
308
-
309
- // Act
310
- const underTest = () => graphQLDocumentNodeParser(documentNode);
311
-
312
- // Assert
313
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
314
- `"We only support one query or mutation per component. {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","variableDefinitions":[],"name":{"kind":"Name","value":"query"}},{"kind":"OperationDefinition","operation":"query","variableDefinitions":[],"name":{"kind":"Name","value":"query"}}]} had 2 queries and 0 mutations. "`,
315
- );
316
- });
317
-
318
- it("should throw if the document contains more than one mutation", () => {
319
- // Arrange
320
- const documentNode = {
321
- kind: "Document",
322
- definitions: [
323
- {
324
- kind: "OperationDefinition",
325
- operation: "mutation",
326
- variableDefinitions: [],
327
- name: {
328
- kind: "Name",
329
- value: "mutation",
330
- },
331
- },
332
- {
333
- kind: "OperationDefinition",
334
- operation: "mutation",
335
- variableDefinitions: [],
336
- name: {
337
- kind: "Name",
338
- value: "mutation",
339
- },
340
- },
341
- ],
342
- } as const;
343
-
344
- // Act
345
- const underTest = () => graphQLDocumentNodeParser(documentNode);
346
-
347
- // Assert
348
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
349
- `"We only support one query or mutation per component. {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","variableDefinitions":[],"name":{"kind":"Name","value":"mutation"}},{"kind":"OperationDefinition","operation":"mutation","variableDefinitions":[],"name":{"kind":"Name","value":"mutation"}}]} had 0 queries and 2 mutations. "`,
350
- );
351
- });
352
-
353
- it("should throw if the document is a combination of query and mutation", () => {
354
- // Arrange
355
- const documentNode = {
356
- kind: "Document",
357
- definitions: [
358
- {
359
- kind: "OperationDefinition",
360
- operation: "query",
361
- variableDefinitions: [],
362
- name: {
363
- kind: "Name",
364
- value: "query",
365
- },
366
- },
367
- {
368
- kind: "OperationDefinition",
369
- operation: "mutation",
370
- variableDefinitions: [],
371
- name: {
372
- kind: "Name",
373
- value: "mutation",
374
- },
375
- },
376
- ],
377
- } as const;
378
-
379
- // Act
380
- const underTest = () => graphQLDocumentNodeParser(documentNode);
381
-
382
- // Assert
383
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
384
- `"We only support one query or mutation per component. {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","variableDefinitions":[],"name":{"kind":"Name","value":"query"}},{"kind":"OperationDefinition","operation":"mutation","variableDefinitions":[],"name":{"kind":"Name","value":"mutation"}}]} had 1 queries and 1 mutations. "`,
385
- );
386
- });
387
- });
388
-
389
- it("should return the operation name, type, and variables for a query", () => {
390
- // Arrange
391
- const documentNode: any = {
392
- kind: "Document",
393
- definitions: [
394
- {
395
- kind: "OperationDefinition",
396
- operation: "query",
397
- variableDefinitions: [
398
- {
399
- kind: "VariableDefinition",
400
- variable: {
401
- kind: "Variable",
402
- name: {
403
- kind: "Name",
404
- value: "variable",
405
- },
406
- },
407
- type: {
408
- kind: "NamedType",
409
- name: {
410
- kind: "Name",
411
- value: "String",
412
- },
413
- },
414
- defaultValue: {
415
- kind: "StringValue",
416
- value: "defaultValue",
417
- },
418
- },
419
- ],
420
- name: {
421
- kind: "Name",
422
- value: "myQuery",
423
- },
424
- },
425
- ],
426
- };
427
-
428
- // Act
429
- const result = graphQLDocumentNodeParser(documentNode);
430
-
431
- // Assert
432
- expect(result).toEqual({
433
- name: "myQuery",
434
- type: "query",
435
- variables: documentNode.definitions[0].variableDefinitions,
436
- });
437
- });
438
-
439
- it("should return the operation name, type, and variables for a mutation", () => {
440
- // Arrange
441
- const documentNode: any = {
442
- kind: "Document",
443
- definitions: [
444
- {
445
- kind: "OperationDefinition",
446
- operation: "mutation",
447
- variableDefinitions: [
448
- {
449
- kind: "VariableDefinition",
450
- variable: {
451
- kind: "Variable",
452
- name: {
453
- kind: "Name",
454
- value: "variable",
455
- },
456
- },
457
- type: {
458
- kind: "NamedType",
459
- name: {
460
- kind: "Name",
461
- value: "String",
462
- },
463
- },
464
- defaultValue: {
465
- kind: "StringValue",
466
- value: "defaultValue",
467
- },
468
- },
469
- ],
470
- name: {
471
- kind: "Name",
472
- value: "myMutation",
473
- },
474
- },
475
- ],
476
- };
477
-
478
- // Act
479
- const result = graphQLDocumentNodeParser(documentNode);
480
-
481
- // Assert
482
- expect(result).toEqual({
483
- name: "myMutation",
484
- type: "mutation",
485
- variables: documentNode.definitions[0].variableDefinitions,
486
- });
487
- });
488
-
489
- it("should use name of data if no name in document", () => {
490
- // Arrange
491
- const documentNode: any = {
492
- kind: "Document",
493
- definitions: [
494
- {
495
- kind: "OperationDefinition",
496
- operation: "query",
497
- },
498
- ],
499
- };
500
-
501
- // Act
502
- const result = graphQLDocumentNodeParser(documentNode);
503
-
504
- // Assert
505
- expect(result).toEqual({
506
- name: "data",
507
- type: "query",
508
- variables: [],
509
- });
510
- });
511
-
512
- it("should cache the output", () => {
513
- // Arrange
514
- const documentNode: any = {
515
- kind: "Document",
516
- definitions: [
517
- {
518
- kind: "OperationDefinition",
519
- operation: "query",
520
- name: {
521
- kind: "Name",
522
- value: "myQuery",
523
- },
524
- },
525
- ],
526
- };
527
-
528
- // Act
529
- const initialResult = graphQLDocumentNodeParser(documentNode);
530
- const secondResult = graphQLDocumentNodeParser(documentNode);
531
-
532
- // Assert
533
- expect(initialResult).toBe(secondResult);
534
- });
535
- });
@@ -1,34 +0,0 @@
1
- import {SsrCache} from "../ssr-cache";
2
-
3
- import {
4
- initializeHydrationCache,
5
- purgeHydrationCache,
6
- } from "../hydration-cache-api";
7
-
8
- describe("#initializeHydrationCache", () => {
9
- it("should call SsrCache.Default.initialize", () => {
10
- // Arrange
11
- const sourceCache: Record<string, any> = {};
12
- const initSpy = jest.spyOn(SsrCache.Default, "initialize");
13
-
14
- // Act
15
- initializeHydrationCache(sourceCache);
16
-
17
- // Assert
18
- expect(initSpy).toHaveBeenCalledWith(sourceCache);
19
- });
20
- });
21
-
22
- describe("#purgeHydrationCache", () => {
23
- it("should call SsrCache.Default.purgeData", () => {
24
- // Arrange
25
- const predicate = jest.fn();
26
- const purgeDataSpy = jest.spyOn(SsrCache.Default, "purgeData");
27
-
28
- // Act
29
- purgeHydrationCache(predicate);
30
-
31
- // Assert
32
- expect(purgeDataSpy).toHaveBeenCalledWith(predicate);
33
- });
34
- });
@@ -1,73 +0,0 @@
1
- import {mergeGqlContext} from "../merge-gql-context";
2
-
3
- describe("#mergeGqlContext", () => {
4
- it("should combine the default context with the given overrides", () => {
5
- // Arrange
6
- const baseContext = {
7
- foo: "bar",
8
- } as const;
9
-
10
- // Act
11
- const result = mergeGqlContext<any>(baseContext, {
12
- fiz: "baz",
13
- });
14
-
15
- // Assert
16
- expect(result).toStrictEqual({
17
- foo: "bar",
18
- fiz: "baz",
19
- });
20
- });
21
-
22
- it("should overwrite values in the default context with the given overrides", () => {
23
- // Arrange
24
- const baseContext = {
25
- foo: "bar",
26
- } as const;
27
-
28
- // Act
29
- const result = mergeGqlContext<any>(baseContext, {
30
- foo: "boo",
31
- });
32
-
33
- // Assert
34
- expect(result).toStrictEqual({
35
- foo: "boo",
36
- });
37
- });
38
-
39
- it("should not overwrite values in the default context with undefined values in the given overrides", () => {
40
- // Arrange
41
- const baseContext = {
42
- foo: "bar",
43
- } as const;
44
-
45
- // Act
46
- const result = mergeGqlContext<any>(baseContext, {
47
- foo: undefined,
48
- });
49
-
50
- // Assert
51
- expect(result).toStrictEqual({
52
- foo: "bar",
53
- });
54
- });
55
-
56
- it("should delete values in the default context when the value is null in the given overrides", () => {
57
- // Arrange
58
- const baseContext = {
59
- foo: "bar",
60
- fiz: "baz",
61
- } as const;
62
-
63
- // Act
64
- const result = mergeGqlContext<any>(baseContext, {
65
- fiz: null,
66
- });
67
-
68
- // Assert
69
- expect(result).toStrictEqual({
70
- foo: "bar",
71
- });
72
- });
73
- });
@@ -1,28 +0,0 @@
1
- import {SharedCache} from "../../hooks/use-shared-cache";
2
- import * as HydrationCacheApi from "../hydration-cache-api";
3
-
4
- import {purgeCaches} from "../purge-caches";
5
-
6
- describe("#purgeCaches", () => {
7
- it("should purge the shared cache", () => {
8
- // Arrange
9
- const spy = jest.spyOn(SharedCache, "purgeAll");
10
-
11
- // Act
12
- purgeCaches();
13
-
14
- // Assert
15
- expect(spy).toHaveBeenCalled();
16
- });
17
-
18
- it("should purge the hydration cache", () => {
19
- // Arrange
20
- const spy = jest.spyOn(HydrationCacheApi, "purgeHydrationCache");
21
-
22
- // Act
23
- purgeCaches();
24
-
25
- // Assert
26
- expect(spy).toHaveBeenCalled();
27
- });
28
- });