1
1
import { gql } from "graphql-tag" ;
2
2
3
3
import { InMemoryCache } from "@apollo/client/cache" ;
4
+ import type { TypedDocumentNode } from "@apollo/client/core" ;
4
5
import { ApolloClient } from "@apollo/client/core" ;
5
6
import {
6
7
CombinedGraphQLErrors ,
@@ -19,37 +20,21 @@ describe("GraphQL Subscriptions", () => {
19
20
"Vyacheslav Kim" ,
20
21
"Changping Chen" ,
21
22
"Amanda Liu" ,
22
- ] . map ( ( name ) => ( { result : { data : { user : { name } } } , delay : 10 } ) ) ;
23
-
24
- let options : any ;
25
- let defaultOptions : any ;
26
- beforeEach ( ( ) => {
27
- options = {
28
- query : gql `
29
- subscription UserInfo($name: String) {
30
- user(name: $name) {
31
- name
32
- }
33
- }
34
- ` ,
35
- variables : {
36
- name : "Changping Chen" ,
37
- } ,
38
- context : {
39
- someVar : "Some value" ,
40
- } ,
41
- } ;
42
-
43
- defaultOptions = {
44
- query : gql `
45
- subscription UserInfo($name: String = "Changping Chen") {
46
- user(name: $name) {
47
- name
48
- }
49
- }
50
- ` ,
51
- } ;
52
- } ) ;
23
+ ] . map ( ( name ) => ( {
24
+ result : { data : { user : { __typename : "User" as const , name } } } ,
25
+ delay : 10 ,
26
+ } ) ) ;
27
+
28
+ const subscription : TypedDocumentNode <
29
+ { user : { __typename : "User" ; name : string } } ,
30
+ { name ?: string }
31
+ > = gql `
32
+ subscription UserInfo($name: String) {
33
+ user(name: $name) {
34
+ name
35
+ }
36
+ }
37
+ ` ;
53
38
54
39
it ( "should start a subscription on network interface and unsubscribe" , async ( ) => {
55
40
const link = new MockSubscriptionLink ( ) ;
@@ -59,7 +44,17 @@ describe("GraphQL Subscriptions", () => {
59
44
cache : new InMemoryCache ( ) ,
60
45
} ) ;
61
46
62
- const stream = new ObservableStream ( client . subscribe ( defaultOptions ) ) ;
47
+ const stream = new ObservableStream (
48
+ client . subscribe ( {
49
+ query : gql `
50
+ subscription UserInfo($name: String = "Changping Chen") {
51
+ user(name: $name) {
52
+ name
53
+ }
54
+ }
55
+ ` ,
56
+ } )
57
+ ) ;
63
58
link . simulateResult ( results [ 0 ] ) ;
64
59
65
60
await expect ( stream ) . toEmitTypedValue ( results [ 0 ] . result ) ;
@@ -75,7 +70,12 @@ describe("GraphQL Subscriptions", () => {
75
70
cache : new InMemoryCache ( ) ,
76
71
} ) ;
77
72
78
- const stream = new ObservableStream ( client . subscribe ( options ) ) ;
73
+ const stream = new ObservableStream (
74
+ client . subscribe ( {
75
+ query : subscription ,
76
+ variables : { name : "Changping Chen" } ,
77
+ } )
78
+ ) ;
79
79
80
80
link . simulateResult ( results [ 0 ] ) ;
81
81
@@ -91,7 +91,10 @@ describe("GraphQL Subscriptions", () => {
91
91
cache : new InMemoryCache ( ) ,
92
92
} ) ;
93
93
94
- const obs = client . subscribe ( options ) ;
94
+ const obs = client . subscribe ( {
95
+ query : subscription ,
96
+ variables : { name : "Changping Chen" } ,
97
+ } ) ;
95
98
const stream1 = new ObservableStream ( obs ) ;
96
99
const stream2 = new ObservableStream ( obs ) ;
97
100
@@ -108,7 +111,12 @@ describe("GraphQL Subscriptions", () => {
108
111
cache : new InMemoryCache ( ) ,
109
112
} ) ;
110
113
111
- const stream = new ObservableStream ( client . subscribe ( options ) ) ;
114
+ const stream = new ObservableStream (
115
+ client . subscribe ( {
116
+ query : subscription ,
117
+ variables : { name : "Changping Chen" } ,
118
+ } )
119
+ ) ;
112
120
113
121
for ( let i = 0 ; i < 4 ; i ++ ) {
114
122
link . simulateResult ( results [ i ] ) ;
@@ -131,8 +139,13 @@ describe("GraphQL Subscriptions", () => {
131
139
132
140
expect ( cache . extract ( ) ) . toEqual ( { } ) ;
133
141
134
- options . fetchPolicy = "no-cache" ;
135
- const stream = new ObservableStream ( client . subscribe ( options ) ) ;
142
+ const stream = new ObservableStream (
143
+ client . subscribe ( {
144
+ query : subscription ,
145
+ fetchPolicy : "no-cache" ,
146
+ variables : { name : "Changping Chen" } ,
147
+ } )
148
+ ) ;
136
149
137
150
link . simulateResult ( results [ 0 ] ) ;
138
151
@@ -147,7 +160,10 @@ describe("GraphQL Subscriptions", () => {
147
160
cache : new InMemoryCache ( ) ,
148
161
} ) ;
149
162
150
- const obs = client . subscribe ( options ) ;
163
+ const obs = client . subscribe ( {
164
+ query : subscription ,
165
+ variables : { name : "Changping Chen" } ,
166
+ } ) ;
151
167
const stream = new ObservableStream ( obs ) ;
152
168
153
169
link . simulateResult ( {
@@ -195,7 +211,10 @@ describe("GraphQL Subscriptions", () => {
195
211
cache : new InMemoryCache ( ) ,
196
212
} ) ;
197
213
198
- const obs = client . subscribe ( options ) ;
214
+ const obs = client . subscribe ( {
215
+ query : subscription ,
216
+ variables : { name : "Changping Chen" } ,
217
+ } ) ;
199
218
const stream = new ObservableStream ( obs ) ;
200
219
201
220
link . simulateResult ( {
@@ -247,7 +266,10 @@ describe("GraphQL Subscriptions", () => {
247
266
cache : new InMemoryCache ( ) ,
248
267
} ) ;
249
268
250
- const obs = client . subscribe ( options ) ;
269
+ const obs = client . subscribe ( {
270
+ query : subscription ,
271
+ variables : { name : "Changping Chen" } ,
272
+ } ) ;
251
273
const stream = new ObservableStream ( obs ) ;
252
274
253
275
link . simulateResult ( { error : new Error ( "Oops" ) } ) ;
@@ -309,7 +331,11 @@ describe("GraphQL Subscriptions", () => {
309
331
cache : new InMemoryCache ( ) ,
310
332
} ) ;
311
333
312
- const obs = client . subscribe ( { ...options , errorPolicy : "all" } ) ;
334
+ const obs = client . subscribe ( {
335
+ query : subscription ,
336
+ errorPolicy : "all" ,
337
+ variables : { name : "Changping Chen" } ,
338
+ } ) ;
313
339
const stream = new ObservableStream ( obs ) ;
314
340
315
341
link . simulateResult ( { error : new Error ( "Oops" ) } ) ;
@@ -325,27 +351,22 @@ describe("GraphQL Subscriptions", () => {
325
351
const { httpLink, enqueueProtocolErrors } =
326
352
mockMultipartSubscriptionStream ( ) ;
327
353
328
- const query = gql `
329
- subscription UserInfo($name: String) {
330
- user(name: $name) {
331
- name
332
- }
333
- }
334
- ` ;
335
354
const client = new ApolloClient ( {
336
355
link : httpLink ,
337
356
cache : new InMemoryCache ( ) ,
338
357
} ) ;
339
358
340
359
const obs = client . subscribe ( {
341
- query,
360
+ query : subscription ,
342
361
variables : { name : "Iron Man" } ,
343
362
errorPolicy : "all" ,
344
363
} ) ;
345
364
const stream = new ObservableStream ( obs ) ;
346
365
347
- // Silence expected warning about missing field for cache write
348
- using _consoleSpy = spyOnConsole ( "warn" ) ;
366
+ // Silence warning about missing field for cache write
367
+ // TODO: Investigate this to see if we can silence this since this should
368
+ // not be expected.
369
+ using _consoleSpy = spyOnConsole ( "error" ) ;
349
370
350
371
enqueueProtocolErrors ( [
351
372
{
@@ -372,21 +393,14 @@ describe("GraphQL Subscriptions", () => {
372
393
} ) ;
373
394
374
395
it ( 'does not emit anything for GraphQL errors with no data in next result when `errorPolicy` is "ignore"' , async ( ) => {
375
- const query = gql `
376
- subscription UserInfo($name: String) {
377
- user(name: $name) {
378
- name
379
- }
380
- }
381
- ` ;
382
396
const link = new MockSubscriptionLink ( ) ;
383
397
const client = new ApolloClient ( {
384
398
link,
385
399
cache : new InMemoryCache ( ) ,
386
400
} ) ;
387
401
388
402
const obs = client . subscribe ( {
389
- query,
403
+ query : subscription ,
390
404
variables : { name : "Iron Man" } ,
391
405
errorPolicy : "ignore" ,
392
406
} ) ;
@@ -406,21 +420,14 @@ describe("GraphQL Subscriptions", () => {
406
420
} ) ;
407
421
408
422
it ( 'does not emit anything for network errors with no data in next result when `errorPolicy` is "ignore"' , async ( ) => {
409
- const query = gql `
410
- subscription UserInfo($name: String) {
411
- user(name: $name) {
412
- name
413
- }
414
- }
415
- ` ;
416
423
const link = new MockSubscriptionLink ( ) ;
417
424
const client = new ApolloClient ( {
418
425
link,
419
426
cache : new InMemoryCache ( ) ,
420
427
} ) ;
421
428
422
429
const obs = client . subscribe ( {
423
- query,
430
+ query : subscription ,
424
431
variables : { name : "Iron Man" } ,
425
432
errorPolicy : "ignore" ,
426
433
} ) ;
@@ -434,27 +441,20 @@ describe("GraphQL Subscriptions", () => {
434
441
it ( 'does not emit anything and completes observable for protocolErrors when `errorPolicy` is "ignore"' , async ( ) => {
435
442
const { httpLink, enqueueProtocolErrors } =
436
443
mockMultipartSubscriptionStream ( ) ;
437
- const query = gql `
438
- subscription UserInfo($name: String) {
439
- user(name: $name) {
440
- name
441
- }
442
- }
443
- ` ;
444
444
const client = new ApolloClient ( {
445
445
link : httpLink ,
446
446
cache : new InMemoryCache ( ) ,
447
447
} ) ;
448
448
449
449
const obs = client . subscribe ( {
450
- query,
450
+ query : subscription ,
451
451
variables : { name : "Iron Man" } ,
452
452
errorPolicy : "ignore" ,
453
453
} ) ;
454
454
const stream = new ObservableStream ( obs ) ;
455
455
456
- // Silence expected warning about missing field for cache write
457
- using _consoleSpy = spyOnConsole ( "warn " ) ;
456
+ // Silence warning about missing field for cache write
457
+ using _consoleSpy = spyOnConsole ( "error " ) ;
458
458
459
459
enqueueProtocolErrors ( [
460
460
{
@@ -475,7 +475,9 @@ describe("GraphQL Subscriptions", () => {
475
475
cache : new InMemoryCache ( ) ,
476
476
} ) ;
477
477
478
- const stream = new ObservableStream ( client . subscribe ( defaultOptions ) ) ;
478
+ const stream = new ObservableStream (
479
+ client . subscribe ( { query : subscription } )
480
+ ) ;
479
481
480
482
setTimeout ( ( ) => link . simulateComplete ( ) , 50 ) ;
481
483
@@ -489,15 +491,19 @@ describe("GraphQL Subscriptions", () => {
489
491
link,
490
492
} ) ;
491
493
492
- const stream = new ObservableStream ( client . subscribe ( options ) ) ;
494
+ const stream = new ObservableStream (
495
+ client . subscribe ( {
496
+ query : subscription ,
497
+ variables : { name : "Changping Chen" } ,
498
+ context : { someVar : "Some value" } ,
499
+ } )
500
+ ) ;
493
501
494
502
link . simulateResult ( results [ 0 ] ) ;
495
503
496
504
await expect ( stream ) . toEmitTypedValue ( results [ 0 ] . result ) ;
497
505
498
- expect ( link . operation ?. getContext ( ) . someVar ) . toEqual (
499
- options . context . someVar
500
- ) ;
506
+ expect ( link . operation ?. getContext ( ) . someVar ) . toEqual ( "Some value" ) ;
501
507
} ) ;
502
508
503
509
it ( "emits an error if the result has protocolErrors on it" , async ( ) => {
@@ -509,11 +515,14 @@ describe("GraphQL Subscriptions", () => {
509
515
cache : new InMemoryCache ( ) ,
510
516
} ) ;
511
517
512
- const obs = client . subscribe ( options ) ;
518
+ const obs = client . subscribe ( {
519
+ query : subscription ,
520
+ variables : { name : "Changping Chen" } ,
521
+ } ) ;
513
522
const stream = new ObservableStream ( obs ) ;
514
523
515
- // Silence expected warning about missing field for cache write
516
- using _consoleSpy = spyOnConsole ( "warn " ) ;
524
+ // Silence warning about missing field for cache write
525
+ using _consoleSpy = spyOnConsole ( "error " ) ;
517
526
518
527
enqueueProtocolErrors ( [
519
528
{
@@ -535,5 +544,7 @@ describe("GraphQL Subscriptions", () => {
535
544
} ,
536
545
] ) ,
537
546
} ) ;
547
+
548
+ await expect ( stream ) . toComplete ( ) ;
538
549
} ) ;
539
550
} ) ;
0 commit comments